0% found this document useful (0 votes)
19 views

Chapter 3

Uploaded by

Adugna Negero
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Chapter 3

Uploaded by

Adugna Negero
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

WUGC, CNCS, Computer Programming (Comp1033) for LAM-I, Sem.

II, 2017 Chapter 3

Chapter Three

Control Statements

Flow control in a program is typically sequential, from one statement to the next, but may be
diverted to other paths by branch statements. There are 3 types of flow control statements: i)
Conditional Statements, ii) Loop Statements and iii) Branching Statements.

3.1. Conditional Statements

3.1.1. The if Statement


This makes the execution of a statement dependent upon a condition being satisfied. Its general
syntax is as follows:
if (expression)
statement;
First, expression is evaluated. If the outcome is nonzero (true), then statement is executed;
otherwise, nothing happens.
For example, when dividing two values, we may want to check that the denominator is nonzero:
if (count != 0)
average = sum / count;
To make multiple statements dependent on the same condition, we can use a compound statement:
if (balance > 0) {
interest = balance * creditRate;
balance += interest;
}
3.1.2. The if-else Statement
This allows two alternative statements: one which is executed if a condition is satisfied and one
which is executed if the condition is not satisfied. Its general syntax is as follows:
if (expression)
statement1;
else
statement2;

-1-
WUGC, CNCS, Computer Programming (Comp1033) for LAM-I, Sem. II, 2017 Chapter 3

First, expression is evaluated. If the outcome is nonzero (true), then statement1 is executed;
otherwise, statement2 is executed.
For example
if (balance > 0) {
interest = balance * creditRate;
balance += interest;
} else {
interest = balance * debitRate;
balance += interest;
}
Given the similarity between the two alternative parts, the whole statement can be simplified to:
if (balance > 0)
interest = balance * creditRate;
else
interest = balance * debitRate;
balance += interest;

3.1.3. The switch Statement


This provides a way of choosing between a set of alternatives, based on the value of an
expression. Its general syntax is as follows:
switch (expression) {
case constant1:
statements;
...
case constantn:
statements;
default:
statements;
}
First, expression (i.e. switch tag) is evaluated, and the outcome is compared to each of the
numeric constants (i.e. labels), in the order they appear, until a match is found. The statements

-2-
WUGC, CNCS, Computer Programming (Comp1033) for LAM-I, Sem. II, 2017 Chapter 3

following the matching case are then executed. 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
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;
}

3.2. Loop Statements

3.2.1. The while Loop Statement


This provides a way of repeating a statement while a condition holds. Its general syntax is as
follows:
while (expression)
statement;
First, expression is evaluated. If the outcome is nonzero, then statement is executed and the whole
process is repeated; otherwise, the loop is terminated.
For example, suppose we wish to calculate the sum of all numbers from 1 to some integer denoted
by n. This can be expressed as:
i = 1;
sum = 0;
while (i <= n) {
sum += i;

-3-
WUGC, CNCS, Computer Programming (Comp1033) for LAM-I, Sem. II, 2017 Chapter 3

i++;
}
Let n set to 5. Then, we can show a trace of the loop by listing the values of the variables involved
and the loop condition as follows:
Iteration i n i <= n sum += i++
First 1 5 1 1
Second 2 5 1 3
Third 3 5 1 6
Fourth 4 5 1 10
Fifth 5 5 1 15
Sixth 6 5 0

3.2.2. The do…while Loop Statement


This is similar to the while statement, except that its body is executed first and then the loop
condition is examined. Its general syntax is as follows:
do
statement;
while (expression);
First, statement is executed and then expression is evaluated. If the outcome of the latter is
nonzero, then the whole process is repeated; otherwise, the loop is terminated.
For example, suppose we wish to repeatedly read an integer value and print its square, and stop
when the value is zero:
do {
cin >> n;
cout << n * n << '\n';
} while (n != 0);

3.2.3. The for Loop Statement


This is also similar to the while statement, but has two additional components: an expression which
is evaluated only once before everything else, and an expression which is evaluated once at the
end of each iteration. Its general syntax is as follows:

-4-
WUGC, CNCS, Computer Programming (Comp1033) for LAM-I, Sem. II, 2017 Chapter 3

for (expression1; expression2; expression3)


statement;
First, expression1 is evaluated. Each time round the loop, expression2 is evaluated. If the outcome
is nonzero, then statement is executed and expression3 is evaluated; otherwise, the loop is
terminated.
For example, suppose we wish to calculate the sum of all numbers from 1 to some integer denoted
by n. This can be expressed as:
sum=0;
for(i=1;i<=n;i++)
sum+=i;
for loop statement can be nested as follows:
for(expression1){
for(expression2)
statement;
}
For example, to display the following output, we can use nested for loop as follows:
(1,1) (1,2) (1,3) (2,1) (2,2) (2,3) (3,1) (3,2) (3,3)
for(i=1;i<=3;i++){
for(j=1;j<=3;j++){
cout<<”(”<<i<<”,”<<j<<”)”<<” “; }}

3.3. Branching Statements

3.3.1. The continue Statement


It terminates the current iteration of a loop and instead jumps to the next iteration. It applies to the
loop immediately enclosing the continue statement. It is an error to use the continue statement
outside a loop.
For example, a loop which repeatedly reads in a number, processes it but ignores negative
numbers, and terminates when the number is zero, may be expressed as follows:
do {
cin >> num;

-5-
WUGC, CNCS, Computer Programming (Comp1033) for LAM-I, Sem. II, 2017 Chapter 3

if (num < 0) continue;


// process num here...
} while (num != 0);

3.3.2. The break Statement


Like the continue statement, a break statement only applies to the loop or switch immediately
enclosing it. It causes a jump out of these constructs, and hence terminates them. It is an error to
use the break statement outside a loop or a switch.
For example, suppose we wish to read in n integer numbers form the keyboard and find the
reciprocal each number until the user enters 0 that makes the calculation undefined:
int num;
for (i = 0; i < n; i++) {
cin>>num;
if(num==0)
break;
num=1/num;
cout << num<<endl;
}

-6-

You might also like