Problem Solving Algorithms m3
Problem Solving Algorithms m3
IF (condition) THEN
Action (if condition is true)
ELSE
Action (if condition fails)
END IF
IF (condition) THEN
Action (if condition is true)
ELSE
Action (if condition fails)
END IF
Flowchart : IF… THEN… ELSE
The IF…THEN… ELSE statement:
the condition
• Can be any logic test:
– Is the rain falling?
– Is a number greater than another?
– Is a persons name equal to someone else’s name
ELSE
END IF
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
}
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
Try this one :
loops
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
Pseudo code:
a=0
while some condition exists
Change value
END While
}
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.
• 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?