PC - Unit - 2
PC - Unit - 2
Flow chart
True Con False
diti
on?
Statement (s)
2. if … else statement
The if … else statement is an extension of simple if statement. It takes the following general
form
Syntax:
if (condition)
{
True block statement(s)
}
else
{
False block statement(s)
}
statement-x
If the condition is true, then the true-block statement(s), immediately following the if
statements are executed.: otherwise, the false-block statements are executed. In either case,
either true-block or false-block will be executed, not both. In both cases, the control is
transferred subsequently to the statement-x.
Flow chart
True False
Con
diti
on?
else
{
if (condition3)
stmt3;
else
stmt4;
}
Flow chart
True Con False
diti
on1
4. Else – if ladder
This is the most general way of writing a multi-way decision. A multi-way decision is a chain of
ifs in which the statement associated with else is an if. It takes the following general form
Syntax
if (condition1)
stmt1;
else if (condition2)
stmt2;
-----
else if (condition n)
stmnt;
else
stmt x ;
flow chart
Stmt n Stmt x
Switch statement
C has a built-in multiway decision statement known as switch. It is used to select one among
multiple decisions. The switch statement tests the value of a given variable (or expression)
against a list of case values and when a match is found, a block of statements associated with
that case is executed.
Syntax
switch (expression)
{
case value1 : stmt1;
break;
case value2 : stmt2;
break;
------
default : stmt – x;
}
Flow chart
Switch
(expres
sion)
Exp =value2
Stmt2
|
|
default Stmt-x
False
Initialization Increment/
condition decrement
True
Like the initialization section, the increment section may also have more than one part.
The multiple arguments in the increment section are separated by commas.
Ex: for(i=1 , j=10; i <= j; i++, j--)
Further, the test-condition may have any compound relation and the testing need not be
limited only to the loop control variable.
Ex:
sum = 0;
for( i = 1 ; i < 20 && sum < 100 ; ++i)
{
sum=sum + i;
}
Another unique aspect of for loop is that one or more sections can be omitted, if
necessary.
Ex:
m = 5;
for( ; m != 100 ; )
{
m = m + 5;
}
Here both initialization and increment sections are omitted in the for statement. The
initilisation has been done before the for statement and the control variable is incremented
inside the loop. In such cases the sections are left blank. However, the semicolons separating
the sections must remain. If the test-condtion is not present, the for statement sets up an
infinite loop.
Nesting of for loops
Nesting of loops, that is, one for statement within another for statement, is allowed in C. For
example two loops can be nested as shown bellow
-----------------------------------
------------------------------------
for( i = 1 ; i < 10 ; ++i)
{
----------------------
---------------------
for( j = 1; j != 5; ++j)
{
-----------------------
-----------------------
}
}
------------------------------
-----------------------------
while loop
The simplest of all the looping structures in C is the while statement.
Syntax
while (condition)
{
body of the loop
}
The while is an entry-controlled loop statement. The condition is evaluated and if the condition
is true, then the body of the loop is executed. After execution of the body, the test-condition is
once again evaluated and if it is true, the body is executed once again. This process of repeated
execution of the body continues until the test-condition finally becomes false and the control is
transferred out of the loop. On exit, the program continues with the statement immediately
after the body of the loop.
Flow chart initialization
Is False
expressio
n?
True
Incr/ dec
do-while loop
The while loop construct, makes a test of condition before the loop is executed. Therefore, the
body of the loop may not be executed at all if the condition is not satisfied at the very first
attempt. In some situations it might be necessary to execute the body of the loop before the
test is performed. Such situations can be handled with the help of the do- while statement.
Syntax
Initialization
do
{
body of the loop
inc/ dec
} while (condition);
On reaching the do statement, the program proceeds to evaluate the body of the loop first. At
the end of the loop, the 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 immediately after the while statement.
Since the condition is evaluated at the bottom of the loop, the do---while construct provides an
exit-controlled loop and, therefore, the body of the loop is always executed atleast once.
Flow chart
initialization
Incr/ dec
True Is
expressio
n?
Flase
Program
Output
main( ) 1
{ 2
int k; 3
4
5
k = 1;
do
{
printf (”%d”,k);
k++;
} while (k == 5);
}
Summary:
It is a keyword used to terminate the loop (or) exit from the block
The control jumps to next statement after the loop (or) block
‘break is used with for, while, do-while and switch statement
When break is used in nested loops then only the innermost loop is terminated
When a break statement is encountered inside a loop, the loop is immediately exited and the
program continues with the statement immediately following the loop. When the loops are
nested, the break would only exit from the loop containing it. That is, the break will exit only a
single loop.
Program
main( )
{ int i; Output
for (i=1; i<=5; i++) 1
2
{
3
printf (”%d”, i);
if (i= =3)
break;
}
}
2) continue (skipping a part of a loop)
Like the break statement, C supports another similar statement called the continue
statement. However, unlike the break statement that causes the loop to be terminated, the
continue, as the name implies, causes the loop to be continued with the next iteration after
skippin any statements in between. The continue statement tells the compiler, “skip the
following statements and continue with the next iteration”.
It is a keyword used for continuing the next iteration of the loop
It skips the statements after the continue statement
It is used with for, while and do-while
Syntax
{
Stmt1;
Stmt2;
continue;
Stmt3;
Stmt4;
}
Program Output
1
main( )
3
{
4
int i;
5
for (i=1; i<=5; i++)
{
if (i= =2)
continue,
printf(“%d”, i)
}
}
3) goto
C supports the goto statement to branch unconditionally from one point to another in the
program. The goto requires a label in order to identify the place where the branch is to be
made. A label is any valid name, and must be followed by a colon( : ) . The label is placed
immediately before the statement where the control is to be transferred. The general form of
goto is shown in fig
The label : can be anywhere in the program either before or after the goto label; statement.
During running of a program when a statement like goto begin; is met, the flow of control
jumps to the statement immediately following the label begin. This happens unconditionally.
Note that a goto breaks the normal sequential execution of the program. If the label: is
before the statement goto label; a loop will be formed and some statements will be executed
repeatedly. Such a jump is known as a backward jump. On the other hand, if the label: is placed
after the goto label; some statements will be skipped and the jump is known as a forward
jump.
Program
main( ) Output
Hello
{
you
printf(’Hello”);
goto l1;
printf(”How are”);
l1: printf(”you”);
}