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

Control Structures

Uploaded by

Devanand Manaram
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Control Structures

Uploaded by

Devanand Manaram
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Information Technology

Form 4 Notes for Class 1 and Class 2


Wednesday, October 17, 2023
Class 1 - 8:00am to 9:00 am Class 2 - 9:00am to 10:30 am

Control Structures Statements

The steps that are identified for preparing algorithms can be written using three basic control
structures or program constructs to indicate how instructions will be carried out or how the flow of
control from one statement to another will take place.

The three basic control structure statements are:


1. Sequential control structure
2. Selection control structure
3. Iteration/Repetition/Loop structure

The Sequential Control Structure


The sequential control structure is used when you have instructions to be carried out a particular
order such as:

1. Input statements for example: (a) read price, tax


(b) Get num1, num2
(c) Accept guess

2. Output statements example: (a) Print total_cost


(b) Display average
(c) Produce ”Enter an integer value"

3. Involving arithmetic operators, such as:


(a) Sum  num1 +num2
(b) Average  sum/2
(c) Total_cost  Total_cost +Price

4. Statements involving the assignment of values variables through initialization, such as:
(a) Count  1
(b) Maximum  20
(c) Num1  0

1
The Selection Control Structure
The selection control structure is used in problems with instructions to be carried out if a certain
condition is met. The choice of options will be dependent on whether the condition it's true or false.
The control structure statements commonly used in algorithms are normally written as:

If (condition) then

< Instructions to be performed if the condition is true>

Else

< Instructions to be performed if the condition is false>

Endif

Example 2
Example 1
If (Age>=50) then
If (A>B) then Print “Old”
Display A Else
Else Print “Young”
Display B Endif
Endif

If (condition) then

< Instructions to be performed if the condition is true>

Endif
Example

If (A>B) then
Display A
Endif

2
The Nested IF statement
The If/Elseif statement allows you to create a chain of if statements. The If statements are evaluated
in order until one of the expressions is true or the end of the If/Elseif chain is reached. If the end of
the If/Elseif chain is reached without a true expression, no code blocks are executed. The Nested
control structure statements commonly used in algorithms are normally written as:

If (condition 1) then
< Instructions to be performed if the condition 1 is true>
Elseif (condition 2) then
< Instructions to be performed if the condition 2 is true>
Elseif (condition 3) then
< Instructions to be performed if the condition 3 is true>
Else
< Instructions to be performed if the condition is false>
Endif

Example
Print “Please enter today’s temperature”
Get Temp
If (Temp >45) then
Print “Wear light weight rain coat”
Elseif (Temp>20) AND (Temp<45) then
Print “Wear soft shell jacket”
Elseif (Temp>0) AND (Temp<20) then
Print “Wear down jacket”
Else
Print “Wear base layers and down jacket”
Endif

Iteration/Repetition/Loop Control Structure


Iteration/Repetition/Loop Control Structure allows statements to be repeated a fixed number of times
or until some condition evaluates to false while some condition is true. Iteration forms a repetitive
stage of the processing steps. There are two types of Iteration or Loop control structures:

1. Indefinite Loop/Unbounded Iteration


In this type of loop, you do not know in advance how many times the statements within the loop are to
be repeated.

3
Example of the While-do loop and Repeat-Until loop

While-do loop Repeat-Until Loop.


The general form of the While-do loop is The general form of the Repeat-Until loop
as follows: is as follows:

While (condition is true) do Repeat


<statements> <statements>
Endwhile Until (Condition is True)

Example
Display “Please enter Price of an item” Example
Read Price Repeat
While (price <> 0) do Display “Please enter Price of an item”
Total_Cost  Total_Cost + price Read Price
Display “Please enter Price of an item” Total_Cost  Total_Cost + price
Read Price Until (Price=0)
Endwhile Print Total_cost
Print Total_cost

2. Definite Loop/Bounded Iteration


In this type of loop, you know in advance the number of times the statement within the loop will be
repeated.
E.g. For-do Loop

The general form of the For-do loop is as follows:


:
For (<counter variable> = <Start Value> to <Terminating Value>) do

<statements>

Endfor

Example 1
For (Count = 1 to 5) do
Display “Please enter Price of an item”
Read Price
Total_Cost  Total_Cost + price
Endfor
Print Total_cost

You might also like