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

For Loop-Problem Solving and Program m3

The document discusses looping statements and constructs that allow code to be executed multiple times. It covers for loops, which execute a set number of times, while loops, which execute until a condition is no longer met, and repeat until loops. Examples are provided in pseudocode to demonstrate how to use these loops to iterate through a known number of items, iterate while a condition holds true, count iterations, and calculate running totals or averages. Loops are described as a powerful way to perform repetitive tasks like summing a dynamic number of inputs without needing separate variables for each item.

Uploaded by

Amos
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)
41 views

For Loop-Problem Solving and Program m3

The document discusses looping statements and constructs that allow code to be executed multiple times. It covers for loops, which execute a set number of times, while loops, which execute until a condition is no longer met, and repeat until loops. Examples are provided in pseudocode to demonstrate how to use these loops to iterate through a known number of items, iterate while a condition holds true, count iterations, and calculate running totals or averages. Loops are described as a powerful way to perform repetitive tasks like summing a dynamic number of inputs without needing separate variables for each item.

Uploaded by

Amos
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/ 26

PROBLEM SOLVING AND

PROGRAM
Looping statements
Executing steps many times
LOOPING
 What if there are a number of steps that must be
done several times, would you re-write those steps for
each time you needed them to be executed?

 What if you wanted to calculate the total of a set of


numbers ( say 15 different numbers), would you
create a variable for each one?

 What if you wanted to perform an operation as long


as a condition exists, how would you know that the
condition continues to exist and perform these loops?

 These are some of the questions that the looping


structure was created to answer.
LOOPING

 So what is a Loop?
 Any number of steps that are being executed a
number of times

 How do we govern a loop?


 A condition (or number of conditions)
 A number of times ( iterations )
LOOPING
 Real World Examples:
 A Condition
 While my ring is not found, search the house
 While shoes are dirty, buff with cloth

 A number of times
 Scrub floor 10 times for sheen
 Knock door three times
PSEUDO CODE CONSTRUCTS
 A Condition – The While or Repeat Until Loop
 Condition must be satisfied for loop to continue to be
executed.

 A Number of iterations – The FOR Loop


 A number of exact times for the loop to be executed
THE FOR LOOP
 The FOR loop construct syntax (of course this is Pseudo code)

}
FOR <variable> = <beginning value> to <ending value>
Do something
END FOR

Pseudo code:
FOR i = 1 to 5 - loop begins here

ACTION –
this action may be any number of other statements ( IF, PRINT, READ, etc)

NEXT i - tells loop to go to next value of i

END FOR - end the for loop

*Reserved words are in RED


PSEUDO CODE EXAMPLE – FOR LOOPS
START
READ name

FOR i = 1 to 5
PRINT name

NEXT i
END FOR
STOP

 What does the above code do?


 What is the first and last value of the variable i?
 When does the code come stop printing person name?
PASCAL EXAMPLES – ONE LINE
 For Counter := 1 to 7 do
 writeln('for loop');

This code prints the string ‘for loop’ seven times in different lines
PASCAL EXAMPLES – MORE THAN ONE
LINE

 For Counter := 1 to 5 do
 Begin
 gotoxy(25, 5 + Counter);
 Writeln('I');
 End;

This code causes the program to print the letter ‘I’ at different x
and y positions

Try these codes in Pascal and see how they work


THE FOR LOOP – ADDING STEPS
 The FOR loop construct syntax (of course this is Pseudo code)

}
FOR <variable> = <beginning value> to <ending value> STEP <incremental value>
Do something
END FOR

Pseudo code:
FOR i = 1 to 10 step 2 - counts by 2: 1 => 1, 3, 5, 7, 9

ACTION –
this action may be any number of other statements ( IF, PRINT, READ, etc)

NEXT i - tells loop to go to next value of i

END FOR - end the for loop

Note that the number after step can be any particular number
even a variable if you so choose
*Reserved words are in RED
THE FOR LOOP – ADDING STEPS
Example:
Print a table to find the square and cube

Pseudo code:
FOR i = 1 to 10 step 2 - counts by 2: 1 => 1, 3, 5, 7, 9
ACTION –
this action may be any number of other statements ( IF, PRINT, READ, etc)

NEXT i - tells loop to go to next value of i

END FOR - end the for loop

Note that the number after step can be any


particular number even a variable if you so
choose
*Reserved words are in RED
PSEUDO CODE EXAMPLE – FOR LOOPS
Print a table finding the square and cube of all even numbers
between 2 and 20 inclusive

START
Print “Number”, “Square”, “Cube” {prints the table headings}

FOR i = 2 to 20 STEP 2
Print i , i^2 , i^3

NEXT i
END FOR
STOP

 Notice that the beginning value for the loop is 2 and the ending is 20.
 Notice that the incremental value is 2.
 Try to do a trace table for this example
PSEUDO CODE EXAMPLE – FOR LOOPS
Try this one :

Print a table finding the square


and cube of all odd numbers
between 1 and 20 inclusive
PSEUDO CODE EXAMPLE – FOR LOOPS

START

FOR i = 0 to 20 STEP 5
Print i

NEXT i
END FOR
STOP

 What does the above code do?


 Notice that the beginning value for the loop is 0 and the ending is 20.
 Notice that the incremental value is 5.
 What will be the various values for i?
THE WHILE LOOP
 The FOR loop construct syntax (of course this is Pseudo code)

Pseudo code:
while some condition exists
Do something
Change value
END While }
a=0 - priming the loop
While (a < 10) - starting while loop

ACTION –
this action may be any number of other statements ( IF, PRINT, READ, etc)

a = a +1 - manipulating loop condition

END WHILE - ending while loop

*Reserved words are in RED / IMPORTANT statements


PSEUDO CODE EXAMPLE – WHILE LOOP
START
i=0
READ name
WHILE (i<10)
PRINT NAME
i=i+1
END WHILE
STOP

 What does the above code do?


 What is the first and last value of the variable i?
 When does the code come stop printing person name?
PSEUDO CODE EXAMPLE – WHILE LOOP
START
READ name
WHILE (name<> “end”)
Print “Hello “, name
Print “Please enter your age”
Read age

If age > 50 then


Print “You qualify for the golden citizen account”
(Else)
EndIf

READ name
END WHILE
STOP

 What does the above code do?


 When does the code come stop?
PASCAL EXAMPLES – MORE THAN ONE
LINE

While Ch <> 'q' do


Begin
Writeln('I told you press ''q'' to exit!!');
Ch := Readkey;
End;

 What does this code do?


 What is the purpos of the Readkey statement?
THE REPEAT…UNTIL LOOP
 The FOR loop construct syntax (of course this is Pseudo code)

Repeat
Do something
Change value
Until some condition is satisfied

Pseudo code:
a=0
Repeat

ACTION –
this action may be any number of other statements ( IF, PRINT, READ, etc)

a = a +1
Until a = 10

*Reserved words are in RED / IMPORTANT statements


THE REPEAT…UNTIL LOOP

a=1
Repeat
Read Name, Age
Print “My name is “, Name
Print “I am “, Age
a = a +1
Until a = 10

*Reserved words are in RED / IMPORTANT statements


THE REPEAT…UNTIL LOOP
Read student_name
Repeat
Read grade_1, grade_2, grade_3
average = (grade_1+ grade_2+ grade_3)/3
Print student_name, “ - “ , average
Read student_name
Until Name = End

*Reserved words are in RED / IMPORTANT statements


THE REPEAT…UNTIL LOOP –
PASCAL EXAMPLE
Program Repeat_until_example:
Var YN : String;
Begin
Writeln('Y(YES) or N(NO)?');

Repeat {repeat the code for at least one time}


YN := Readkey ; {reads a key stroke}
If YN = 'y' then Halt; {Halt - exit}
If YN = 'n' then Writeln('Why not? Exiting...');
Delay(1800); { wait a second plus 800 milliseconds }

Until (YN = 'y') OR (YN = 'n');


End.
COUNTING AND RUNNING TOTALS
 One of the most powerful uses of the Loops
 For example:
 What if you wanted to find the sum or average of 30
numbers? Would you use 30 different variables?
 What if the amount of numbers were unknown but
you still have to find their sum or average etc?

 The answer:
 Use a variable to count
 Use a variable to add the numbers as they are entered
PSEUDOCODE EXAMPLE – FOR LOOPS
 Here is a simple example:
 Write a pseudocode to add 10 numbers:

START
sum = 0 {this will store the sum}

FOR i = 1 to 10
READ number
sum = sum + number {remember work out the right hand side first}

NEXT i
END FOR
Print sum
STOP

 NB
 sum = sum + number
 The starting value of sum is 0. therefore if we entered 10 for the number
then we would have
 sum = 0 + 10.

 At which point sum now becomes 10


PSEUDOCODE EXAMPLE – FOR LOOPS
 Here is a simple example:
 Write a pseudocode to find the average 10 numbers:

START
sum = 0
average =0 {this will store the sum}

FOR i = 1 to 10
READ number
sum = sum + number {remember work out the right hand side first}

NEXT i
END FOR
average = sum / 10
PRINT average
STOP

 NB
 average = sum / 10
 Note that the average has to calculate after the sum
WHAT IF YOU WERE UNCERTAIN
ABOUT THE AMOUNT OF
NUMBERS TO BE ENTERED?

You might also like