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

Chapter 3- Algorithms Control Structures FINAL

Uploaded by

golsiaraoul9
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)
37 views

Chapter 3- Algorithms Control Structures FINAL

Uploaded by

golsiaraoul9
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/ 14

TOPIC: ALGORITHMS [ ALGORITHM CONTROL STRUCTURES ]

Chapter 3: Algorithm Control structures


Learning objectives : after studying this lesson , the students should Be able:

 Define algorithm control structure


 Explain an Use types algorithms constructs (sequence,decision,loops)

3.1. Introduction

Algorithm control structures also known as program control structures or program constructs are
blocks of statements that determine how statement are to be executed. There are three control
structures namely:
Sequence
Selection
Iteration( loops)

3.2. Sequence

Sequence refers that instructions should be executed one after another.


The general format of a sequence is shown below.

Pseudocode version Flowchart version

Instruction 1
Begin

Instruction1 Instruction 2
Instruction 2
Instruction 3
Instruction 3
Instruction n
Instruction n
Stop

Example of sequence: the pseudo code below add two numbers ( number1 and 2) and display
their sum.
Begin
INPUT number1
INPUT number2
Sum = number1+number2
PRINT sum
STOP

Page | 1
TOPIC: ALGORITHMS [ ALGORITHM CONTROL STRUCTURES ]

3.2.1. Incrementation

Incrementation is an operation consisting of adding 1,2,3…n to the value of a variable. The


general format of incrementation is shown below:

Variable = variable + n; where n is an integer


(new) ( old)
Example1:
1. Count = 5
2. Count = count + 1
In the instruction 1, above count is assigned the value 5.
In the second instruction 2 the value of count has been incremented by 1 , so the new value of
count is 6 ( ie 5+1=6).
3.2.2. Decrementation

decrementation is an operation consisting of subtracting 1,2,3…n from the value of a variable .


The general format of incrementation is shown below:

Variable = variable - n; where n is an integer


(new) ( old)
Example2:
1. Count = 10
2. Count = count - 1
In the example , count is assigned the value 20 in initially.
In the second instruction 2 the value of count has been decremented by 1 , so the new value of
count is 9.( ie 10-1=9)

3.3. Selection/decision

This control structure is used to branch whether a condition return a value True or False
There are four types of selection control structures used in programs namely:
IF ……..THEN…….ENDIF
IF……...THEN…….ELSE……..ENDIF
Nested IF
Case Selection

3.4. IF…..THEN……ENDIF

This control structure is used if Only one Option is available. Others options are ignored .

Page | 2
TOPIC: ALGORITHMS [ ALGORITHM CONTROL STRUCTURES ]

The general format of IF…..THEN…..ENDIF is shown as follow:


Pseudocode version Flowchart version

IF ( Condition) THEN
FALSE
Statements
Condition
ENDIF

TRUE

Statements

Here , the statements are executed if the condition is true.


For example, If a school wants to Award Honour Rolls only to the students with an Average
score of 12 and Above. It will award only those students and ignored the rest.
IF ( AVERAGE >=12) THEN
PRINT “Honour Roll Awarded”
ENDIF
3.4.1. IF…..THEN……ELSE…….ENDIF
This selection control structure is used when two options are available. Here, if the condition is
true statement1 or a block of statement1 is/are executed otherwise statement2 is executed(
condition is false).
The general format of IF ………THEN……..ELSE……ENDIF

Pseudocode version Flowchart version

IF ( Condition) THEN
Statement 1 FALSE
ELSE
Condition
Statement 2
Statement 2
ENDIF TRUE
Statement 1

Page | 3
TOPIC: ALGORITHMS [ ALGORITHM CONTROL STRUCTURES ]

Example: In Football match a player is given a Red Card if he does a serious fault/mistake ,
otherwise he is given a Yellow Card.

IF ( Fault = Serious) THEN Fault = FALSE


Serious
PRINT “ Red Card”
PRINT “ Yellow Card”
TRUE
ELSE
PRINT “ Red Card”
PRINT “ Yellow Card”

ENDIF

3.4.2. Nested IF

This selection control structure is used where two or more options have to be considered to make
a decision. The general format of Nested If is as follow
Pseudocode version Flowchart version

IF ( Condition1) THEN
Statement 1
ELSE IF
Statement 2
ELSE IF ( condition2)
THEN
Statement 3
ELSE ( condition n)
THEN
Statement n
ENDIF
ENDIF
ENDIF

Page | 4
TOPIC: ALGORITHMS [ ALGORITHM CONTROL STRUCTURES ]

Note: Each IF statement and ELSE IF statement must have an ENDIF statement to terminate.

Example 16: Write pseudo code that will take marks in an exam as input and will tell the
grade A for 80 to 100, B for 70 to 79, C for 60 to 69 and F grade for 0 to 59.
Solution
WRITE “Enter Marks”
READ marks
IF marks >= 80 AND marks <= 100
THEN
WRITE “The Grade is A”
ELSE IF marks >= 70 AND marks < 80
THEN
WRITE “The Grade is B”
ELSE IF marks >= 60 AND marks < 70
THEN
WRITE “The Grade is C”
ELSE IF marks >= 0 AND marks < 60
THEN
WRITE “The Grade is F”
ELSE
WRITE “Invalid Marks”
ENDIF
ENDIF
ENDIF
ENDIF

3.5. Iteration /repetition

Problem statement:
We want to Print a rectangle of stars, on 5 lines and each line having 5 stars. The pseudo code A
can be used to achieve this.
Another way to achieve this is by using a FOR loop which requires fewer instructions (Pseudo
code B) the statement In The FOR loop “ PRINT “ ***** ” is repeated 5 times.

Page | 5
TOPIC: ALGORITHMS [ ALGORITHM CONTROL STRUCTURES ]

Pseudocode A: without loop Pseudocode B: using FOR loop Output


BEGIN
FOR ( Count = 1 TO 5) *****
1. PRINT “ *****” *****
2. PRINT “ *****” PRINT “ ***** ”
*****
3. PRINT “ *****” ENDFOR *****
4. PRINT “ *****” *****
5. PRINT “ *****” *****
END

Limitation of Pseudo code A: If we want to Display a pattern of 50 lines of stars, the Pseudo
Code A is not appropriate since we need to type in 50 instructions that will occupies a lot of
space and the program will be too long. Therefore the Program B is suitable for this task.

Iteration also known as repetion or loop. This control structure is used when a statement or a
block of statements is to executed many times based on given condition. Three categories of
loop exist.

FOR……….TO………..ENDFOR
WHILE………DO……..ENDWHILE
REPEAT………..UNTIL

3.5.1. FOR…….TO……ENDFOR.

This repetition statement is used when we know how many times an instruction or set of
instructions is to be repeated.
Count = Lower Value

The General Format.


TRUE Is Count =
FOR Count = Initial_value TO Final_Value Higher Value
Repeated Statements
FALSE
ENDFOR.
Repeated Statements

Incrementation of Count

Page | 6
TOPIC: ALGORITHMS [ ALGORITHM CONTROL STRUCTURES ]

Few things to remember about the FOR loop.

 There is a variable in FOR … TO … ENDFOR loop for controlling the number of


iterations and is known as a control variable. The name of the control variable is
usually ‘Count’.
 We specify the initial (lower) and final (higher) values of the control variable in the
opening statement of the loop. These initial and final values are not restricted to
the numerical values only, they can be variables as well.
 The Control Variable is automatically incremented by ‘1’ each time the loop ends.
 The value of the control variable is tested at the beginning of the loop & loop is repeated
until the value of control variable is less than or equal to the specified final value

Example: 4: Write pseudo code that will take 10 numbers as input and print their average by
using FOR…TO…NEXT loop. Dry run for num = 10, 20, 30, 25, 5,15,2,8,45, 40.

Solution: Count Num Total Avg Output


1 10, 0 10
BEGIN 2 20 + = 30
Total = 0 3 30 60
FOR Count = 1 TO 10 4 25 85
WRITE “Enter number” 5 5 90
READ num 6 15 105
Total =Total+num 7 2 107
ENDFOR 8 8 115
Avg =Total/10 9 45 160
10 40 200
WRITE “Average of 10 Numbers is:”, Avg.
200/ 10= 20 20
END

3.5.2. WHILE…….DO……ENDWHILE

This repetition statement is used when we don’t know how many times an instruction or set of
instructions is to be repeated.

Page | 7
TOPIC: ALGORITHMS [ ALGORITHM CONTROL STRUCTURES ]

The general format of WHILE…….DO……ENDWHILE

Pseudocode version Flowchart version

WHILE ( Condition)
DO FALSE
Statements Condition
ENDWHILE.

TRUE
Repeated Statements

Few things to remember about WHILE…DO…ENDWHILE loop are:

The Statements in the loop are repeated as long as the condition is true and halted when
the condition is false.
Condition is tested at the beginning of the loop.
The statements/instruction between DO and ENDWHILE keywords are repeated.

Example 7: Write pseudocode or draw a flowchart that will take a Password as Input and
compared it with the one stored in the computer that is “ call@237” if the password matched
then the access is granted otherwise the access is denied.

Solution

BEGIN
INPUT “ Type in your Password”
READ pswd
WHILE ( Pswd Not “ call@237”)
DO
PRINT “ password incorrect, ACCESS DENIED”
Statements to be
INPUT “ Enter a Correct Password”
repeated
READ Pswd
ENDWHILE
PRINT “ ACCESS GRANTED”
END

Page | 8
TOPIC: ALGORITHMS [ ALGORITHM CONTROL STRUCTURES ]

3.5.3. REPEAT …….……UNTIL

It is a repetition statement that is used when we don’t know how many times an instruction or
set of instructions is to be repeated. It is different from WHILE… DO … ENDWHILE because
this loop will be repeated so long as a condition is false and stop when the condition is True.
The general format

Pseudocode version Flowchart version

REPEAT
Statements
Statements
UNTIL ( Condition)

FALSE
Condition

TRUE

Note : The condition is tested at the end of the loop and even if a condition is true the
loop will execute at least once.

Example 13: Write the pseudo code that will take numbers as input, add and gives the
total as output. The loop will continue until “0” is given as input.

Solution

BEGIN
Total = 0
REPEAT
WRITE “Enter a number to add”
READ num
Total = Total + num
UNTIL num is “0”
Write Total
END

Page | 9
TOPIC: ALGORITHMS [ ALGORITHM CONTROL STRUCTURES ]

End of chapter questions

Part 1 : Selections

Exercise 1: the pseudo code compute the Profit and loss of items sold.
START
1. WRITE “Enter the cost price"
2. READ CP
3. WRITE "Enter the selling price"
4. READ SP
5. IF SP>CP THEN
6. PROFIT = SP-CP
7. WRITE "The profit is", PROFIT
8. ELSE
9. LOSS = CP-SP
10. WRITE "The loss is" , LOSS
11. ENDIF
END

1. Dry run the pseudo code of Exercise1 with the values below of inputs; and then write the
output for each case(case a and b)
a. CP = 5000, SP = 7000
b. CP = 8000 SP = 7500
2. List the variables used in this pseudo code.
3. Convert the pseudo code into a flowchart
4. What is the name of the program construct depicted from line 5 to 11.

Exercise 2: Write pseudocode that will take a number as input and tells whether a number is
positive, negative or zero.

Exercise 3: Write down pseudocode that will take three numbers as input and tells
(a) The Largest Number
(b) The Smallest Number
Exercise 4: A store charges $12 per item if you buy less than 10 items. If you buy between 10
and 99 items, the cost is $10 per item. If you buy 100 or more items, the cost is $7 per item.
Write a program that asks the user how many items they are buying and prints the total cost.

Page | 10
TOPIC: ALGORITHMS [ ALGORITHM CONTROL STRUCTURES ]

BEGIN
PRINT “ Enter the number of items bought”
READ num
IF num < 10 THEN
TotalCcost = num*12
ELSEIF num >=10 AND num <=99 THEN
TotalCost = num*10
ELSE
TotalCcost = num*7
ENDIF
ENDIF
PRINT TotalCcost
END
1. What is the value of the output when the user enters the numbers below as inputs:
i) Num = 70 ii) Num = 250 iii) 5
2. Convert the pseudo code of exercise 4 into a flowchart.

Part 2 : Sequence

Exercise 5. Consider the pseudo code below. Write down the values of Cost, Price and
SellingPrice,
SET Cost to 10
SET Price to COST times 2
Tax = price * 0.12
SellingPrice Price + Tax

Exercise 6: Write down a program in pseudo code that gets radius and central angle as user
inputs and computes the area of a sector. Given that the area of a sector is:
Area of a Sector = (π * radius * radius * central angle)/360

Exercise 7: Write a program that asks the user for a weight in kilograms and converts it to
gram. There are 1000 grams in a kilogram.

Part 2 : LOOPS

Exercise 8: Write down a pseudo code to display on the screen a rectangle of star as shown
below: Allow the user to specify the height of the rectangle.
*******************
*******************
*******************
*******************

Page | 11
TOPIC: ALGORITHMS [ ALGORITHM CONTROL STRUCTURES ]

Exercise 9: Write a program that asks the user for their name and how many times to print it.
The program should print out the user’s name the specified number of times.

Exercise 10: A geography class decided to measure daily temperature per day over a week
period (7 days). Write pseudo code that inputs the temperature for all 7 days and give
output as average temperature for the week.
BEGIN
SET Total_temp to 0
FOR Count = 1 to 7
WRITE “Please enter temperature”
READ temp
Total_temp =Total_temp + temp
ENDFOR
Avgtemp = Total_temp/7
WRITE “Average temperature for whole year is:”, Avgtemp
END.
1. Use the table below to dry run the code.
Count Temp Total_temp Avgtemp Output
1 20 0
21
21
23
22
25
24

Exercise 11: The pseudo code below Prints the Multiples of 5 from zero to a limit entered by
a user.
BEGIN
INPUT m.
Sum = 0
Count = 0
WHILE (Count <= m )
PRINT count
Count = count +5
ENDWHILE
END
2. Write down the Output for the following values of m:
a. 25
b. 50
3. Convert the pseudo code into a flowchart

Page | 12
TOPIC: ALGORITHMS [ ALGORITHM CONTROL STRUCTURES ]

Solution of exercise 2:
WRITE “Enter a number”
READ num
IF num> 0
THEN
WRITE “The number is positive”
ELSE IF num = 0
THEN
WRITE “The number is zero”
ELSE
WRITE “The number is negative”
ENDIF
ENDIF

Solution of exercise 3:
WRITE “Enter three numbers”
READ num1
READ num2
READ num3
IF num1 > num2 AND num1 > num3
THEN
WRITE “The largest Number is”, num1
ELSE IF num2 > num1 AND num2 > num3
THEN
WRITE “The largest Number is”, num2
ELSE
WRITE “The largest Number is”, num3
ENDIF
ENDIF

Page | 13
TOPIC: ALGORITHMS [ ALGORITHM CONTROL STRUCTURES ]

Pseudocode version Flowchart version

IF ( Condition1) THEN
Statement 1 TRUE
Condition 1 Statement 1
ELSE IF
Statement 2
FALSE
ELSE IF ( condition2)
THEN TRUE
Statement 3 Statement 2
Condition 2
ELSE ( condition n)
FALSE
THEN
Statement n
Statement 3
ENDIF Condition 3
TRUE
ENDIF
ENDIF
FALSE

Statement n
Condition n
TRUE

FALSE

Page | 14

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy