0% found this document useful (0 votes)
95 views

SSC Gr10 ICT Q4 Module 1 WK 1 - v.01-CC-released-14June2021

Computer Programming for Grade 10 Science Class

Uploaded by

Vj Aleser
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
95 views

SSC Gr10 ICT Q4 Module 1 WK 1 - v.01-CC-released-14June2021

Computer Programming for Grade 10 Science Class

Uploaded by

Vj Aleser
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

10

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.

Published by the Department of Education


Secretary: Leonor Magtolis Briones
Undersecretary: Diosdado M. San Antonio

Development Team of the Module


Writer: RENANTE R. ALESER
Reviewer: MARGIE B. ENANORIA
Layout Artist: CARLA L. KONG
Subject Area Supervisor: MERIAM T. ABADILLA
Management Team: RONALD G. GUTAY, ESTELA B. SUSVILLA,
MARY JANE J. POWAO, AQUILO A. RENTILLOSA,
CRISTINA T. REMOCALDO
Division ADM Coordinator: RYAN B. REDOBLADO

Printed in the Philippines by:

Department of Education –Region7, Central Visayas

Office Address: Department of Education – Carcar City Division (Learning


Resources Management Section
P. Nellas St. Población III, Carcar City, Cebu
Telefax: (032)77561
E-mail Address: [email protected]
10
ICT
(Special Science Class)
Computer Programming
Quarter 4 – Module 1:
Pascal - Loops
Introductory Message

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.

After going through this module, you are expected to:


▪ demonstrate skills in using loop statements in Pascal programming.
A. Describe the loop statements in Pascal programming.
B. Use loop statements in Pascal programming.

What I Know

Directions: Read each item carefully. Choose the letter of the correct answer. Write
your answers on a separate sheet of paper.

1. What control structure allows us to execute a statement or group of


statements multiple times?
A. loop statement
B. repeat statement
C. cycle statement
D. continuous statement

2. Repeats a statement or group of statements while a given condition is


true
A. for – do loop
B. while – do loop
C. repeat – until loop
D. nested loop

3. What do you call a loop inside another loop?


A. repeat – until loop
B. for – do loop
C. nested loop
D. while – do loop

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

5. The use of this statement is highly discouraged in any programming


language because it makes difficult to trace the control flow of a program,
making the program hard to understand and hard to modify.
A. jumpto
B. proceedto
C. directto
D. goto

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

Features of the Pascal Language


® Pascal is a strongly typed language
® It offers extensive error checking
® It offers several data types like arrays, records, files and sets
® It offers a variety of programming structures
® It supports structured programming through functions and procedures
® It supports object-oriented programming

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.

Why to use 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.

Among the most amazing implementations of Pascal are −


® Skype
® Total Commander
® TeX
® Macromedia Captivate
® Apple Lisa
® Various PC Games
® Embedded Systems

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.

® Start/Stop - Used at the beginning and end of the


algorithm to show start and end of the program.

® Process-Indicates processes like mathematical operations.

® Input/ Output - Used for denoting program inputs and outputs.

® Decision - Stands for decision statements in a


program, where answer is usually Yes or No.

® Arrow - Shows relationships between different shapes.

® On-page Connector- Connects two or more parts of a


flowchart, which are on the same page.

® Off-page Connector- Connects two parts of a flowchart which is


spread over different pages.

8
What is It

Lesson Pascal – LOOPs


1
There may be a situation, when you need to execute a block of code several
number of times. In general, statements are executed sequentially. The first
statement in a function is executed first, followed by the second, and so on.
Programming languages provide various
control structures that allow for more
complicated execution paths.
A loop statement allows us to execute a
statement or group of statements multiple
times and following is the general form of a
loop statement in most of the programming
languages.
Pascal programming language provides the
following types of loop constructs to handle
looping requirements.

Sr.No Loop Type & Description

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.

The syntax of a while-do loop is −


while (condition) do S;
Where, condition is a Boolean or relational expression whose value would be true
or false and S is a simple statement or group of statements within BEGIN ... END
block.

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.

The syntax for the for-do loop in Pascal is as follows −


for< variable-name >:=<initial_value> to [down to]<final_value>do
S;

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.

The syntax for a nested for-do loop statement in Pascal is as follows −


for variable1:=initial_value1 to [downto] final_value1 do

begin
for variable2:=initial_value2 to [downto] final_value2 do

begin
statement(s);
end;
end;

The syntax for a nested while-do loop statement in Pascal is as follows −


while(condition1)do

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

Loop Control Statements


Loop control statements change execution from its normal sequence. When
execution leaves a scope, all automatic objects that were created in that scope are
destroyed.

Pascal supports the following control statements.


Sr.No Control Statement & Description

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.

The syntax for a break statement in Pascal is as follows −


break;
Flow Chart

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.

The syntax for a continue statement in Pascal is as follows −


continue;
Flow Chart
Example
program exContinue;
var
a:integer;

begin
a :=10;
(* repeat until loop execution *)
repeat
if( a=15)then

begin
(* skip the iteration *)
a := a +1;
continue;
end;

writeln('value of a: ', a);


a := a+1;
until( a=20);
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

3. goto Statement - A goto statement in Pascal provides an unconditional jump


from the goto to a labeled statement in the same function.
NOTE − Use of goto statement is highly discouraged in any programming language
because it makes difficult to trace the control flow of a program, making the program
hard to understand and hard to modify. Any program that uses a goto can be
rewritten so that it doesn't need the goto.

The syntax for a goto statement in Pascal is as follows −


gotolabel;
...
...
label: statement;
Here, label must be an unsigned integer label, whose value can be from 1 to 9999.

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;

writeln('value of a: ', a);


a:= a +1;
until a =20;
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

Please note that −


• In Pascal, all labels must be declared before constant and variable
declarations.
• The if and goto statements may be used in the compound statement to
transfer control out of the compound statement, but it is illegal to transfer
control into a compound statement.

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

3 repeat – until 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

A loop statement allows us to execute a statement or group of statements


1_______times.2________ loop repeats a statement or group of statements while a
given condition is 3_____. It tests the condition before executing the loop
body.4______ loop executes a sequence of statements multiple times and
5___________ the code that manages the loop variable.6____________loop like a
while statement, except that it tests the condition at the 7_____ of the loop
body.8______ loopsallows using one loop inside another loop. In loop nesting, you
can put any type of loop 9______ of any other type of loop. 10______ statement
terminates the loop or case statement and transfers execution to the statement
immediately following the loop or case statement.11_______ statement causes the
loop to skip the remainder of its body and immediately retest its condition prior to
reiterating.12_____ statement transfers control to the labeled statement. Use of goto
statement is highly 13___________ in any programming language because it makes
difficult to 14______ the control flow of a program, making the program hard to
15__________ and hard to modify.

What I Can Do

Directions: Write the syntax of the following loop statements.


1. while-do loop
2. for-do loop
3. repeat-until loop
4. nested loop
5. break statement
6. continue statement
7. goto statement

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.

1. Which of the following is NOT a loop statement in Pascal programming?


A. while-do
B. for-do
C. break
D. nested
2. How would you describe a go-to statement?
A. Causes the loop to skip the remainder of its body and immediately
retest its condition prior to reiterating.
B. Provides an unconditional jump from the goto to a labeled statement
in the same function.
C. Terminates the loop or case statement and transfers execution to the
statement immediately following the loop or case statement.
D. Allows repetitive computations till some test condition is satisfied.
3. This type checks its condition at the bottom of the loop and is guaranteed to
execute at least one time.
A. while-do loop
B. for-do loop
C. repeat-until loop
D. nested loop
4. In computer programming, there is a structure that repeats a sequence of
instructions until a specific condition is met.
A. Flow chart
B. Statement
C. Syntax
D. Loop
5. What do you call a loop inside another loop?
A. nested loop
B. repeat – until loop
C. for – do loop
D. while – do loop

21
Answer Key

What I Have Learned What I Know


1. C
1. multiple 2. A
2. while-do 3. A
3. true 4. A
4. for-do 5. A
5. abbreviates
6. repeat-until
7. end
8. nested
9. inside
10. break Assessment
11. continue
12. goto 1. d
13. trace 2. d
14. understand 3. a
4. a
15. discouraged
5. a

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

For inquiries or feedback, please write or call:

Department of Education – Carcar City Division (Learning Resources


Management Section)

P. Nellas St., Poblacion III, Carcar City, Cebu Philippines 6019


Tel. No. 4878495

Email Address: [email protected]

23

You might also like