Handout If and Loop Week 5
Handout If and Loop Week 5
LESSON OBJECTIVES
ACTIVITY 1
Pair Activity: Consider the following fragment of pseudocode, write the output of each IF
statement when it would have been executed by the program.
a) IF (a > b) PRINT b
b) IF (c <> b) PRINT “Values are unequal” ELSE PRINT “Values are equal”
c) IF (b >= c) PRINT a
d) IF (a <c) AND (c > b) PRINT “Hello”
e) IF (a <c) AND (c > b) PRINT “Hello” ELSE “Hurray
f) IF (a <c) OR (c > b) PRINT “Hello”
ACTIVITY 2: Develop a psuedocode read in TWO unique integer values, call n1, n2. The
program should determine and print the largest number entered.
ACTIVITY 3: Develop a psuedocode read in FOUR unique integer values, call n1, n2,n3, n4.
The program should determine and print the largest number entered.
HOMEWORK
Write a program to accept the name and test score of 10 students. The program must print the
average score. The algorithm must prompt and read in each student’s data separately
POSSIBLE SOLUTION TO ACTIVITY 3
Develop a psuedocode read in FOUR unique integer values, call NUM1, NUM2, NUM3,
NUM4. The program should determine and print the largest number entered.
Most Efficient Solution is to set one of the values to be the largest and then compare the other
values to largest.
Pseudocode FINDINGLARGEST
This program will determine and print the largest of four numbers
Declare variables NUM1, NUM2, NUM3, NUM4, largest as integers;
BEGIN
PRINT “Enter four numbers separated by a space and then press the enter key”
READ NUM1, NUM2, NUM3, NUM4
//set one of the numbers to be the largest
largest = NUM1
Explaining the logics of the program above: Let say the user enters 3, 8, 5, 10
The program will first see the first number, which is 3, as the largest.
The program will then move to first IF statement and compare the second number, 8, to what is
assigned as the largest, which is 3. If num2, which is 8, is greater than the largest, then it will be
the largest. In this example, 8 will become the new largest because it greater than 3.
The program then moves to the second IF statement, and compares the third number to the
largest, if the third number is larger, then it will be the largest. In this example, the third number
is not larger, so the second number, 8, remains the largest.
The program then moves to the third IF statement and compares the fourth number to what is
assigned as the largest value. If the fourth number is larger, then it will be stored as the largest. In
this example, the fourth is larger, so it will be stored as the largest.
The numbers 3, 8, 5, 10 are sample values that a user may enter into the program. Please note
that it can be any four random numbers.
INFORMATION TECHNOLOGY
PROBLEM SOLVING AND COMPUTING
COTROL STRUCTURES
REPETITION OR LOOPING
Assignment is the physical act of placing a value into a variable. Assignment can be shown using
set = 5;
set = num + set;
The left side is the variable a value is being stored in and the right side is where the variable is being
accessed. When a variable is assigned a value, the old value is written over with the new value so the old
value is gone. x = 5 does not mean that x is equal to 5; it means set the variable x to have the value 5. Give
x the value 5, make x equal to 5.
USING ARRAYS – An array is a structure which acts as a large variable that can store a fixed number of
data of the same data type.
When to use arrays – Instead of declaring multiple variables to store a series data on the same
thing(entity), use one big storage location called array. For example, you are ask to write a program to
accept the id_number of 50 employees. Which would you use? 50 individual variables or one variable that
will make space for the 50 id_numbers? Use the most efficient solution.
The array name is like a variable name, which should match what the array will store.
E.g. You are asked to store the firstname and lastname of 30 students in a class.
Declare array as firstname: array [1..30] of string //all first name are store in the same column
Declare array as lastname: array [1..30] of string //all first name are store in the same column
REPITITION/LOOPING
Repetition is a construct that allows instructions to be executed multiple times (IE repeated).
The repetitive performance of the same statements is called looping. Examples of repetitive constructs are:
(a) FOR – This is used when the number of iterations or the number of input is known to the
programmer
(b) WHILE - This is used when the number of iteration is known or when the number of inputs is
known.
In a repetition problem
– Count is initialized
– Tested
– incremented – increase by 1
PSEUDOCODE STUDENT
This pseudocode will accept the name and score of 10
students
Declare array as names: array [1..10] of string
Declare array as scores: array [1..10] of real
Declare variable count as integer
BEGIN
FOR (count = 1 TO 10) DO
BEGIN
PRINT “Enter name”
READ names[count]
PRINT “Enter score”
READ scores[count]
ENDFOR
END
The program in the box on the right solves the long program codes shown on the left.
PSEUDOCODE STUDENT PSEUDOCODE STUDENT
This pseudocode will accept the name and score of This pseudocode will accept the name and score of 10
10 students students
Declare variable Declare array as names: array [1..10] of string
N1,N2,N3,N4,N5,N6,N7,N8,N9,N10 as string Declare array as scores: array [1..10] of real
Declare variable S1,S2,S3,S4,S5,S5,S7,S8,S9,S10 as Declare variable count as integer
real BEGIN
FOR (count = 1 TO 10) DO
BEGIN BEGIN
PRINT “Enter name 1:” PRINT “Enter name”
READ N1 READ names[count]
PRINT “Enter name 1:”
READ N1
PRINT “Enter score 1”
READ S1
PRINT “Enter name 2:”
READ N2
PRINT “Enter score 2”
READ S2
PRINT “Enter name 3:”
READ N3
PRINT “Enter score 3”
READ S3
PRINT “Enter name 2:”
READ N4
PRINT “Enter score 4”
READ S4 Which solution
PRINT “Enter name 5:”
READ N5 would you use?
PRINT “Enter score 5”
READ S5
Programming tip:
When you need to develop
programs to process data for
multiple entity, (for example, name
and age of 20 people, size and
weight of 20 puppies, etc.) do not
waste time declaring multiple
variables, use an ARRAY.
Additionally, an array needs a FOR
loop to keep track of its index of
location.
When you want to read or store values in a location in an array, you must specify the name of the array
and the index. See the format below:
READ array_name [index] E.g. READ score [count]
Since a FOR loop will lessen the number of instructions by repetition, the loop control variable in the
array is storing each iteration. When you instruct the computer to repeat from 1 to 10 by writing FOR
(count = 1 TO 10) DO, whatever is written between DO and ENDFOR will be executed x times. In this
case, the instructions will be repeated 10 times. And on each repetition, the user has the opportunity to
enter different values.
When the FOR loop executes the first time, the value 1 will be in the variable count, which is pointing to
the first index of the array and the code READ score[count] will have the value 50.
When the FOR loop executes the second time, the value 2 will be in the variable count, which is pointing
to the seconding index of the array and the code READ score[count] will have the value 90.
This process continues until it stops after the loop has finish its 10 th repetition.
Since a loop (FOR or WHILE) will keep a count of the repetition, the array would then depend on the
loop control variable to indicate the index or the location in which to store the value entered by the user.
Example
PRINT “THE LIST OF SCORES IS:”
FOR (count = 0 TO 10) DO
PRINT score[count]
ENDFOR
Once you have accepted the values in an array using the FOR loop, you would end that FOR loop and
then implement another FOR loop to print the elements. See example below:
PSEUDOCODE STUDENT
This pseudocode will accept the name and score of 10 students
Declare array as names: array [1..10] of string
Declare array as scores: array [1..10] of real
Declare variable count as integer
BEGIN
//accepting the data in the list
FOR (count = 1 TO 10) DO
BEGIN
PRINT “Enter name”
READ names[count]
PRINT “Enter score”
READ scores[count]
ENDFOR
END
COUNTING
PSEUDOCODE STUDENT
This pseudocode will accept the name and score of 10
students
Declare array as names: array [1..10] of string
Declare array as scores: array [1..10] of real
Declare variable count, total as integer
Declare variable avg as real
BEGIN
total = 0 //initializing total to 0
FOR (count = 1 TO 10) DO
BEGIN
PRINT “Enter name”
READ names[count]
PRINT “Enter score”
READ scores[count]
total = total + score [count]
ENDFOR
END
SUMMARY
Use IF-THEN selection statement when you only have an action to perform if the
condition in true
Use IF-THEN-ELSE selection statement when you have an action to perform if the
condition in true and another action to be performed in the condition is false.
Use IF-THEN-ELSE-IF when you have multiple conditions to be tested.
Use an array to lessen the number of individual elements for an entity
Use a FOR loop to keep track of the array index
Use a FOR loop to lessen the number of instructions in your program
Use a FOR loop when you know the exact number of data to collect. For example, if you
are asked to write a program to accept the age of 15 students as opposed to being as to
write a program to accept the age of a number of students.
Initialize counter variables to zero, since it will accumulated after each loop.
The words repetition, loop and iteration are used interchangeably