Notes Chapter-10

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

1

LESSON – 10
CONTROLE STATEMENTS

 Introduction
 Control statements provide more flexibility and control over the execution of a program.
Control statements either allow the user to decide on the execution of a certain set of
statements based on a requirement or condition or allow the user to repeat the execution of
a certain set of statements again and again until certain requirement or condition is
satisfied. Control statements are classified as three types
a. Selection statements
b. Iterative statements and
c. Jump statements

a. Selection statements
Selection statements allow us to choose a set-of-instructions for execution
depending upon an expressions truth value, which indicates the requirement or
condition. Selection statements are also called conditional statements or decision
statements. C++ provides five types of selection statements

i. if statement
The if statement is the simplest form of selection statement. It is very
frequently used in decision making and altering the flow of program execution.
Syntax: if(Expression)
Statement – 1;
Statement – 2;
Example Max = a;
if (max < b)
max = b;
cout << “Largest = “ << max;

True
Is
Expression?

Statements 1
False

Statements 2
2

The expression should always be enclosed in parentheses. The expression is


evaluated first. If the result is TRUE, the Statement 1 is executed. If it is FALSE,
the Statement 1 is ignored and the flow of execution continues with the next
Statement 2. The statement can be a single or compound statement.

ii. if-else statement


The if - else construct is used to carry out a logical test and then take one of the
two possible courses of actions depending on the outcome of the test. If the
expression is evaluated as TRUE, Statement-1 is executed else is Statement-2
executed. Both the statements Statement-1 and Statement-2 can be simple or
compound
Syntax: if(Expression)
Statement – 1;
else
Statement – 2;
Example if (a < b)
cout << “A is the largest”;
else
cout << “B is the Largest “;

False True
Is
Expression?

Statements 2 Statements 1

Next Statements

iii. nested if statement and


It is possible to have one if-else statement embedded in an other if-else
statement. Such a statement is called a nested if statement.
3

Syntax: if(Expression-1)

if(Expression-2)
Statement – 1; Nested if
else
Statement – 2;

else
if(Expression-3)
Statement – 3;
else
Statement – 4;
Example if (A > B)
{
if (A > C)
Max = A;
Else
Max = C;
}
else
{
if (B > C)
Max = B;
Else
Max = C;
}

False
True
Is Expression-1
?

Statement-3 True
False
Is Expression -2
?

Statement-2 Statement-1

Next Statements
4

iv. else-if ladder


By using one if-else construct, it is possible to choose between two choices.
There may be a situation wherein it is required to select one among several
choices. This can be accomplished by another if-else in the else part of the
construct. This type of if construct is called else if ladder statement.

Syntax: if(Expression-1)
Statement – 1;
else if (Expression-2)
Statement – 2;
else if (Expression-3)
Statement – 3;
Next statement;
Example if (Percentage >= 80)
cout << “Distinction”;
else if(Percentage >= 60)
cout << “First Class”;
else if(Percentage >= 50)
cout << “Second Class”;
else if(Percentage >= 35)
cout << “Pass Class”;
else
cout << “Fail”;

v. switch statement (Mutliway Decisions)


The switch statement allows a program to select one statement for execution
out of a set of alternatives. During the execution of the switch statement, only
one of the possible statements will be executed; the remaining statements will
be skipped.
Syntax: switch (Expression)
{
case label-1 : Statement-1;
break;
case label-2 : Statement-2;
break;
default : default block;
}
Next statement;
Example switch (ch)
{
case ‘A’ :
case ‘a’ :
case ‘E’ :
5

case ‘e’ :
case ‘I’ :
case ‘i’ :
case ‘O’ :
case ‘o’ :
case ‘U’ :
case ‘u’ : cout << ch << “is a vowel”;
break;
default : cout << ch << “is not a vowel”;
}

The switch statement executes according to the following rules :


 The expression of the switch statement is first evaluated to a single
value.
 This value is then matched with the various case labels one at a time.
 When the value of the expression is matched against a case label, the
statements in that block are executed until either a break statement is
found or the end of the switch structure is reached.
 If the value of the expression does not match any of the case labels, the
statements following the default is executed.
 A break statement causes an immediate exit from the switch structure.
 A label is an integer i.e. it can be take either an integer value or single
character value.

b. Iterative statements
An iteration is a program construct that causes a statement to be executed again and
again. The process of repeating the execution of a certain set of statements again
and again is termed as repetition or looping. The types of loops are
(1) while statement (2) do-while statement (3) for statement
The while and do-while loops are called as indefinite control structure, as the
number of iteration or repetition of the loop body may vary depending on the value
of an arbitrary Boolean expression. The for loop is a definite control structure where
the number repetitions is known in advance.
i. while statement
This structure is also called as the “pre-tested” looping statement. This loop
repeats a statement or a block (group of statements) is executed again and
again until the test condition is TRUE. When the condition becomes FALSE, the
control passes to the line of code immediately following the loop.
6

False
Is Test
Condition
?

True Nest Statement

Body of the loop

Syntax: while (Test – Condition)


{
………….
Body of the loop
…………….
}
Next statement;
Example int Count = 1;
while (Count <= 5)
{
cout << setw(6) << Count;
Count ++;
}

ii. do-while statement


This loop is also called as the “post – tested” looping statement. Whenever
one is sure of the test condition, then the do-while loop can be used as it enters
into the loop at least once and then checks whether the given condition is TRUE
is FALSE. The set of statements in the loop is executed again and again until the
test condition is TRUE. If the test condition becomes FALSE control is
transferred out of the loop.
7

Body of the loop

False
Is Test
Condition
?

True
Nest Statement

Syntax: do
{
………….
Body of the loop
…………….
} while (Test – Condition);
Next statement;
Example int Sum = 0;
int Count = 1;
do
{
Sum = Sum + Count;
Count ++;
} while (Count <= 50)

Difference between while and do while loop

While Do while
This is pre- tested looping structure This is post tested looping structure
It tests the given condition at initial point It tests the given condition at the last of
of looping structure looping structure.
Minimum execution of loop is zero Minimum execution of loop is once.
Syntax Syntax
while (Test – Condition) do
{ {
…………. ………….
Body of the loop Body of the loop
……………. …………….
} } while (Test – Condition);
Next statement; Next statement;
Semi colon is not used. Semi colon is used.
8

iii. for statement


for statement is also called as the “fixed execution” looping structure. This
loop is normally used when we know exactly how many times a particular set of
statements is to be repeated again and again. The for statement is a looping
control structure which will execute a set of statements a specified number of
times and automatically keep track of the number of ‘passes’ through the set of
statements.

Initialize Expression-1

False
Is
Expression-2
?

True
Nest Statement
Body of the loop

Change of initial condition

i.e. Expression-3

Syntax: for (Expression-1; Expression-2; Expression-3)


{
Body of the loop
}
Next statement

Where
1) Expression-1 represents the initialization expression.
2) Expression-2 represents the expression for the final
condition.
3) Expression-3 represents the increment or decrement
expression.
Example int Sum = 0;
int Count;
for (Count = 1; Count <=10; Count ++)
9

{
Sum = Sum + Count;
}

c. Jump statements
The jump statements unconditionally transfer program control within a program.
C++ provides three types of jump statements, goto, break and continue. We can use
goto statement anywhere in the program whereas break and continue are used
inside the loops.
i. goto statement
Structured programming advocates avoiding the arbitrary transfer of control
provided by the goto statement. The goto statement is a simple statement,
used to transfer control from one point in a program to any other point in
that program. Where loop is lable and can appear either before or after goto.
This statement provides an unconditional jump to the statement indicated by
the label. No declaration is required for the label. Label requires a colon (:)
after the label.
Example int Count;
Loop : cout << Count << setw(5);
Count ++;
if (Count <= 10)
goto Loop;

ii. break statement


It can be used within a while, do-while or for statement. When break is
encountered inside any loop, control automatically passes to the first
statement after the loop. This provides a convenient way to terminate the
loop if an error or other irregular condition is encountered. The break
statement is used in
 To terminate a statement sequence in switch statement
 To exit a loop

iii. continue statement


In some programming situations we want to take the control back to the
beginning of the loop, bypassing the statements inside the loop, which have
not yet been executed. When the keyword continue is encountered inside
any loop, control automatically passes to the beginning of the loop.

*****

You might also like