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

Control Structures

The document discusses various programming language constructs including sequence, selection, and repetition structures. It provides examples of if-then and if-then-else statements, as well as linear and non-linear nested if statements. It also discusses for loops, while loops, and repeat until loops. Examples are given for each type of loop to illustrate how they can be used. The document aims to explain different programming language constructs through examples and help students understand how to use them in pseudocode.

Uploaded by

DeJeanae Lewis
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Control Structures

The document discusses various programming language constructs including sequence, selection, and repetition structures. It provides examples of if-then and if-then-else statements, as well as linear and non-linear nested if statements. It also discusses for loops, while loops, and repeat until loops. Examples are given for each type of loop to illustrate how they can be used. The document aims to explain different programming language constructs through examples and help students understand how to use them in pseudocode.

Uploaded by

DeJeanae Lewis
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

PROBLEM SOLVING WITH COMPUTERS

Programming Language Constructs

Immaculate Conception High


CAPE Com. Sci. Unit 1
Mr Allen
Objectives
By the end of the lesson the students should be
able to:
• Use Linear and Non-Linear Nested Ifs in Pseudocodes
• Use For loops in Pseudocodes
• Differentiate between Bounded and Unbounded loops
in pseudocode
• Utilize Unbounded and Bounded loops in
pseudocodes
• Choose appropriate Relational Operators to create
pseudocodes
Relational Operators

Operator Meaning

> Greater Than

< Less Than

= Equal to

<= Less than or equal to

>= Greater than or equal to

<> Not equal to


What are programming language
Constructs?
The phrase 'language construct' just means the
combination of keywords, grammar and structure
of a coding language. Some reading materials use
the phrase control structures to refer to a few
language constructs such as Sequence, Selection
and Repition structures. Other language constructs
include assignmnet statements, input and output
statements
Sequence
This structure is one in which a set of instructions
is each executed once in the order in which they
are written.
Selection
With this structure a question is asked and
depending on the answer one or more courses of
action are taken and then the program will move to
the next event and the statement will be executed.

IF Then structure: IF -Then-Else structure:


IF condition THEN
IF condition THEN sequence1
sequence1 ELSE
END IF sequence2
END IF
If-Then-Else Example

IF HoursWorked > RegularHrs THEN


WRITE “overtime worked”
ELSE
WRITE “Not enough hours for over time”
ENDIF
If with null Else statement
This is a variation of the simple IF (IF-Then-Else)
structure. This is used only when a task is to be
performed if a condition is true.

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.

Country Code Country Winnings


1 Jamaica winnings = (0.5 * bet) + bet
2 Guyana winnings = (0.35 * bet) + bet
3 Cayman winnings = (0.5 * bet) + 1000
Example: Cascading IF
The pseudocode snippet below shows how the winnings for a betting program
is calculated based on country status.

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

A Non-Linear nested IF statement is used when


multiple condition within a program structure
need to be tested before a instruction(s) can be
executed. It is called Non-Linear as the Else
statement can be separated from the IF
Example Problem: Non-Linear nested IF

Write a pseudocode snippet that checks students’


status and gender, then counts separately the
amount of female part-time, male part-time and
full time students.
Non-Linear Nested IF
A non-Linear nested if statement is used when a number of
different conditions need to be satisfied before a particular action
can take place.

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

N.B n denotes any positive integer


FOR Loop Example

Algorithm: GCT Calculator


START
DECLARE cnt AS INTEGER
DECLARE purchaseAmt AS REAL
purchaseAMT0

FOR cnt  0 TO 9 STEP 1


WRITE “Please enter Purchase Amount”
READ purchaseAmt
gct  purchaseAmt * 16.5/100
WRITE “The GCT is: ” gct
END FOR
STOP
While& Repeat Until Loop Syntax
WHILE REPEAT UNTIL

WHILE condition DO REPEAT


sequence sequence
END DO Until condition

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
cnt0
WHILE cnt <= 10 DO
WRITE “Enter a bet greater than 500”
READ bet
cntcnt + 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
cntcnt + 1
UNTIL cnt > 9

STOP
Unbounded while loop: DO WHILE
START
DECLARE bet as Integer
DECLARE winnings as REAL
bet0
winnigs0

WRITE “Enter -1 to end the program”


WRITE “Please enter your bet”
READ bet

WHILE bet <> -1 DO


winnigs(bet * 0.8) + bet
WRITE “Possible Winnigs: ” winnings
WRITE “Please enter your bet”
READ bet
END WHILE

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 ($)

< = 2.5 kg $3.50 per kg

2.6 – 5 kg $2.85 per kg

>5 kg $2.45 per kg


Amusement Park Ride Selector
Write a pseudocode that will accept or deny
students for an amusement park. In order for
students to be accepted for the trip they have to be
at least 152 cm tall otherwise output “too short for
the trip”. For the students who are tall enough
check if the permission slip is signed, if it is output
“board the bus” otherwise output “no slip no trip”.

You might also like