0% found this document useful (0 votes)
18 views18 pages

Csc211 2023 Class - Iteration

Uploaded by

osakuejeremiah
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)
18 views18 pages

Csc211 2023 Class - Iteration

Uploaded by

osakuejeremiah
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/ 18

STRUCTURED

PROGRAMMING
in PASCAL
CSC211

ITERATION STATEMENTS
ITERATION STATEMENTS

•FOR - DO
•WHILE-DO
•REPEAT-UNTIL

•Iteration in programming
means REPEATING steps, or
instructions, over and over
again. This is often called a
‘LOOP’.
•ITERATION continues as long as
the condition remains TRUE. It
stops when the condition
becomes FALSE except on
exceptional cases.
ITERATION STATEMENT
FLOWCHART

TRUE
CONDITION

FALSE
FOR and
WHILE

CONDITION FALSE

TRUE

STATEMENT(S)

STATEMENT(S)

REPEAT - UNTIL
CONDITION TRUE

FALSE
ALGORITHM
The algorithm used is as follows:
1. Initialize the counter to zero.
2. Increment the counter by 1.
3. Test the counter to see if it is
less than or equal to
MAXIMUM VALUE. WHEN IT
IS TRUE STEP 4 OTHERWISE
STEP 5
4. Perform the statement.
GOTO STEP 2
5. EXIT LOOP
The FOR Loop
The general form of the FOR
construct is as follows:

FOR variable := expression-1 TO


expression-2 DO statement;
OR
FOR variable := expression-1
DOWNTO expression-2 DO
statement;

where:
i. variable is the loop counter,
ii. expression-1 is the initial
value, and
iii. expression-2 is the final
value.

The variable, expression-1, and


expression-2 can be of any type
except REAL.
The FOR Loop
E.g

FOR K := 1 TO 5 DO
WRITELN(‘Hello World’);

How many times will the Loop


and Write statement be
executed

FOR counter := 1 TO 5 DO;


WRITELN(‘Hello World.’);

NOTE: There is a semicolon ; at


the end of the FOR line.
The FOR Loop
Program to print Hello World 5 times
on the screen:

PROGRAM ForLoop(OUTPUT);
USES CRT;
VAR
counter :INTEGER;
BEGIN
FOR counter := 1 TO 5 DO
WRITELN('Hello World..’);
WRITELN;
WRITELN('Press ENTER to continue');
READLN
END.

What changes will be made to the


program to make it print the message
10 times?

REWRITE the program using


FOR - DOWNTO - DO
PROGRAM TO PRINT NUMBERS

PROGRAM Loop1(input,output);
USES CRT;
VAR K,N :INTEGER;
BEGIN
READLN(N);
FOR K := 1 TO N DO WRITELN(K);
WRITELN('Press ENTER to continue..’);
READLN
END.

PROGRAM Loop2(input,output);
USES CRT;
VAR K,N :INTEGER;
BEGIN
READLN(N);
FOR K := 1 TO N DO
BEGIN
WRITE('Number is = ');
WRITELN(K);
END;
WRITELN('Press ENTER to continue..');
READLN
END.
The WHILE Loop
WHILE condition DO statement;
PROGRAM Loop3(input,output);
USES CRT;
VAR K,N :INTEGER;
BEGIN
N:=10;
K:=1;
WHILE K <= N DO
BEGIN
WRITELN('Number ',K);
K := K+1;
END;
WRITELN('Press ENTER to continue..’);
READLN
END.

WHAT WILL HAPPEN WHEN THESE


STATEMENTS ARE EXCHANGED ?
K := K+1;
WRITELN('Number ',K);
CLASS WORK - 1
Use the WHILE loop to write a
program that display a
MULTIPLICATION TABLE as in the
following example:
X*1=Y
X*2=Y
X*3=Y
X*4=Y
X*5=Y
X*6=Y
X*7=Y
X*8=Y
X*9=Y
X*10=Y
X*11=Y
X*12=Y
The value of X is received from the
keyboard and the value Y is the
result.
CLASS WORK - 2

WRITE A PROGRAM THAT SOLVES


THE FORMULA BELOW:
𝑵

𝑺𝑼𝑴 = ෍ 𝑿 ∗ 𝑲
𝑲=𝟏

FOR statement

HINT:
The program should assign values
to the following variables: N and X.
The value of K ranges from 1 to N
The REPEAT Loop

This loop is used to execute a group


of statements until a specified
condition is met. It takes the form:

REPEAT
statement-1;
statement-2;
...
statement-n;
UNTIL condition;

As you can see , this loop is ready to


execute more than one statement
without using the BEGIN-END blocks.
The REPEAT Loop
PROGRAM Loop4(input,output);
USES CRT;
VAR K,N :INTEGER;
BEGIN
N:=10;
K:=1;
REPEAT
WRITELN('Number ',K);
K := K+1;
UNTIL K>N;
WRITELN('Press ENTER to continue..’);
READLN
END.

EXCHANGE THESE STATEMENTS,


WHAT WILL HAPPENED?
K := K+1;
WRITELN('Number ',K);
CLASS WORK - 3

WRITE A PROGRAM THAT SOLVES


THE FORMULA BELOW:
𝑵

𝑺𝑼𝑴 = ෍ 𝑿 ∗ 𝑲
𝑲=𝟏

REPEAT - UNTIL statement

HINT:
The program should assign values
to the following variables: N and X.
The value of K ranges from 1 to N
NESTED LOOPS
Loops can be used inside another loop.
In this case it is said that the inner loop
is nested inside the outer loop.
You can nest as many loops as you wish
inside one another, according to your
application.
LOOPS AND NESTED LOOPS ARE VERY
USEFUL IN ARRAY / MATRICES
PROGRAMMING.

PROGRAM NestedLoops(OUTPUT);
USES CRT;
VAR
Row, Column :INTEGER;
BEGIN
FOR Row := 1 TO 3 DO
BEGIN
FOR Column := 1 to 5 DO
WRITE(Column, ' ');
WRITELN
END;
READLN
END.
WHAT DOES THIS PROGRAM DO?

PROGRAM Loop1(input,output);
USES CRT;
VAR I,J :INTEGER;
BEGIN
FOR I := 1 TO 6 DO
BEGIN
FOR J := 1 TO I DO
WRITE('*’);
WRITELN;
END;
FOR I := 6 DOWNTO 1 DO
BEGIN
FOR J := I DOWNTO 1 DO
WRITE('*’);
WRITELN;
END;
WRITELN('Press ENTER to
continue..');
READLN
END.
DRILL 2: NESTED LOOPS

Write Pascal program to simulate the


DIGITAL watch, it should display the
HR: MIN : SEC
24 : 60 : 60

NOTE: This appears as 3 nested loops


THE END

You might also like