You are on page 1of 25

Chapter 4

Loops and Decisions (control structures)

A program is usually not limited to a linear sequence of instructions. During its process it may
bifurcate, repeat code or take decisions. For that purpose, C++ provides control structures that
serve to specify what has to be done by our program, when and under which circumstances.

Flow of control through any given function is implemented with three basic types of control
structures:

1. Sequential: default mode, Sequential execution of code statements (one line after
another).
2. Selection: used for decisions, branching -- choosing between 2 or more alternative paths.
In C++, these are the types of selection statements:
 If
 If…else
 else…if
 switch
3. Repetition: used for looping, i.e. repeating a piece of code multiple times in a row. In C+
+, there are three types of loops:
 while
 do/while
 for

The statement parts are the "bodies" of the control structures. The statement after control
structures must be either:

 an empty statement
;
 a single statement Expression;
 A compound statement (i.e. a block). Can enclose multiple code statements. Remember, a
compound statement is enclosed in set braces { }

Introduction to computing Page 1


{
Statement 1;
Statement 2;
.
.
.
Statement n;
}

1. Decision Making (Selection or branching )


 Decision making structures require that the programmer specify one or more conditions
to be evaluated or tested by the program, along with a statement or statements to be
executed if the condition is determined to be true, and optionally, other statements to be
executed if the condition is determined to be false.
 Typical decision making structure found in most of the programming languages:

 C++ programming language provides following types of decision making statements. i.


if statement

An if statement consists of a Boolean expression followed by one or more statements.

Introduction to computing Page 2


 If the boolean expression evaluates to true, then the block of code inside the if
statement will be executed. If boolean expression evaluates to false, then the first set of
code after the end of the if statement (after the closing curly brace) will be executed.  It
should be noted that the output of a expression is either True (represented by anything
different from zero) or False (represented by Zero ).

ii. i f…else statement

An if statement can be followed by an optional else statement, which executes when the boolean
expression is false.

Introduction to computing Page 3


 If the boolean expression evaluates to true, then the if block of code will be
execu ted, otherwise else block of code will be executed.

Introduction to computing Page 4


The last two examples will do the same thing except expression in the Eg3 is changed from
relational to arithmetic. We know that the result from the modulus operator is either 0 or one if
the divider is 2. Thus the final result of the expression in Eg3 will either be 1 for odd numbers or
be 0 for even numbers. iii. The else if...else Statement:

An if statement can be followed by an optional else if...else statement, which is very useful
to test various conditions using single if...else if statement.

Introduction to computing Page 5


iv. Switch statement :

Introduction to computing Page 6


A switch statement allows a variable to be tested for equality against a list of values. Each value
is called a case, and the variable being switched on is checked for each case.

 You can have any number of case statements within a switch. Each case is
followed by the value to be compared to and a colon.
 The constant-expression for a case must be the same data type as the variable in the
switch, and it must be a constant or a literal.
 When the variable being switched on is equal to a case, the statements following that case
will execute until a break statement is reached.
 When a break statement is reached, the switch terminates, and the flow of control
jumps to the next line following the switch statement.
 A switch statement can have an optional default case, which must appear at the end
of the switch. The default case can be used for performing a task when none of the
cases is true. No break is needed in the default case.

Introduction to computing Page 7


Introduction to computing Page 8
E.g.: Receives grade as A,B,C,….and print out the status of that grade select from d/t grades using
switch statement.

2. Iteration or Looping
 There may be a situation, when you need to execute a block of code several number of times.
 Repetition statements control a block of code to be executed repeatedly for a fixed number of
times or until a certain condition fails.
 General from of a loop statement in most of the programming languages:

Introduction to computing wsu.ece.by_amanuel.T_2018 Page 9


i. The while loop
 The while statement (also called while loop) provides a way of repeating a statement
or a block as long as a condition holds / is true.  The general form of the while loop
is:

 Here, statement(s) may be a single statement or a block of statements. The condition


may be any expression, and true is any nonzero value. The loop iterates while the
condition is true.
 When the condition becomes false, program control passes to the line immediately
following the loop.

Introduction to computing wsu.ece.by_amanuel.T_2018 Page 10


Introduction to computing wsu.ece.by_amanuel.T_2018 Page 11
E.g.3: prints the numbers from 10 to 19 in a new line.

O uput:

Introduction to computing wsu.ece.by_amanuel.T_2018 Page 12


ii. Do…while loop.
 First statement is executed and then expression is evaluated. If the outcome of the
expression is nonzero, then the whole process is repeated. Otherwise the loop is
terminated.
 In do…while loop, we are sure that the body of the loop will be executed at least once.
Before the condition is tested.
 The general form is:

Introduction to computing wsu.ece.by_amanuel.T_2018 Page 13


iii. f or loop
 A for loop is a repetition control structure that allows you to efficiently write a loop
that needs to execute a specific number of times.  Syntax:

 Here is the flow of control in a for loop:


 The initialization step is executed first, and only once. This step allows you to declare
and initialize any loop control variables. You are not required to put a statement here, as
long as a semicolon appears.
 Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is false,
the body of the loop does not execute and flow of control jumps to the next statement just
after the for loop.
 After the body of the for loop executes, the flow of control jumps back up to the
increment statement. This statement allows you to update any loop control variable.
 The condition is now evaluated again. If it is true, the loop executes and the process
repeats itself (body of loop, then increment step, and then again condition). After the
condition becomes false, the for loop terminates.

Introduction to computing wsu.ece.by_amanuel.T_2018 Page 14


Introduction to computing wsu.ece.by_amanuel.T_2018 Page 15
Common problem that beginner may encounter when writing loops.

i. Infinite loop: no matter what you do with the while loop (and other repetition
statements), make sure that the loop will eventually terminates.

ii. Off-By-One Bugs (OBOB): another thing for which you have to watch out in writing
a loop is the so called Off-By-One Bugs or errors. Suppose we want to execute the
loop body 10 times. Does the following code works?

Introduction to computing wsu.ece.by_amanuel.T_2018 Page 16


N ested loops
A loop can be nested inside of another loop. C++ allows at least 256 levels of nesting. Syntax:

Introduction to computing wsu.ece.by_amanuel.T_2018 Page 17


Loop Control Statements: Loop control statements change execution from its normal
sequence.

Introduction to computing wsu.ece.by_amanuel.T_2018 Page 18


Introduction to computing wsu.ece.by_amanuel.T_2018 Page 19
 A goto statement provides an unconditional jump from the goto to a labeled statement in the
same function.

Examples on loops and selection stmt.

Example 1: Program to print half pyramid using *

Introduction to computing wsu.ece.by_amanuel.T_2018 Page 20


Example 2: Program to print half pyramid a using numbers

Introduction to computing wsu.ece.by_amanuel.T_2018 Page 21


Example 3: Inverted half pyramid using *

Introduction to computing wsu.ece.by_amanuel.T_2018 Page 22


Example 4: Program to print full pyramid using *

Introduction to computing wsu.ece.by_amanuel.T_2018 Page 23


Example 5: Print Floyd's Triangle.

Introduction to computing wsu.ece.by_amanuel.T_2018 Page 24


Introduction to computing wsu.ece.by_amanuel.T_2018 Page 25

You might also like