DECISSION MAKING AND BRANCHING
CONTENT
DECISION MAKING STATMENTS
IF STATEMENT
SWITCH STATEMENT
CONDITIONAL OPERATOR STATEMENT
GOTO STATEMENT
BRANCHING, SELECTION AND LOPPING
BRANCHING: A realistic C program may require that a
logical test be carried out at some particular point within the
program. One of the several actions then be carried out,
depending on the outcome of the logical test. This is known
as branching.
SELETION: There is a special kind of branching, called
selection, in which one group of statements is selected from
several available groups.
LOOPING: The program may require that a group of
instructions be executed repeatedly, until some logical
condition has been satisfied.
DECISION MAKING STATMENTS
IF STATEMENT
SWITCH STATEMENT
CONDITIONAL OPERATOR
STATEMENT
GOTO STATEMENT
DECISION MAKING WITH IF STATEMENT
The if statement is a powerful decision making statement and
is used to control flow of execution of statements.
GENERAL FORMAT
if (test expression)
FLOW CHART ENTRY
TEST
EXPRESSION FALSE
?
TRUE
EXAMPLES OF DECISION MAKING
if ( age is more than 55)
person is retired;
if (code is 1)
MTE student;
CLASSIFICATION OF IF STATEMENT
FOUR CLASS
SIMPLE IF STATEMENT
IF……ELSE STATEMENT
NESTED IF ……STATEMENT
ELSE IF LADDER
SIMPLE IF STATEMENT
GENERAL FORMAT
if (test expression)
statement-block;
statement-x;
KEY POINTS
The statement –block may be a single
statement or group of statements.
If test expression is true, the statement –block
will be executed; otherwise statement-block will
be skipped and statement-x are executed in
sequence.
FLOW CHART OF IF STATEMENT
ENTRY
True
TEST
EXPRESSION
?
False Statement-block
Statement-x
NEXT STATEMENT
THE IF…..ELSE STATEMENT
The if….else statement is an extension of simple if
statement.
GENERAL FORMAT
If (test expression)
{
True-block statement(s);
}
else
{
False-block statement(s);
}
statement-x;
FLOW CHART ENTRY
True False
TEST
EXPRESSION
?
True-block False-block
statements statements
Statement-x
WRITE A PROGRAM TO FIND OUT LARGEST OF TWO
NUMBER.
#include<stdio.h>
#include<conio.h>
main() {
clrscr();
int a,b;
Printf(“ENTER THE VALUE OF A AND B”);
Scanf(“%d%d”,&a,&b);
if(a>b)
printf(“%d”,a);
else
printf(“%d”,b);
getch();}
NESTED IF…..ELSE STATEMENT
When a series of decisions are involved, then
more than if..else statement is used.
Largest
Number
GENERAL FORMAT OF NESTED IF…ELSE STATEMENT
If (test condition 1)
{ if (test condition 2)
{ statement-1;
}
else {
statement-2;
}
}
else
{
statement-3;
}
Statement-x;
FLOW CHART
True
False Test
condition 1
?
False True
Test
Statement-3 condition 2
?
Statement-1
Statement-2
Statement-x
ELSE….IF LADDER
GENERAL FORMAT
if (condition 1)
statement -1;
else if (condition 2)
statement -2;
else if (condition n)
statement -n;
else
default statement;
statement-x;
#include<stdio.h>
int main(){
int Mark;
printf("Enter the mark=");
scanf("%d", &Mark);
if (Mark>79)
printf("Grade= A+");
else if (Mark>74)
printf("Grade= A");
else if (Mark>69)
printf("Grade= A-");
else if (Mark>65)
printf("Grade= B+");
else
printf("Grade= Fail ");
printf("\n");
return 0;
}
FLOW CHART OF ELSE….IF LADDER
TRY YOURSELF
DISADVANTAGE OF IF STATEMENT
We can design a program using if statements to control the
selection. The complexity of such program increases
dramatically when the number of alternatives increases. The
program become difficult to read and follow.
SWITCH STATEMENT
The switch statement tests the value of a given variable
against a list of case values and when a match is found, a
block of statements associated that case is executed.
GENERAL FORMAT OF SWITCH STATEMENT
switch ( expression)
{
case value-1:
block-1;
break;
case value-2
block-2;
break;
…………
default:
default block;
break;
}
statement –x;
GENERAL FORMAT OF SWITCH STATEMENT
KEY POINTS
The break statement at the end of each block signals the
end of a particular case and causes an exit from the
switch statement.
The default is optional case. It will be executed if the
value of the expression does not match with any of the
case values.
No action takes place if all matches fail and the control
goes to the statement-x.
EXAMPLE
index=marks/10;
switch(index)
{
case 10:
case 9:
case 8:
printf(“A+ and HONOURS ”);
break;
case 7:
printf(“A”);
break;
default:
printf(“FAIL”);
break;
The index is always an integer
quantity.
#include<stdio.h>
int main(){
int index, mark;
printf("mark=");
scanf("%d", &mark);
index=mark/10;
switch(index)
{
case 10:
case 9:
case 8:
printf("A+ and Honours");
break;
case 7:
KEY POINT
printf("A");
break;
case 6:
printf("A-");
break;
case 5:
printf("B+");
break;
default:
printf("FAIL");
break;
}
printf("\n");
return 0;
}
CONDITIONAL OPERATOR STATEMENT
GENERAL FORMAT
Conditional expression? Expression1:expression2
EXAMPLE
if (x>o)
flag=0;
else
flag=1;
The above statement is same as
flag=(x>o)? 0:1
ADVANTAGE
When conditional operator is used, the code become
more concise and perhaps, more efficient.
DISADVANTAGE
The readability is poor.
Y=(x>2) ? (2*x+5) : (1.5*x+3);
GOTO STATEMENT
GENERAL FORMAT
goto label; label:
……….. statement;
………. ……..
label: ……….
statement; goto label;
FORWARD JUMP BACKWARD JUMP
KEY POINTS
The goto requires a label in order to identify the place.
A label is any valid variable name and must be followed
with a semicolon
For forward jump the label is placed immediately after
the statement.
For backward jump the label is placed before the
statement.
PROGRAM WITH GOTO STATEMENT
Finding the
Square Root
#include<stdio.h>
#include<math.h>
of a Positive
Number
int main(){
int x; float y;
printf("Enter a number = ");
exampleProblem:
scanf("%d", &x);
if(x<=0){
printf("\nSorry !, Please Enter a Positive Number = ");
goto exampleProblem;
}
else
y=sqrt(x);
printf("\nThe Squared Root is = %f", y);
printf("\n\nYou are Succesfully Commpleted the Task!!!!!\n\n\n");
return 0;
}