Professional Documents
Culture Documents
IISem-Problem Solving Using C - UNIT-2
IISem-Problem Solving Using C - UNIT-2
CONTROL STATEMENTS:
2) if…..else statement
The if .. else statement is an extension of the simple if
statement. test expression
Syn:
if(test expression)
{
statement block 1;
statement block statement block
} 1 2
else
{
statement block 2; statement x
}
statement x;
In the above syntax, first the text expression is evaluated. If the expression is true, statement
block 1 is executed and statement block 2 is skipped. Otherwise, if the test expression is false,
statement block 2 is executed and statement block 1 is ignored. In both cases the control is will pass
to statement x. Therefore, statement x is executed in every case.
3. Nested if .. else statement
When a series of decisions are involved, we may have to use more than one if … statements in
nested form.
Syntax: if(expression-1)
{
if(expression-2)
{
statement-1;
}
else
{
statement-2;
}
}
else
{
if(expression-3)
{
statement-3;
}
else
{
statement-4;
}
}
The control comes to the expression-2, if the value of expression-1 is true. If expression-2 is
true then the statement-1 is executed, otherwise statement-2 is executed. If the expression-1 is false
then the expression-3 is executed. If the expression-3 is true then statement-3 is executed otherwise
statement-4 is executed.
2
There is another way of putting ifs together when multipath decisions are involved. A
multipath decision is a chain of ifs in which the statement associated with each else is an if.
Syntax : if(condition-1)
Statement-1;
else if(condition-2)
statement-2;
else if(condition-3)
statement-3;
else if(condition-n)
statement-n;
else
default-statement;
statement-x;
This construct is known as the else if ladder. The conditions are evaluated from the top of
the ladder, downwards. As soon as a true condition is found, the statement associated with it is
executed and the control is transferred to the statement-x. When all the n conditions become false,
then the final else containing the default-statement will be executed.
switch case
A switch case statement is multi-way decision statement. The general form of a switch
statement is shown below.
The switch case statement compares the value of the expression given in the switch statement
with the value of each case statement that follows. When the value of the switch and the case
statement matches, the statement block of that particular case is executed.
Default is also a case that is executed when the value of the expression does not match with
any of the case statement, i.e., the default case is executed when there is no match found between the
value of switch and case statements and thus there are no statements to be executed.
In the syntax of switch case, the keyword break is used at the end of each case because if it
were not used, then all the cases from the one met will be executed.
4
printf(“%d “,n);
n++;
}
}
THE DO STATEMENT:
The do..while loop, sometimes simply referred to as the do loop in C, differs from the while
loop.
In while statement, it checks a test condition before the loop is execute. Therefore, the body
of the loop may not be executed at all if the condition is not satisfied at the very first attempt. On
some occasion it might be necessary to execute the body of the loop before the test condition is
performed. Such situations can be handled with the help of do while statement.
Syntax:
Initialization;
do
{
body of the loop;
}while(test-condition);
In this statement, the program proceeds to evaluate the body of the loop first. At the end of the
loop, the test-condition in the while statement is evaluated. If the condition is true, the program
continues to evaluate the body of the loop once again. This process continues as long as the
condition is true. When the condition becomes false, the loop will be terminated and the control goes
to the statement that appears after the while statement.
The do..while constructs provides an exit-controlled loop and therefore the body of the loop is
always executed at least once.
NESTED LOOPS:
C allows its users to have nested loops, i.e., loops that can be places inside other loops.
Although this feature will work with any loop like while, do-while, and for but it is most commonly
used with the for loop, because this is easiest to control.
In C, loops can be nested to any desired level, for loops can be nested as follows:
..............
..............
for(i=1;1<10;1++) // Outer Loop
{
for(j=1;j<10;j++) //Inner Loop
{
............
6
............
}
}
for(...)
for(...)
{ {
...............
......for(...)
if(condition)
{
break; .......
....... if(condition)
break;
} .......
........
}
........
Transfers control out
}
of the for loop
Transfers control out
of the for loop
The continue statement:
Like the break statement, the continue statement can only appear in the body of a loop. When
the compiler encounters a continue statement then the rest of the statements in the loop are skipped
and the control is unconditionally transferred to the loop-continuation portion of the nearest
enclosing loop. Its syntax is:
continue;
When the continue statement is encountered in the while loop and in the do-while, the control
is transferred to the code that tests the controlling expression. In a for loop the continue statement
causes a branch to the code that updates the loop variable.
Eg:
#include<stdio.h>
main()
{
int n=1;
while(n<=20)
{
if(n%2==1)
{
n++;
continue;
}
printf(“%d ”, n);
n++;
}
}
The following is illustrate the use of continue statement in loops.
while(...) do
{ {
...... ......
if(condition) if(condition)
continue; continue;
....... .......
} }while(...);
........ ........
for(...) for(...)
{ {
...... ...............
for(...)
if(condition) {
continue; .......
....... if(condition)
} continue;
........ .......
}
........
Transfers control to the
condition of the for }
Transfers control to
the condition of the
for
GOTO STATEMENT:
8
The goto statement is used to transfer control to a specified label. However, the label must
reside in the same function and can appear only before one statement in the same function. The
syntax of goto statement is as shown below.
Here, label is an identifier that specifies the place where the branch is to be made. Label can
be any valid variable name that is followed by a colon(:). The label is placed immediately before the
statement where the control has to be transferred.
The label can be placed anywhere in the program either before or after the goto statement.
Whenever the goto statement is encountered the control is immediately transferred to the statements
following the label. Therefore, the goto statement breaks the normal sequential execution of the
program. If the label is placed after the goto statement, then it is called a forward jump and in case it
is located before the goto statement, it is said to be a backward jump.
Eg-1:
#include<stdio.h>
main()
{
int n,sum=0;
for(n=1;n<=20;n++)
{
sum=sum+n;
if(n==10)
{
goto addition;
}
addition:
printf(“%d ”, sum);
}
Eg-2:
#include<stdio.h>
main()
{
int n=1;
naturals:
printf(“%d ”,n)
n++;
if(n<=10)
goto naturals;
}