Programming Lesson Notes 3, Homework 3 Corrected Used of Equal Test and Indentation
Programming Lesson Notes 3, Homework 3 Corrected Used of Equal Test and Indentation
INTRODUCTION TO PROGRAMMING
LESSON 3
Introduction to Programming
Preface: Develop Psuedocode Algorithm to solve simple mathematical problems, involving the
use of Control Statements (IF-Then, IF-Then-ELSE)
In a program/psuedocode, a computer will execute instructions in the order (sequence) they are
written. This done where the first is executed, followed by the second and them by a third until
statements in the order written are executed.
Sometimes, certain statements could be skipped and thus the successes of some statements are
dependent on a statement before.
Conditional Statements/Structures
The (IF Statements is used to test a condition)
We are all familiar with conditions one way or another. If we are hungry, we need something to
eat and if we are thirsty, we need something to drink. Or, if the rain falls, we need to take our umbrella or
if the rain does not fall, we do not take our umbrella.
Possible Algorithms:
ALG. 1 IF Rain is falling
THEN take umbrella
ENDIF
In the first Algorithm, it tells the user what to do IF the rain falls and leaves the decision to the
user IF the rain does not fall.
In the second Algorithm, it is quite specific because it tells the user what to do if the rain falls
(take an umbrella) and what not to do if the rain is not falling (do not take an umbrella).
The main purpose of conditional statements is to help the computer to decide what to do if the
more than one option is available based on a particular condition. This statement would help the
computer to decide which statement to carry out if the condition is favourable and which statement should
be carried out if the rain does not fall (which is unfavourable).
1
These statements are carried out based on the TRUTH or FALSITY of a condition that is being
used to decide or being tested.
EXAMPLE Write Psuedocode Algorithm to print the value of NUMB1 if it is greater than that of
NUMB2.
NOTE: The choice to print NUMB1 is dependent if NUMB1 is greater than NUMB2!
The condition therefore is IF NUMB1 > NUMB2. If this is true, NUMB1 should be
printed.
Task 1 Write a Psuedocode Algorithm to print the larger of two variables A and B.
NB In this case, it does not matter if A = B, in such the computer does not matter which number is
printed first. The conditional structure would therefore be IF A > B.
Task 2 Write a Psuedocode to read in the value of two numbers A and B and subtract the smaller
number from the larger number. Print the difference.
Psuedocode Read A, B
IF A = B THEN
Print „The numbers are the same‟
ELSE
IF A > B THEN
Difference A – B
ELSE
Difference B – A
ENDIF
ENDIF
END
2
Task 3 Write a Psuedocode to read in variables A, B and C and sum all numbers and print the
largest of the 3 numbers.
Read A, B, C
Sum:= A + B + C
IF A = B THEN
Print „The numbers are the same‟
ELSE
IF A = C THEN
Print „The numbers are the same‟
ELSE
IF B = C THEN
Print „The numbers are the same‟
ELSE
IF A= B = C THEN
Print „The numbers are the same‟
ELSE
ENDIF
ENDIF
ENDIF
ENDIF
ALTERNATIVE USING LOGICAL OPERATIONS
SITUATION 1
IF A > B THEN IF A = B OR IF A = C OR IF B = C THEN
Largest A ELSE Print „The numbers are the same‟
Largest B ELSE
ENDIF IF A = B = C THEN
IF C > Largest THEN Print „The numbers are the same‟
Largest C ELSE ENDIF
Largest Largest ENDIF
ENDIF Situation 2
Print Largest
END IF A = B OR IF A = C OR IF B = C OR IF A = B = C THEN
Print „The numbers are the same‟
ENDIF
3
Task 4 Write a psuedocode algorithm to read an integer value for mark and print the appropriate
grade based on the following table:
4
INFORMATION TECHNOLOGY
INTRODUCTION TO PROGRAMMING
LESSON 3 HOMEWORK
Introduction to Programming
1. Write a psuedocode algorithm to print the value of SUM1 if it is less than SUM2. (5 marks)
2. Write a psuedocode algorithm to print the smaller of the contents of variables A and B. (5 marks)
3. Write a psuedocode to read in variables Q, P and S and print the smallest number. (5 marks)
4. Write an algorithm that prompts the user to input a number. If the number is greater than 45, the
algorithm should prompt the user to enter three additional numbers and find their sum otherwise,
the psuedocode should end. (5 marks)
5. Write a psuedocode to read the values for A, B and C as variables. If the value for A is negative,
the psuedocode should print the sum of the values for variables B and C otherwise, the
psuedocode should print the average of the 3 variables. (5 marks)
6. Write a psuedocode to read a value (between 1 – 5) into a variable of your choice. If the value of
the variable entered is 1, the psuedocode should prompt the user to enter their name. If the value
entered is 2, the algorithm should prompt the user to enter their school name. If the value entered
is 3, the psuedocode should prompt the user to enter their favorite color; otherwise the program
should terminate. (5 marks)
TOTAL MARKS 30