Chapter 3-Control StatementsI
Chapter 3-Control StatementsI
Fundamentals of Programming I
Chapter III
Control Statements
________________________________________________________________________________
3.1. Introduction
A running program spends all of its time executing statements. The order in
which statements are executed is called flow control (or control flow). Flow
control in a program is sequential, from one statement to the next, but may be
diverted to other paths by branch statements.
There are different forms of C++ statements:
Declaration statements are used for defining variables.
Assignment statements are used for simple, algebraic computations.
Branching statements are used for specifying alternate paths of execution,
depending on the outcome of a logical condition.
Loop statements are used for specifying computations, which need to be
repeated until a certain logical condition is satisfied.
Flow control statements are used to divert the execution path to another part
of the program.
}
else {
interest = balance * debitRate;
balance += interest;
}
For example:
if(n!=0){
if(score>=50)
z = x/y; {
} cout<<”U Passed!”;
else { }
else{
cout<<”Illegal Division by 0!”;Cout<<”U Failed!”;
} Cout<<”U Must Take This Course
Again!”;
3.2.2. The switch Statement }
First expression (called the switch tag) is evaluated, and the outcome is
compared to each of the numeric constants (called case labels), in the order
they appear, until a match is found. The final default case is optional and is
exercised if none of the earlier cases provide a match.
Example: Binary arithmetic operation into its three components and stored
these in variables operator, operand1, and operand2, switch 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;
}
The break terminates the switch statement by jumping to the very end of it.
For example, allowing x to be used as a multiplication operator
switch (operator) {
case 'x':
case '*': result = operand1 * operand2;
break;
default: cout << "unknown operator: " << ch << '\n';
break;
}
The above switch statement can be written as in if-else statement
if (operator == '+')
2|P a g e
Assosa University Department of Computer Science
while (n % 2 == 0 && n /= 2)
Here the loop condition provides all the necessary computation including
division by 2, so there is no real need for a body.
statement;
expression3;
}
For loop example to calculates the sum of all integers from 1 to n.
sum = 0;
for (i = 1; i <= n; ++i)
sum += i;
This is preferred to the while-loop version, i is usually called the loop variable.
Loop variable i can be defined inside the loop itself:
4|P a g e
Assosa University Department of Computer Science
Unlike the while loop, the do loop is never used in situations where it would
have a null body.
For example:
for(n=10; n>0; n--)
{
cout<<n << ", ";
if(n==3)
{
cout<< "Countdown Aborted!";
break; //drop out of the loop
}
}
7|P a g e