Control Structures
Control Structures
Operator Meaning
= Equal to
Example:
IF student_stat = ‘P’ THEN
part_time_count part_time_count + 1
END IF
Cascading/Linear Nested IF
The Linear nested IF is a variation of the IF
structure that is used when a program is being
tested for various values and a different action is
to be taken for each value.
Cascading/Linear Nested IF
Structure 1
IF condition1 THEN
sequence1
ELSE
IF condition2THEN
sequence2
ELSE
IF condition3 THEN
sequence3
END IF
END IF
ENDIF
Cascading/Linear Nested IF
Structure 2
IF condition1 THEN
sequence1
ELSE IF condition2THEN
sequence2
ELSE IF condition3 THEN
sequence3
END IF
END IF
ENDIF
Example Problem: Cascading IF
Write a program snippet that calculates the bet for
the following countries as follows using an
appropriate control structure.
IF country_code = 1 THEN
winnings (0.5 * bet) + bet
ELSE
IF country_code = 2 THEN
winnings(0.35 * bet) + bet
ELSE
IF country_code = 3 THEN
winnings 1000 + (0.5 * bet)
END IF
END IF
ENDIF
Non-Linear nested IF
Example
IF student_stat = ‘P’ THEN
IF student_gender = ‘F’ THEN
fem_pTime fem_pTime + 1
ELSE
male_pTime male_pTime + 1
END IF
ELSE
full_time full_time + 1
END IF
Repetition
This is a structure in which a group of statements is
executed repeatedly. There are a few types of repetition
structures:
1. For Loop
2. While Loop (Bounded & Unbounded)
a) While
b) Repeat Until
N.B. Bounded loops denote the number of times the instructions within the
loop are executed while unbounded loops allow the user to terminate the
loop which means instructions are executed as many times as the user
wishes.
For Loop
A for-loop is a control structure for specifying iteration,
which allows code to be executed repeatedly. The typical
For loops are examples of bounded loops meaning the
programmer has to specify the amount of time the loop will
run
Syntax
FOR counter 0 TO n Increment
sequence
END FOR
The WHILE loop will iterate The REPEAT UNTIL loop will
until the condition becomes iterate until the condition
false. becomes true.
Bounded While Loop: WHILE
START
DECLARE bet AS REAL
DECLARE cnt AS INTEGER
cnt0
WHILE cnt <= 10 DO
WRITE “Enter a bet greater than 500”
READ bet
cntcnt + 1
END WHILE
STOP
Bounded Loop: REPEAT UNTIL
Algorithm: Winnings Calculator
START
DECLARE bet, cnt AS INTEGER
DECLARE winnings AS REAL
bet 0
cnt 0
winnings 0
REPEAT
WRITE “Enter a bet”
READ bet
winnings bet + (0.75 * bet)
WRITE “The possible winnings is: ”, winnings
cntcnt + 1
UNTIL cnt > 9
STOP
Unbounded while loop: DO WHILE
START
DECLARE bet as Integer
DECLARE winnings as REAL
bet0
winnigs0
N.B This kind of loop uses what is called a sentinel value to terminate the loop. In this case -1
Complete the problems
Use only control structures of a similar nature
covered in this presentation
Car rental promotion
A car rental firm rents its cars for $JMD5500 per
day. If a car is rented for more than seven days
and returned in good condition the customer gets
back 7% of the rental. Write a pseudocode to solve
the problem.
Highest Mark and Average
Write a pseudocode that reads the marks of some
students and is terminated by the value 999. The
program should find and print the average and the
highest mark.
Process Customer Record
A pseudocode is required to read a customers name,
a purchase amount and a tax code. The tax codes
have been validated and will be one of the following:
0 Tax exempt (0%)
1 State sales tax only (3%)
2 Federal and state sales tax (5%)
3 Special sales tax (7%)
The program must then compute the sales tax and
the total amount due and print the customers name,
purchase amount, sales tax and total amount due.
Parcel delivery charge calculator
Design a pseudocode using the Linear nested if
Structure that will receive the weight of a parcel
and determine the delivery charge for that
parcel. Calculate the charges as follows:
Pacel Weight in (Kg) Cost per Kg ($)