Program that we tried so far are executed in an orderly
manner i.e. the statements are executed one after another without getting
repeated or ignored. Certain tasks require execution of some statements
ignoring the rest. These can be accomplish by using control statements.
Conditional control statements
These statements are executed when one or more conditions
is/are satisfied. C++ supports many such statements. These statements are
listed below with a brief description.
if:- The statement following the if statement
is executed when the condition given is true.
if(condition)
s1;
s2;
When
the condition is true statement s1 and then s2 is executed. If the condition is
false only s2 is executed. If we want to execute more
than 1 statements when condition is true then we should write all those
statements within braces {} after if.
if(condition)
{
s1;
s2;
}
s3;
when the condition is true
statement s1, s2 and then s3 is executed. If the condition is false only s3 is
executed.
if-else:- Also known as either or. This
statement is used to select one statement and ignore the other statements.
if(condition)
s1;
else
s2;
when
the condition is true statement s1 is executed. If the condition is false s2 is
executed. Thus one of the statements, either s1 or s2 is always executed.
if-else if-else:- It is a branching statement
which can choose and execute on of the statements available depending upon the
condition.
if(condition1)
s1;
else
if(condition2)
s2;
else
s3;
when
the condition1 is true statement s1 is executed and rest are ignored. When
condition1 is false condition2 is verified. If condition2 is true statement s2
is executed and other statements are ignored and so on. Thus only one statement
is executed from the top, depending upon the condition. The statement following
else is executed when all the conditions are false. However
else clause is optional.
while:- It is a repeated structure statement
which repeats a statement given as long as the condition is true.
while(condition)
statement1;
statement1
is executed till the condition is true.
do while:- Like while statement it repeats a
statement given as long as the condition is satisfied unlike in while statement
the condition is checked at the end of the structure.
do
{
statement1;
}
while(condition);
statement1 is executed till the
condition is true.
for:-
It is a repeated structure which can repeat a statement as long as the given
condition is satisfied.
for(statement1;
condition; statement2)
statement3;
where
statement1 initialises control variable, condition is used to check whether
statement written after loop can be repeated or not , statement2 is used to
modify the control variable. Statement3 is a simple statement (having only one
statement) or compound statement (a set of statements written within braces {} )
which can be repeated as long as condition is true or satisfied.
gotoxy(x,y):- This function moves the cursor
to the xth column of the yth row i.e. it fixes the
displaying position at xth column of yth row. Its header
file is <conio.h>
switch:-
It is a multi branch statement which can be used to select and execute one of
the available statements.
switch(value)
{
case
1: statement 1; break;
case
2: statement 2; break;
case
n: statement n; break;
default:
statement d;
}
Where value can be a variable of
type numeric or character. The case label 1 to n can also be written with
constant identifiers.
When
the value assigned matches with case label 1 statement 1 is executed. The break
statement written after statement 1 transfers the control out of the switch
statement. When the value doesn’t match with case label 1 then it checks with
case label 2 and so on. When the value assigned doesn’t match with any of the
case labels (1 to n) then the default clause is considered and the statement d
is executed.
Default clause is optional like else clause in if-else-if-else
statement.
Unconditional control statements
These statements are executed without satisfying any
condition.
goto:-
This statement transfers control from one part of the program to the other
part.
------
------
goto
abc;
------
------
abc:
------
------
where
abc is a label which need not be declared.
When the control reaches goto
statement then the execution is transferred from goto statement to the
statement preceded by the label. Note that the symbol colon
(:) is used between the label and the statement.
break:- This statement can be used to
terminate a repeated structure (loops) such as while, do while and for and multi
branching statements like switch.
while(condition)
{
------
------
break;
------
}
When
the control encounters the statement break then the execution of while loop is
terminated.
continue:- Like the statement break continue
can be used in repeated structure. However this statements repeats the loop for
next value (next iteration) i.e. the statement continue transfers the control
back to the beginning of the loop by ignoring a set of statements written after
it.
while(condition)
{
------
------
continue;
------
}
When
the control encounters the statement continue then the control is transferred
back to while loop.
No comments:
Post a Comment