SSC Gr10 ICT Q4 Module 1 WK 1 - v.01-CC-released-14June2021
SSC Gr10 ICT Q4 Module 1 WK 1 - v.01-CC-released-14June2021
ICT
(Special Science Class)
Computer Programming
Quarter 4 – Module 1:
Pascal - Loops
Information and Communications Technology– Grade 10
Special Science Class
Alternative Delivery Mode
Quarter 3 – Module 1: Computer Programming – PASCAL LOOPS
First Edition, 2021
Republic Act 8293, section 176 states that: No copyright shall subsist in any work
of the Government of the Philippines. However, prior approval of the government
agency or office wherein the work is created shall be necessary for exploitation of
such work for profit. Such agency or office may, among other things, impose as a
condition the payment of royalties.
Borrowed materials (i.e., songs, stories, poems, pictures, photos, brand names,
trademarks, etc.) included in this module are owned by their respective copyright
holders. Every effort has been exerted to locate and seek permission to use these
materials from their respective copyright owners. The publisher and authors do not
represent nor claim ownership over them.
This Self-Learning Module (SLM) is prepared so that you, our dear learners, can
continue your studies and learn while at home. Activities, questions, directions,
exercises, and discussions are carefully stated for you to understand each lesson.
Each SLM is composed of different parts. Each part shall guide you step-by-step as
you discover and understand the lesson prepared for you.
Pre-tests are provided to measure your prior knowledge on lessons in each SLM.
This will tell you if you need to proceed on completing this module or if you need to
ask your facilitator or your teacher’s assistance for better understanding of the
lesson. At the end of each module, you need to answer the post-test to self-check
your learning. Answer keys are provided for each activity and test. We trust that you
will be honest in using these.
In addition to the material in the main text, Notes to the Teacher are also provided to
our facilitators and parents for strategies and reminders on how they can best help
you on your home-based learning.
Please use this module with care. Do not put unnecessary marks on any part of this
SLM. Use a separate sheet of paper in answering the exercises and tests. And read
the instructions carefully before performing each task.
If you have any questions in using this SLM or any difficulty in answering the tasks in
this module, do not hesitate to consult your teacher or facilitator.
Thank you.
4
What I Need to Know
This module was designed and written with you in mind. It is here to help you master
the basic concepts of computer programming. The scope of this module permits it to
be used in many different learning situations. The language used recognizes the
diverse vocabulary level of students. The lessons are arranged to follow the standard
sequence of the course. But the order in which you read them can be changed to
correspond with the textbook you are now using.
What I Know
Directions: Read each item carefully. Choose the letter of the correct answer. Write
your answers on a separate sheet of paper.
5
4. Which of the following loop control statements terminates
the loop or case statement and transfers execution to the statement
immediately following the loop or case statement?
A. break statement
B. continue statement
C. goto statement
D. stop statement
What’s In
PASCAL PROGRAMMING
Pascal is a general-purpose, high-level language that was originally developed by
Niklaus Wirth in the early 1970s. It was developed for teaching programming as a
systematic discipline and to develop reliable and efficient programs.
Pascal isAlgol-based language and includes many constructs of Algol (Algorithmic
Language). Algol 60 is a subset of Pascal. Pascal offers several data types and
programming structures. It is easy to understand and maintain the Pascal programs.
Pascal has grown in popularity in the teaching and academics arena for various
reasons:
® Easy to learn
® Structured language
® It produces transparent, efficient and reliable programs
® It can be compiled on a variety of computer platforms
6
Facts about Pascal
® The Pascal language was named for Blaise Pascal, French mathematician
and pioneer in computer development.
® Niklaus Wirth completed development of the original Pascal programming
language in 1970.
® Pascal is based on the block structured style of the Algol programming
language.
® Pascal was developed as a language suitable for teaching programming as a
systematic discipline, whose implementations could be both reliable and
efficient.
® The ISO 7185 Pascal Standard was originally published in 1983.
® Pascal was the primary high-level language used for development in the
Apple Lisa, and in the early years of the Mac.
® In 1986, Apple Computer released the first Object Pascal implementation,
and in 1993, the Pascal Standards Committee published an Object-Oriented
Extension to Pascal.
Pascal allows the programmers to define complex structured data types and build
dynamic and recursive data structures, such as lists, trees and graphs. Pascal offers
features like records, enumerations, subranges, dynamically allocated variables with
associated pointers and sets.
Pascal allows nested procedure definitions to any level of depth. This truly provides
a great programming environment for learning programming as a systematic
discipline based on the fundamental concepts.
7
What’s New
Definition of Terms:
® Flow Chart -is a diagrammatic representation of sequence of logical steps of
a program. Flowcharts use simple geometric shapes to depict processes and
arrows to show relationships and process/data flow.
® Loop -In computer programming, a loop is a programming structure that
repeats a sequence of instructions until a specific condition is met.
Programmers use loops to cycle through values, add sums of numbers,
repeat functions, and many other things.
® Statement - In computer programming, a statement is a syntactic unit of an
imperative programming language that expresses some action to be carried
out.
® Syntax - refers to the rules that define the structure of a language. Syntax in
computer programming means the rules that control the structure of the
symbols, punctuation, and words of a programming language. Without syntax,
the meaning or semantics of a language is nearly impossible to understand.
8
What is It
1 while-do loop
Repeats a statement or group of statements while a given condition is true.
It tests the condition before executing the loop body.
2 for-do loop
Executes a sequence of statements multiple times and abbreviates the
code that manages the loop variable.
3 repeat-until loop
Like a while statement, except that it tests the condition at the end of the
loop body.
4 nested loops
You can use one or more loop inside any another while, for or repeat until
loop.
9
1. while-do loop - A while-do loop statement in Pascal allows repetitive
computations till some test condition is satisfied. In other words, it repeatedly
executes a target statement as long as a given condition is true.
For example,
while number>0do
begin
sum := sum + number;
number := number -2;
end;
When the condition becomes false, program control passes to the line immediately
following the loop.
Flow Chart
Here, key point of the while loop is that the loop might not ever run. When the
condition is tested and the result is false, the loop body will be skipped and the first
statement after the while loop will be executed.
Example
program whileLoop;
var
a:integer;
begin
a :=10;
while a<20do
begin
writeln('value of a: ', a);
a := a +1;
end;
end.
10
When the above code is compiled and executed, it produces the following result −
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
2. for-do Loop - A for-do loop is a repetition control structure that allows you to
efficiently write a loop that needs to execute a specific number of times.
Where, the variable-name specifies a variable of ordinal type, called control variable
or index variable; initial_value and final_value are values that the control variable
can take; and S is the body of the for-do loop that could be a simple statement or a
group of statements.
For example,
fori:=1 to 10dowriteln(i);
Here is the flow of control in a for-do loop −
• The initial step is executed first, and only once. This step allows you to
declare and initialize any loop control variables.
• Next, the condition is evaluated. If it is true, the body of the loop is executed.
If it is false, the body of the loop does not execute and flow of control jumps
to the next statement just after the for-do loop.
• After the body of the for-do loop executes, the value of the variable is either
increased or decreased.
• The condition is now evaluated again. If it is true, the loop executes and the
process repeats itself (body of loop, then increment step, and then again
condition). After the condition becomes false, the for-do loop terminates.
11
Flow Chart
Example
program forLoop;
var
a:integer;
begin
fora :=10 to 20do
begin
writeln('value of a: ', a);
end;
end.
When the above code is compiled and executed, it produces the following result −
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
value of a: 20
12
3. repeat-until loop - Unlike for and while loops, which test the loop condition at the
top of the loop, the repeat ... until loop in Pascal checks its condition at the
bottom of the loop.
A repeat ... until loop is similar to a while loop, except that a repeat ... until loop is
guaranteed to execute at least one time.
Syntax
repeat
S1;
S2;
...
...
Sn;
untilcondition;
For example,
repeat
sum := sum + number;
number := number -2;
until number =0;
Notice that the conditional expression appears at the end of the loop, so the
statement(s) in the loop execute once before the condition is tested.
If the condition is false, the flow of control jumps back up to repeat, and the
statement(s) in the loop execute again. This process repeats until the given
condition becomes true.
Flow Chart
Example
program repeatUntilLoop;
var
a:integer;
begin
a :=10;
(* repeat until loop execution *)
repeat
writeln('value of a: ', a);
a := a +1
until a =20;
end.
13
When the above code is compiled and executed, it produces the following result −
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
4. nested loops - Pascal allows using one loop inside another loop. Following
section shows few examples to illustrate the concept.
begin
for variable2:=initial_value2 to [downto] final_value2 do
begin
statement(s);
end;
end;
begin
while(condition2)do
begin
statement(s);
end;
statement(s);
end;
The syntax for a nested repeat ... until loop Pascal is as follows −
repeat
statement(s);
repeat
statement(s);
until(condition2);
until(condition1);
A final note on loop nesting is that you can put any type of loop inside of any
other type of loop. For example, a for loop can be inside a while loop or vice versa.
14
Example
The following program uses a nested for loop to find the prime numbers from 2 to 50
−
program nestedPrime;
var
i,j:integer;
begin
fori:=2 to 50do
begin
forj :=2 to ido
if(i mod j)=0then
break;{*if factor found,not prime *}
if(j =i)then
writeln(i,' is prime');
end;
end.
When the above code is compiled and executed, it produces the following result −
2 is prime
3 is prime
5 is prime
7 is prime
11 is prime
13 is prime
17 is prime
19 is prime
23 is prime
29 is prime
31 is prime
37 is prime
41 is prime
43 is prime
47 is prime
15
1 break statement
Terminates the loop or case statement and transfers execution to the
statement immediately following the loop or case statement.
2 continue statement
Causes the loop to skip the remainder of its body and immediately retest its
condition prior to reiterating.
3 goto statement
Transfers control to the labeled statement. Though it is not advised to use
goto statement in your program.
1. break Statement
The break statement in Pascal has the following two usages −
• When the break statement is encountered inside a loop, the loop is
immediately terminated and program control resumes at the next statement
following the loop.
• It can be used to terminate a case in the case statement (covered in the next
chapter).
If you are using nested loops (i.e., one loop inside another loop), the break
statement will stop the execution of the innermost loop and start executing the next
line of code after the block.
Example
program exBreak;
var
a:integer;
begin
a :=10;
(*while loop execution *)
while a<20do
begin
writeln('value of a: ', a);
a:=a +1;
if( a>15)then
(* terminate the loop usingbreak statement *)
break;
end;
end.
16
When the above code is compiled and executed, it produces the following result −
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
2. continue Statement
The continue statement in Pascal works somewhat like the break statement.
Instead of forcing termination, however, continue forces the next iteration of the
loop to take place, skipping any code in between.
For the for-do loop, continue statement causes the conditional test and increment
portions of the loop to execute. For the while-
do and repeat...until loops, continue statement causes the program control to
pass to the conditional tests.
begin
a :=10;
(* repeat until loop execution *)
repeat
if( a=15)then
begin
(* skip the iteration *)
a := a +1;
continue;
end;
17
When the above code is compiled and executed, it produces the following result −
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 16
value of a: 17
value of a: 18
value of a: 19
Flow Chart
Example
The following program illustrates the concept.
program exGoto;
label 1;
var
a : integer;
begin
a :=10;
(* repeat until loop execution *)
1: repeat
if( a=15)then
begin
(* skip the iteration *)
a := a +1;
goto1;
end;
18
When the above code is compiled and executed, it produces the following result −
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 16
value of a: 17
value of a: 18
value of a: 19
What’s More
Directions: Describe each type of loop statements and loop control statements
Loop Type Description
1 while – do loop
2 for – do loop
4 nested loop
5 Break
6 continue
7 Goto
19
What I Have Learned
Directions: Fill in the blanks with the correct term/s about Pascal Loop
statements. Choose from the given pool of words below. Write your answers on a
separate sheet of paper.
multiple while-do true for-do abbreviates
repeat-until end nested inside break
continue goto trace hard understand
discouraged
What I Can Do
20
Assessment
Directions: Read each item carefully. Write only the letter of the correct answer for
each question. Use a separate sheet for your answers.
21
Answer Key
22
References
® https://www.tutorialspoint.com/pascal/pascal_loops.htm
® https://woz-u.com/blog/what-is-syntax-in-computer-programming/
® https://en.wikipedia.org/wiki/Statement_(computer_science)
® https://techterms.com/definition/loop
® https://en.wikipedia.org/wiki/ALGOL
23