0% found this document useful (0 votes)
12 views5 pages

Chapter 5

Uploaded by

Bhima Game
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views5 pages

Chapter 5

Uploaded by

Bhima Game
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

CHAPTER 5

Decision Making And Branching

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

Flow chart: - Is a picturoial representation of the sample program


1) if statement: -
It is used to execute a statement or set of statements conditionally
It is also called as one-way branching.
Syntax: -
if (condition)
{
St ;
}
Here, the logical condition is text, which results in either true or false. If the result of
the logical text is true (non zero value) then the statement that imm follows if is executed. If it is
false, then the control transfers to the next executable statement. That imm follows if. The
condition may be
exp containing constants, variables or logical compression’s. The condition must be written
within parenthesis.
Flow chart for if: -

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);
}

2) If-else st :- The if st is used to execute only one


syntax :- if (condition) action if there are two sts to be executed
{ st1) alternatively, then if else st is used.
} The if-else st is a two way branching.
else
{
st 2;
}
Flow chart :-

*)Program to print greatest of 2 numbers.

*)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 :-

Program to print largest of three number

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;

You might also like