Professional Documents
Culture Documents
Notes Chapter-10
Notes Chapter-10
Notes Chapter-10
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
False True
Is
Expression?
Statements 2 Statements 1
Next Statements
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
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”;
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”;
}
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
?
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)
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
Initialize Expression-1
False
Is
Expression-2
?
True
Nest Statement
Body of the loop
i.e. Expression-3
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;
*****