C++ Chapter 4 (2)
C++ Chapter 4 (2)
Chapter four
Control Structure
Depending upon requirements of a problem, it is often required to alter the normal
sequence of execution to in the program. This means that we may desire to selectively
or repetitively execute a program segment.
Control structure
expression false
true
Body of if
E.g. write a program which reads two numbers and then print greater one.
If (a>b) cout<<”greater is: “<<a;
If (b>a) cout<<”greater is: “<<b;
For example, the following code fragment prints x is 100 only if the value stored in
the x variable is indeed 100:
1 if (x == 100)
2 cout << "x is 100";
If we want more than a single statement to be executed in case that the condition is true
we can specify a block using braces { }:
1 if (x == 100)
2{
3 cout << "x is ";
4 cout << x;
5}
2
Introduction to computer & programming
2. if else statement – the expression is evaluated first if that is true then the if
body is executed otherwise else body is executed. the syntax of if else statement is
as follow:
if (expression)
body of if
else
body of else
Body of if and body of else may be simple or block. If body of if and else is compound
then the structure looks like:
if (expression)
{ statement1;
Statement2;
.
.
Statement n; }
else
{ statement 1;
Statement2;
.
.
Statement n;
}
express false
ion
true
Body of if Body of else
3
Introduction to computer & programming
E.g. write a program which reads a number and then prints the message for that number
(that is even or odd).
If (n%2= =0)
cout<< “the number is even”;
else
cout<<”the number is odd”;
The following example also shows its use telling if the value currently stored in x is
positive, negative or none of them (i.e. zero):
1 if (x > 0)
2 cout << "x is positive";
3 else if (x < 0)
4 cout << "x is negative";
5 else
6 cout << "x is 0";
Remember that in case that we want more than a single statement to be executed, we
must group them in a block by enclosing them in braces { }.
#include<iostream.h>
#include<conio.h>
void main ( )
{ float a,b,c;
cout<<”Enter three number: “;
cin>>a>>b>>c;
if (a>b)
{
if (a>c)
Cout<< “largest value : “<<a<<endl;
else
Cout<<”largest value: “<<c<<endl;
}
else
{
if (b>c)
Cout<<”largest value : “<<b<<endl;
else
Cout<<”largest value :”<<c<<endl:
}
4
Introduction to computer & programming
4. Switch Statement
The switch or switch-case statement is an alternate of nested if-else structure. This is a
multi-way decision maker that tests whether an expression matches one of the numbers of
values. The syntax of switch statement is as follow:
switch(expression)
{
case value 1:
Statement 1;
Statement 2; first
. case
. body
Statement n;
break;
case value 2:
statement 1;
statement 2; second
. case
. body
Statement n;
break;
case value n:
statement 1;
statement 2; nth
. case
. body
Statement n;
Break;
default:
statement 1;
this is optional we statement 2; default
can skip this also . body
.
Statement n;
}
This expression must be integer or character type. That means the value 1, value 2,…,
value n all are also integer or character type. In switch statement; switch, case, break and
default are reserve word. Switch first calculate the expression then match that expression
with value 1 if both are equal then first case body is executed and switch is terminated
because of break statement, which is written at last of every body. If we skip the break
statement then after executing first body switch is not terminated this match with next
value and so on. If expression is not equal to value 1, then expression checked with value
2 if equal execute second case body and switch is terminated.
5
Introduction to computer & programming
If expression is not equal to value 2 then expression checked with value 3 if there
is no match process continue to last value.
If expression is not match with any value from value 1 to value n then default
body is executed. The diagram for execution of switch statement is:
false
If expression true
equal to value 2 second case body
executed
false
false
default body
executed
Initialization
expression
Test false
expression
true
Body of loop
executed
Assignment
7
Introduction to computer & programming
2. while loop
The while loop allows programs to repeat a statement or series of statements, over and
over, as long as a certain test condition is true. The format is:
while(test condition)
{
block of code;
}
The test condition must be enclosed in parentheses. The block of code is called the
body of the loop and is enclosed in braces and indented for readability. (The braces are
not required if the body is composed of only ONE statement.) Semi-colons follow the
statements within the block only.
When a program encounters a while loop, the test condition is evaluated first. If the
condition is TRUE, the program executes the body of the loop. The program then
returns to the test condition and reevaluates. If the condition is still TRUE, the body
executes again. This cycle of testing and execution continues until the test condition
evaluates to 0 or FALSE. If you want the loop to eventually terminate, something within
the body of the loop must affect the test condition. Otherwise, a disastrous INFINITE
LOOP is the result! The while loop is an entry-condition loop. If the test condition is
FALSE to begin with, the program never executes the body of the loop. The diagram of a
while loop is as follow:
expression false
true
Body of loop
The following program fragment prints and counts the characters of a string array:
apstring name="Corky";
i = 0; //begin with the first cell of array
while (i< name.length( )) // loop test condition
{
cout<< name[ i ] << endl; //print each character
i++; //increment counter by 1
}
8
Introduction to computer & programming
cout << "There are " << i << " characters.\n";
\*This last cout prints when the test condition is false and the loop terminates*\
3. do-while loop
In a while loop, the condition is evaluated at the beginning of the loop. If the condition is
false when the loop is entered, the loop body will not be executed at all. But in do-while
loop first the body of loop is executed then the condition is evaluated. In do-while loop
the body of loop always executes at least one time if the condition is false also.
The syntax of do-while loop is:
do
body of loop;
while (condition);
If body of do-while loop consists multiple statements then that look like:
do
{
Statement 1;
Statement 2;
.
The diagram of do-while loop execution
. is as follow:
Statement n;
}
while (condition);
Body of loop
expres false
sion
true
9
Introduction to computer & programming
void main()
{ int n, sum=0,i=1;
cin>>n;
do {
sum=sum + 1;
I = I + 2;
}
while (I <=n);
cout<<sum;
}
C++ provides three different type of breaking control statements. With the help of
breaking control statement the control structure power is improved.
1. Break statement
Break statement causes an exit from a loop just as it does from a switch statement. Break
terminates the loop in which that is written. We can use break in any loop. The break
statement terminates the execution of the nearest enclosing loop or conditional statement
in which it appears. Control passes to the statement that follows the terminated statement,
if any. break is used with the conditional switch statement and with the do, for, and
while loop statements. In a switch statement, break causes the program to execute the
next statement after the switch. Without a break statement, every statement from the
matched case label to the end of the switch, including the default, is executed.
In loops, break terminates execution of the nearest enclosing do, for, or while statement.
Control passes to the statement that follows the terminated statement, if any.
Within nested statements, the break statement terminates only the do, for, switch, or
while statement that immediately encloses it. You can use a return or goto statement to
transfer control from within more deeply nested structures.
e.g. for(i=0;i<=100;i++)
{ statement 1;
Statement 2;
break;
Statement 3;
}
Statement 4;
In the above example statement 3 is not executed because before that there is a break
statement.
2. Continue Statement
The continue statement is used for inverse operation of the break statement. The continue
statement transfers control to start of loop. The syntax of continue statement is similar to
break. Syntax of break break;
10
Introduction to computer & programming
Syntax of continue continue; both are reserve words.
E.g. while(condition)
{ statement 1;
Statement 2;
In the above example if a>b is true the control is transferred to start means again
if(a>
condition is checked and statement is executed. The arrow shows the flow of control.
3. go to statement
By go to statement we can transfer the control to some other part. Means by go to we can
change the execution sequence of program. The general syntax of go to is:
b)
goto label;
go to is reserve word. Label is a name given by the user or programmer but it must be a
valid identifier. E.g. goto start;
goto A23;
conti
but goto 23A; is invalid because 23A is not valid identifier.
Example1. statement 1;
statement 2;
start: statement 3;
nue; statement 4;
goto start;
example2. statement 1;
statement 2;
B: statement 3;
Statement 4;
if ( a > b ) goto B;
State
ment
3;
}
11