Chapter Three
Chapter Three
3.1 Introduction
A running program spends all of its time executing instructions or statements in that program.
The order in which statements in a program are executed is called flow of that program.
Programmers can control which instruction to be executed in a program, which is called flow
control. This term reflects the fact that the currently executing statement has the control of the
CPU, which when completed will be handed over (flow) to another statement. Flow control in a
program is typically sequential, from one statement to the next. But we can also have
execution that might be divided to other paths by branching statements. Or perform a block of
statement repeatedly until a condition fails by Repetition or looping. Flow control is an
important concept in programming because it will give all the power to the programmer to
decide what to do to execute during a run and what is not, therefore, affecting the overall
outcome of the program.
int main()
bool flag=false;
flag = true;
Example:-
if (n%d) cout << "n is not a multiple of d"; // (n%d) is a Boolean expression.
The output statement will execute precisely when n%d is not zero, and that happens precisely
when d does not divide n evenly, because n%d is the remainder from the integer division.
Print “Passed”
determines whether the condition "student ' s grade is greater than or equal to 60" is true or f a l s
e . If the condition is true, then "Passed" is printed and the next pseudocode statement in order i s
"performed" (remember that pseudocode is not a real programming language). If the condition is
false, the print statement is ignored and the next pseudocode statement in order i s performed.
Note that the second line of this selection structure is indented. Such indentation is optional, but
it is highly recommended because it emphasizes the inherent structure of structured program s.
When you convert your pseudocode into C++ code, the C++ compiler ignores whitespace
characters (like blanks, tabs and newlines) used for indentation and vertical spacing.
if ( grade > = 6 0 )
if ( grade > = 6 0 )
cout « " Passed " ;
else
cout « "Failed" ;
Note that the body of the else is also indented. Whatever indentation convention you choose
should be applied consistently throughout your programs. It is difficult to read programs that do
not obey uniform spacing conventions.
C++ provides the conditional operator ( ? : ), which is closely related to the if/else structure. The
conditional operator is C++'s only ternary operator-it takes three operands.
The above if/else statement can be replaced with the following line of code:-
cout « ( grade >= 60 ? " Passed " : " Failed " ) ;
Switch
Case
Default
Break, Where Default and Break are Optional.
The General Syntax might be:
First expression (called the switch tag) is evaluated, and the outcome is compared to each of
the constants (called case labels), in the order they appear, until a match is found. The
statements following the matching case are then executed. Note the plural: each case may be
followed by zero or more statements (not just one statement). Execution continues until either a
break statement is encountered or all intervening statements until the end of the switch
statement are executed. The final default case is optional and is exercised if none of the earlier
cases provide a match.
For example, suppose we have parsed a binary arithmetic operation into its three components
and stored these in variables operator, operand1, and operand2. The following switch statement
performs the operation and stores the result in result.
switch (operator) {
case '+': result = operand1 + operand2;
break;
case '-': result = operand1 - operand2;
break;
case '*': result = operand1 * operand2;
break;
case '/': result = operand1 / operand2;
break;
default: cout << "unknown operator: " << ch << '\n';
break;
}
As illustrated by this example, it is usually necessary to include a break statement at the end of
each case. The break terminates the switch statement by jumping to the very end of it. There
are, however, situations in which it makes sense to have a case without a break. For example, if
we extend the above statement to also allow x to be used as a multiplication operator, we will
have:
switch (operator) {
case '+': result = operand1 + operand2;
break;
case '-': result = operand1 - operand2;
break;
case 'x':
case '*': result = operand1 * operand2;
break;
case '/': result = operand1 / operand2;
break;
default: cout << "unknown operator: " << ch << '\n';
break;
}
Because case 'x' has no break statement (in fact no statement at all!), when this case is satisfied,
execution proceeds to the statements of the next case and the multiplication is performed.
It should be obvious that any switch statement can also be written as multiple if-else
statements. The above statement, for example, may be written as:
if (operator == '+')
result = operand1 + operand2;
else if (operator == '-')
result = operand1 - operand2;
else if (operator == 'x' || operator == '*')
result = operand1 * operand2;
else if (operator == '/')
result = operand1 / operand2;
else
cout << "unknown operator: " << ch << '\n';
However, the switch version is arguably neater in this case. In general, preference should be
given to the switch version when possible. The if-else approach should be reserved for
situation where a switch cannot do the job