Chapter 5
Chapter 5
In some situations, it is necessary to check the condition to make the decision. This involves
performing a logical text. This text results in either true or false. Depending upon the trueness or falsity
of the condition the st’s to be excluded is determined. Then the control transfer’s to that statement in
the program and starts executing the statement form the pt. This is known as conditional execution. It
involves both decision-making and branching. C provides a variety of conditional control st’s, such as
1) if statement
2) if else
3) Nested if else
4) Switch statement
Ex: - program 1
/* Program to accepts a no& prints if it is an odd number*/
Main ()
int number;
Print (“Enter the number\n”);
Scanf (“%d”, &numb);
If (cnum%2) ! = 0
Print f (“%d , is an odd No\n”,number);
}
*)Nested - if st
When a series of decision’s are involved, we may have to use more then one if else statement in
nested form as shown
syntax :- if (cond 1)
{
if (cond 2)
{
st1;
}
else
{
st2;
}
}
else
{
----
}
Flow chart :-
Int a,b,c
Printf (“\n enter any three number”);
Scanf (a, b ,c);
If (a>b)
{
if (a>c)
printf (a is larger);
else
printf (c is larger);
}
else
{
if(b>c)
printf (b Is large);
else
printf(c is large);
}
Elseif ladder:-
Format:-
if(cond 1)
st 1;
elseif(cond 2)
st2;
elseif(cond 3)
st3;
elseif(cond 4)
st4;
else
default st;
Switch Statement:-
It is a multiple-way selector statement. There will be several alternative st’s for execution.
The transfer of control to the specific statement is made based on the value of the switch condition.
The result of the switch condition is either an int or char.
Syntax:
switch(cond)
{
case 1:
block 1;
break;
case 2:
block 1;
break;
case 3:
block 1;
break;
default:
default block;
break;
}
Goto statement :-
Syntax:
goto label;
label:
statements;