Problem Solving Algorithms
Problem Solving and Program
The IF…THEN… ELSE… statement
Simulating Decisions
The IF…THEN… ELSE statement
• Used to simulate choice based on a condition
• The structure:
IF (condition) THEN
Action (if condition is true)
ELSE
Action (if condition fails)
END IF
• The actions between the THEN and ELSE statements
can be any number of statements:
– READ statements
– PRINT statements
– Calculations
– Even other IF statements
The IF…THEN… ELSE statement:
the structure
The structure:
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
• May contain two or more logic tests:
– A < B AND B > C
– myName = “Jenoir” OR myName = “Latoya”
– A <= C OR S >= T
• Please note the use of AND and OR
The IF…THEN… ELSE statement:
the condition
• If the condition is true
– the action(s) after the THEN part is/are executed.
• If the condition is false
– the action(s) after the ELSE part is/are executed.
• END IF – signals the end of the if statement
The IF…THEN… ELSE statement:
Examples
The example – Real World Scenario:
IF (belly is full) THEN
Stop drinking and eating
ELSE
Drink and eat all night
END IF
• What is the condition?
• What happens if the condition is true?
• What happens if the condition is false?
The IF…THEN… ELSE statement:
Examples
The example – Real World Scenario:
Sometimes no action needs to be taken if the condition is
false
IF (jar is empty) THEN
Fill with cookies
(ELSE)
END IF
• In this case we have a NULL ELSE statement
– No actions following the else statement
The IF…THEN… ELSE statement:
Examples
The example – Closer to pseudocode:
IF (A > 10) THEN
PRINT A, “ is greater than 10”
ELSE
Print A, “ is less than 10”
END IF
• What is the condition?
• What happens if the condition is true?
• What happens if the condition is false?
The IF…THEN… ELSE statement:
Examples
The example – Real World Scenario:
IF (surname = “Prentis”) THEN
Print “ Nicholas is a Prentis”
(ELSE)
END IF
• What is the condition?
• What happens if the condition is true?
• What happens if the condition is false?
The IF…THEN… ELSE statement:
Examples
The example – Here is a complete code:
PRINT “Please enter first number (press enter after)”
READ Num1
PRINT “Please enter second number (press enter after)”
READ Num2
PRINT “Please enter third number”
READ Num3
IF (Num1 > Num 2 AND Num2 >Num3) THEN
DIFF = Num1 – Num3
ELSE
DIFF = Num2 - Num3
END IF
SUM = Num 1 + Num2 + Num3
Print SUM, DIFF
CLASS WORK
• Write a structure algorithm to prompt the user to
input the mark the student received in a test. If
the mark is less than 60, output the word “Fail”,
otherwise output the word “Pass”.
• Write a structure algorithm that prompts the user
to input the pass mark and the mark a student
received in a test. Output the word “Fail” or
“Pass” accordingly.
• Write a structured algorithm that prompts the
user to input two unequal values that are stored
in variables A and B. It should print the higher
value.
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?
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
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
• 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:
a=0
while some condition exists
Change value
END While
- priming the loop
Do something
}
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?
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
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?