Fundamentals of Programming: Chapter 3: Control Structure
Fundamentals of Programming: Chapter 3: Control Structure
2021
To 2
Selection Structure
3
One-way Selection If
5
Two-way Selection If
7
Multi-way Selection
multi-way selection: T
1. if…else if F
2. switch statement.
8
if…else if Statement
Syntax: Expression1 is evaluated. If it is
if (expression1) non zero, statement1(simple or
simple statement or block statement1 block) is executed otherwise
else if (expression2) statement1 is skipped and
simple statement or block statement2 expression2 is evaluated and if
it is non zero , statement2 is
else if (expression3)
executed otherwise statement2
simple statement or block statement3 is skipped and so on. If none of
. the expression is evaluated to
. non zero the statementLast
else if(expressionN) under the “else” is executed.
simple statement or block statementN Only the statement under one of
the condition or under the “else”
else will be executed.
simple statement or block statemtLast The ELSE part is optional.
9
Example
10
Switch Statement
A switch statement is an alternative to the if-else chain for cases that involve
comparing an integer expression with a specific value. It uses four keyword:
switch, case, default and break. It has this general form:
switch ( integer expression)
First expression (called the switch tag) is
{ evaluated, and the outcome is compared to each
case constant1: of the numeric constants (called case labels), in
group of statements 1; the order they appear, until a match is found.
break; The statements following the matching case are
case constant2: then executed. Note the plural: each case may be
followed by zero or more statements (not just
group of statements 2;
one statement). Execution continues until either
break; a break statement is encountered or all
. intervening statements until the end of the
. switch statement are executed. The final default
case constantN: case is optional and is exercised if none of the
group of statements N; earlier cases provide a match.
Unlike the multi-way if, the switch can not be
break;
used for range value. The expression must
default: evaluates to integer and the value is compared
default group of statements with each of the constant for equality.
} 11
Example
Write a program that takes the type of vehicle (‘c’ or ‘C’ for car, ‘b’
or ‘B’ for bus and ‘t’ or ‘T’ for truck) and the hour the vehicle spent
in the parking lot and display the parking charge based on the rate
below:
Car $2 per hour
Bus$3 per hour
Truck $5 per hour
12
Nested if
An if-else statement can contain any valid C++ simple or compound
statements, including another if-else statement. Therefore, one or more
if-else statements can be included in either part of an if-else statement.
Including one or more if statements inside an existing if statement is
called a nested if statement. if (gender == 'M')
if (expression1) if (age < 21 )
if (expression2) policyRate = 0.05;
statement 1 else
else if (expression3) policyRate = 0.035;
else if (gender == 'F')
statement 2
if (age < 21 )
else policyRate = 0.04;
statement 3 else
else policyRate = 0.03;
statement 4
13
Example
14
Repetition- Concept
15
Pre-test and Post-test Loop
If the loop limit test(the test condition) is
checked before the loop starts, it is called
pre-test loop. ? T Actions
In each iteration, the loop control statement
is tested. If it is true. The loop actions are F
executed; otherwise, the loop is terminated. Pre-test loop
Initialization
Actions
T Update
? Actions Update
F T
?
Pre-test Loop 17
F Post-test Loop
Event-controlled and Counter-controlled Loop
The loop limit test expression can be two types: event-controlled and
counter-controlled loop.
In an event-controlled loop, an event changes the loop control
expression from true to false. The number of iteration is not known
beforehand.
If the number of iteration is known beforehand, it is called counter-
controlled loop.
T Count T Increment
? Actions Update <n Actions
counter
F F
18
Loops in C++
21
Example
22
The for Loop
A for statement is a pre-test loop that uses three expression. The
first expression contains any initialization statements, the second
contains the loop limit test, and the third contains the updating
expression. The general syntax for the while loop is:
for (initializations ; loop limit test ; updatings)
simple or block statement
• The initializations are done only once at the first entry to the
loop.
• The loop limit test is executed before every iteration.
• The updatings are executed at the end of each iteration.
Using the comma operator you can initialize and update more than
one variable.
The initializations can be made out of the for structure and the
updating can be made inside the body of the loop. But the
semicolon must be there.
23
Example
24
Example
25
The do…while Loop
The do…while loop is the post-test loop. The general
syntax for the do…while loop is:
do
simple or block statement
while (loop limit test);
do and while are keyword.
Note that unlike the while and for statement, the do…
while loop concluded with a semicolon.
The loop statements are executed at least once. It is
commonly used to validate data entry.
26
Example
27
Example
28
Example
29
Nested Loop
30
Example
31
Example
32
Example
33
Example
34
Unconditional Statement
35
The break Statement
A break statement may appear inside a loop (while, do…while, or
for) or a switch statement. It causes a jump out of these
constructs, and hence terminates them. After the break statement
executes, the program continues to execute with the first
statement after the structure.
A break statement only applies to the loop or switch immediately
enclosing it.
It is an error to use the break statement outside a loop or a switch.
Though simple and therefore preferred, using break may lead
difficult to read code.
Rewriting the loop without a break statement is always possible
by using an additional logical variable and adding it to the loop
condition
36
Example
37
The Continue Statement
38
Example
39
The goto Statement
41
The exit Function