Professional Documents
Culture Documents
”
“That depends a good deal on where you want to get to,” said the Cat.
̶ LEWIS CARROLL , Alice in Wonderland
CHAPTER 3
➢ A flow control statement can cause a change in the
subsequent controls of flow to differ from the natural
sequential order in which the instructions are listed.
▪ Flow control roughly categorized into 3. These are:
1. Conditional/Branching/selection statements
2. Iteration/Looping/Repetition statements
3. Jumping statements
if (Boolean_expression)
{
statement(s);
}
if-else
▪ An if-else statement decides which statements to execute
based on if the condition is true or false.
if (Boolean_expression) Flowchart
{
Syntax
statement(s)-of-true-case;
}
else
{
statement(s)-of-false-case;
}
Computer Programming 3/20/2021 8
▪ What is the output of the following code if number is 14,
15, and 30?
if (x > 2) {
if (y > 2) {
int z = x + y;
cout << "z is " << z << endl;
}
}
else
cout << "x is " << x << endl;
if (x > 2)
if (y > 2)
{
int z = x + y;
cout << "z is " << z << endl;
}
else
cout << "x is " << x << endl;
return 0;
}
Computer Programming 3/20/2021 27
• A conditional expression evaluates an expression based on
a condition.
Syntax: boolean-expression ? expression1 : expression2;
• For example, the following statement assigns 1 to y if x is
greater than 0, and -1 to y if x is less than or equal to 0.
float income;
cin >> income;
float incomeTax = income <= 600 ? 0 :
income <= 1650 ? (income * 0.1) - 60 :
income <= 3200 ? (income * 0.15) - 142.5 :
income <= 5250 ? (income * 0.2) - 302.5 :
income <= 7800 ? (income * 0.25) - 565 :
income <= 10900 ? (income * 0.3) - 955 :
(income * 0.35) - 1500 ;
return 0;
}
Computer Programming 3/20/2021 39
for
Previous statement;
Next statement;
# include <iostream>
using namespace std;
int main( )
{
float num, average, sum = 0.0;
int i, n;
jump:
average = sum / (i - 1);
cout << "\nAverage = " << average;
return 0;
}
do {
cout << "value of a: " << a << endl;
a = a + 1;
if( a > 15 )
value of a: 10
} while( a < 20 ); value of a: 11
value of a: 12
return 0; value of a: 13
} value of a: 14
value of a: 15
Computer Programming 3/20/2021 52
continue
• continue statement causes the loop to skip the rest of
its body and immediately retest its condition prior to
reiterating.
• For the for loop, continue causes the conditional test
and increment portions of the loop to execute.
• For the while and do...while loops, program control
passes to the conditional tests.
• It is an error to use the continue statement outside a
loop.
return 0;
}
Computer Programming 3/20/2021 56
break continue
The continue statement works somewhat like the break
statement. Instead of forcing termination, however, continue
forces the next iteration of the loop to take place, skipping
any code in between.