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

Handout If and Loop Week 5

how to use the if and loop

Uploaded by

wes51
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Handout If and Loop Week 5

how to use the if and loop

Uploaded by

wes51
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

INFORMATION TECHNOLOGY

PROBLEM SOLVING AND COMPUTING


COTROL STRUCTURES
SELECTION STATEMENTS
IF-THEN and IF-THEN-ELSE

LESSON OBJECTIVES

 select and use appropriate selection statements to solve problems


 represent selection statements using pseudocode and flowchart
 use selection statements to determine the highest and lowest value in a set of values
 declare array variables
 implement arrays using FOR loop

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 = 100, b = 500, c = 400

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

IF (NUM2 > largest) THEN


largest = NUM2
ENDIF

IF (NUM3 > largest) THEN


largest = NUM3
ENDIF

IF (NUM4 > largest ) THEN


largest = NUM4
ENDIF

PRINT largest, “is the largest value”

END //end of program

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

VARIABLE INITIALIZATION / ASSIGNMENT

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.

ARRAY DATA STRUCTURE

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.

Declaring a variable array


Syntax:
Declare arrayname: array[startvalue two dots endvalue] of data type

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

The structure of the FOR construct


The FOR construct consist of:
1. The word FOR
2. The Loop variable
3. The Equal sign
4. An initial (start) value
5. The word TO
6. The final (END) value
7. The word DO
8. One or more statements to be executed each time through the loop
9. The word ENDFOR indicating the end of the construct.
NOTE: The part of the construct between the words FOR and DO is called the control part. This is what determines
how many times the loop will be executed. The loopvariable is also called the control variable.

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

The one on the


right or the one on
the left?

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.

DON’T FORGET YOUR FOR LOOP


READING VALUES INTO AN ARRAY STRUCTURE

 You must use a FOR loop when you use an array.


Input values in an array is similar to inputting values in a single variable. Each location in array is called
an index or position.
For example, when you create an array to store 10 scores which are integers, you are telling the memory
make space for 10 values, which will be beside each other. A logical view of the memory will the array
would be
Score: array [1..10] of integer
Index 1 2 3 4 5 6 7 8 9 10
Element 50 90 78 89 67 57 89 90 12 56

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.

PRINTING VALUES STORED IN AN ARRAY


Syntax: PRINT arrayname [index] //the loop control variable is usually the index

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

// printing the list


PRINT “The list is:”
FOR (count = 1 TO 10) DO
PRINT names[count], “ ……….” , scores[count]
ENDFOR

END

COUNTING

Did you know?


Counting involves increasing the value by a fixed
amount repeatedly. The counting is done by the You can keep track of a process
computer and not by the user. each time a set of instruction in
repeated?
A counter instruction performs a count. The instruction
is written as
For example:
counter_variable = counter_variable + 1
 counting the number of
persons in a class
 counting the number of
The counter variable must be given an initial value
person who fails
before the counter instruction is carried out so as to  calculating the sum of scores
facilitate calculation. The initial value is 0 because we  calculating the average scor
start counting from 1.
Variable names for counters should be chosen to reflect what is being counted. So if you were
asked to count the number of students’ name entered in the program, you would use
student_counter as a variable.
If you were asked to calculate the sum of scores entered for 20 students, you could use
total = 0
total = total + score;
Let’s look at how you calculate a sum of 20 scores in a program

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

PRINT “The total is:”, total

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

You might also like