Professional Documents
Culture Documents
“Would you tell me, please, which way I ought to go from here?”
“That depends a good deal on where you want to get to,” said the Cat.
̶ LEWIS CARROLL , Alice in Wonderland
CHAPTER 3
Control of Flow
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 (Boolean_expression) Flowchart
{
Syntax
statement(s)-of-true-case;
}
else
{
statement(s)-of-false-case;
}
Computer Programming 12/20/2022 8
Class Work
What is the output of the following code if number is 14,
if (i > k)
{
if (j > k)
cout << "i and j are greater than k" << endl;
}
else
cout << "i is less than or equal to k" << endl;
if (x > 2) {
if (y > 2) {
int z = x + y;
cout << "z is " << z << endl;
}
}
else
cout << "x is " << x << endl;
Computer Programming 12/20/2022 17
Home Work
Suppose x = 2 and y = 3. Show the output, if any, of the
following code. What is the output if x = 3 and y = 2?
What is the output if x = 3 and y = 3?
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 12/20/2022 27
1.5. The Conditional Expressions
• 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 12/20/2022 39
2.3. The for loop
Previous statement;
Next statement;
• The code goto is used for moving back and forth in the
program. Therefore, for using goto statement one
needs to put in a label.
• Syntax:
# 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;
}
51
Computer Programming 12/20/2022
Exercise: Show the output
#include <iostream>
using namespace std;
int main(){
int a = 10; // Local variable declaration
do {
cout << "value of a: " << a << endl;
a = a + 1;
if( a > 15 ) break;
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 12/20/2022 52
3.3. The continue statement
• 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 12/20/2022 56
break vs. 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.