For Loop-Problem Solving and Program m3
For Loop-Problem Solving and Program m3
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?
So what is a Loop?
Any number of steps that are being executed a
number of times
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.
}
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)
FOR i = 1 to 5
PRINT name
NEXT i
END FOR
STOP
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
}
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)
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)
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 :
START
FOR i = 0 to 20 STEP 5
Print i
NEXT i
END FOR
STOP
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)
READ name
END WHILE
STOP
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
a=1
Repeat
Read Name, Age
Print “My name is “, Name
Print “I am “, Age
a = a +1
Until a = 10
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.
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?