0% found this document useful (0 votes)
11 views41 pages

08 PRG1X1 - Decision Constructs & Loops

The document provides an overview of decision constructs and loops in programming, defining key concepts such as decision constructs, repetition, and their types. It details various decision control structures including if statements, if-else statements, and switch-case statements, as well as different types of loops such as for, while, and do-while loops. Additionally, it explains the use of break and continue statements within loops to control execution flow.

Uploaded by

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

08 PRG1X1 - Decision Constructs & Loops

The document provides an overview of decision constructs and loops in programming, defining key concepts such as decision constructs, repetition, and their types. It details various decision control structures including if statements, if-else statements, and switch-case statements, as well as different types of loops such as for, while, and do-while loops. Additionally, it explains the use of break and continue statements within loops to control execution flow.

Uploaded by

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

Decision Constructs and Loops

Objectives

– Define Decision Constructs

– Types of Decision Constructs

– Define Repetition

– Types of Repetition
Decision Constructs
– A decision Construct, also knows as selection control structure, allows a
program to make a decision and change its behavior based on that decision.

– The decision is made based on the outcome of a logical test.

– A logical test is a calculation whose outcome is either true or false.

If true:

If false:
Decision Constructs
– Decision Constructs can be represented in
algorithms as follows:

Decision to be made on a
question. Answer will either
be true/false, or Yes/No
Types of Decision Constructs
– We will look at 4 types of decision control structures:

a) if statement

b) if…else Statement

c) if…else…if statement

d) switch…case statement
a) if statement

 An if statement consists of a
boolean expression which is
evaluated to a boolean value.

 If the value is true then if block is


executed otherwise next
statement(s) would be executed.
if statement
 Example:
BEGIN
SET a = 5, b = 2;
SET result
result = a / b;

IF result == 2 THEN
PRINT "Result is 2“
END IF
IF result == 3 THEN
PRINT "Result is 3“
END IF
END
b) if…else statement

 An if-else statement consists of two


statements – if statement and else
statement.

 When the expression in an if-statement is


evaluated to true then if block is
executed otherwise the else block would
be executed.
If…else statement
 Example:
BEGIN
SET a = 6, b = 8;
SET result
result = a - b;

IF result > 0 THEN


PRINT “Result is greater than zero.”
END IF
ELSE
PRINT "Result is lesser than zero“
END ELSE
END
b) if…else…if statement
 The If-Else-If ladder is a set of statements that is used to test a series of conditions.

 If the first if statement meet the result then code within the if block executes.

 If not, control passes to the else statement, which contains a second "if" statement.

 If second one meet the result then code within the if block executes.

 This continues as a series of else if statements.

 A default else code block may execute when no condition has been evaluated to true.
b) if…else…if statement
 Flowchart:
If…else statement
 Example:
BEGIN
SET a = 6, b = 8;
SET result
result = a - b;

IF result > 0 THEN


PRINT “Result is greater than zero.”
END IF
ELSE
PRINT "Result is lesser than zero“
END ELSE
END
Switch…Case statement
 Use the switch statement to select one of many
code blocks to be executed.

 This is how it works:

a) The switch expression is evaluated once


b) The value of the expression is compared with the
values of each case
c) If there is a match, the associated block of code is
executed
d) The break and default keywords will be described
later in this chapter
Switch…Case statement
User types in a value for day
BEGIN
SET day = INPUT
switch (day) Switch takes value in “day” and
case 1: passes to each case. Whatever
PRINT "Monday" case matches with day is
break
case 2: activated to print the day in text
PRINT "Tuesday"
break
case 3:
PRINT "Wednesday" Break statement terminates the
break
case 4:
case, and program execution
PRINT "Thursday" ends
break
case 5:
PRINT "Friday"
break
END
The break Keyword
– When the program reaches a break keyword, it breaks out of the switch block.

– This will stop the execution of more code and case testing inside the block.

– When a match is found, and the job is done, it's time for a break.

– There is no need for more testing.

– A break can save a lot of execution time because it "ignores" the execution of
all the rest of the code in the switch block.
Repetition
– Presentation of a set of instruction to be performed repeatedly, as long as the condition
is true.

– A control structure is a block of programming that helps to analyze and


decide program flow based on the given parameters.

– It is mostly used in a programming language that decides the direction


of the program to make the right decision.

– Block statement is executed again and again until a terminating condition occurs

– Also known as a loop, iteration


Types of loops/Iteration

1. Counter Loops
a. For
b. While
c. Do…While

2. Sentinel/Stop-value Based loops


a. Break
b. Continue
a) Counter Loops
– Counter controlled loops are where we know the number of loop
executions beforehand.
– These use a counter to repeat a group of statements a KNOWN number of
times.

There two types:

1. Incrementing (1,2,3 …./2,4,6…/ 5,10,15)


2. Decrementing (5,4,3 …/6,4,2…/15,10,5)
For Loop
– Uses the FOR keyword to loop and always repeats as long as the
condition is still true .
Explanation

Incrementing 1. Starting at 0 || counter = 0 (Declaration


and initialisation)
FOR counter = 0 To 10
Execute statement 2. Counting up to 10 || counter < 10
END
3. Counting by increasing by 1 each time
|| counter++
For Loop
– Uses the FOR keyword to loop and always repeats as long as the
condition is still true .
Explanation

Decrementing 1. Starting at 0 || counter = 0 (Declaration


and initialisation)
FOR counter = 10 To 0
Execute statement 2. Counting up to 10 || counter < 10
END
3. Counting by decreasing by 1 each time
|| counter--
For Pseudocode Example
Create an algorithm using pseudocode for a program that prints numbes
from 1 to 10
Declaring variable counter
BEGIN 1. Assign value 1 to counter
2. Add 1 to counter after each loop
SET counter 3. Loop until counter == 10
FOR counter = 1 TO 10 STEP 1 DO
OUTPUT counter
END FOR Output the value of counter before
looping
END Exit only when counter > 10
For Flowchart Example
Create an algorithm using
flowchart of a program that prints
numbers from 1 to 10:
For IPO Example
Create an algorithm using pseudocode of a program that prints numbers from 1 to 10

Input Processing Output


1. Set counter counter
2. counter = 1
3. Check if counter <=10
4. Display counter
5. counter++
The While Loop
– It is used to repeat a specific block of code an unknown number of
times, until a condition is met.
Explanation

Decrementing 1. Starting at 10 || counter = 10

WHILE counter = 10 To 0 DO 2. Counting Down to 0 || counter = 0


Execute statement
END 3. Counting by decreasing by 1 each
time || counter--
The While Loop
Create an algorithm using pseudocode for a program that prints numbers from 10 to 0

Declaring variable counter

Checking condition: is counter is


BEGIN
between 10 and 0
SET counter
WHILE counter = 10 TO 0 DO
1. If condition is true
PRINT counter
2. then print value of counter
END WHILE
3. decrement counter by 1
END
Exit only when counter == 0
While loop Flowchart Example

Create an algorithm using


flowchart of a program that prints
numbers from 10 to 0:
For IPO Example
Create an algorithm using pseudocode of a program that prints numbers from 1 to 10

Input Processing Output


1. Set counter counter
2. counter = 10
3. Check counter 0 <= counter <=10
4. Display counter
5. Counter--
The Do…While Loop
– The do/while loop is a variant of the while loop.
– This loop will execute the code block once, before checking if the condition is true, then
it will repeat the loop as long as the condition is true.

Explanation
Decrementing
1. Execute statement
DO
Execute statement 2. Starting at 10 || counter = 10 and counting Down
WHILE counter = 10 To 1 to 0 || counter > 0
END
3. Decrease by 1 each time || counter--
The Do…While Loop
Create an algorithm using pseudocode for a program that prints numbers from 10 to 1

Declaring variable counter and assigning it value


10
BEGIN Print value of counter
SET counter = 10
DO
1. If condition is true
PRINT counter
2. decrement counter by 1
WHILE counter = 10 TO 1
END
Exit only when counter == 0
Do…While loop Flowchart Example

Create an algorithm using


flowchart of a program that prints
numbers from 10 to 1:
For IPO Example
Create an algorithm using pseudocode of a program that prints numbers from 1 to 10

Input Processing Output


1. Set counter = 10 counter
2. Display counter
3. Check if 0 < counter <=10
4. counter--
Infinite Loop (Never Ending Loop)

Example:
BEGIN
SET num
DISPLAY “Enter number to display”
num = INPUT
FOR counter = 5 TO counter < 0
DISPLAY num
counter--
END FOR
END
Break (Stop Value)
– We have already seen the break statement used in an earlier when looking at
switch…case.

– It was used to "jump out" of a switch statement.

– The break statement can also be used to jump out of a loop.

– WE normally are required to exit a loop completely when an external condition


triggers.
Break (Stop Value)
– Flowchart syntax of break:
Break (Stop Value)
– Pseudocode example of break:
Declaring variable counter
BEGIN
SET counter Looping counter from 1 until 10
FOR counter = 1 TO 10
IF counter == 4
While looping, if Output
break
counter == 4, exit loop
END IF
PRINT counter
END FOR
END While looping, if
counter != 4, print
value in counter
Continue (Skips value)
– The continue statement breaks one iteration (in the loop), if a specified
condition occurs, and continues with the next iteration in the loop.

– It is used to “skip out" of a loop when a condition is met, and continues after.

– WE normally are required to skip a statement in a loop completely when an


external condition triggers.
Continue (Skip value)
– Flowchart syntax of continue:
Continue (Skip value)
– Pseudocode example of break:
Declaring variable counter
BEGIN
SET counter Looping counter from 1 until 10 Output
FOR counter = 1 TO 10
IF counter == 4 1. While looping, if counter == 4
continue 2. skip statement, continue after
END IF skip and loop again
PRINT counter
END FOR
END
Print only when counter != 4
Example:
BEGIN
SET counter
WHILE counter = 0 TO 10
IF counter == 4
counter++
Create an algorithm using pseudocode of a continue Output
program that loops through and prints values 0 END IF
until 10. The conditions are that the value 4 must
DISPLAY counter
be jumped, and the program should only print
counter++
until 7 the value.
IF i == 8
break
END IF
END WHILE
END
Exercises
– Lets go on Teams

– Attempt Decision Constructs & Loops Day 8 Exercises.pdf


The End

You might also like