100% found this document useful (1 vote)
45 views17 pages

Decision Making and Branching

This document discusses decision making and branching in C language using if, switch, conditional operator, and goto statements. It provides details on the different forms of if statements including simple if, if-else, and nested if-else statements. Code examples are given to demonstrate how to use simple if, if-else, and nested if-else statements to evaluate conditions and execute the appropriate code blocks.

Uploaded by

Maruf Ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
45 views17 pages

Decision Making and Branching

This document discusses decision making and branching in C language using if, switch, conditional operator, and goto statements. It provides details on the different forms of if statements including simple if, if-else, and nested if-else statements. Code examples are given to demonstrate how to use simple if, if-else, and nested if-else statements to evaluate conditions and execute the appropriate code blocks.

Uploaded by

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

CHAPTER 5

DECISION MAKING AND BRANCHING


Introduction
C language possesses decision making capabilities by
supporting following statements:
1. If statement
2. switch statement
3. Conditional operator statement
4. goto statement
DECISION MAKING AND BRANCHING
Decision making with if statement
Decision making with if statement
• Format: if(test expression)
• It is basically a two-way statement and is used in conjunction
with an expression.
• It allows the computer to evaluate the expression first and then,
depending on whether the value of the expression (relation or
condition) is true or false, it transfer the control to a particular
statement.
• This point of program has two paths to follow:
1. One for the true condition and
2. The other for the false condition.
Different forms of if statement
1. Simple if statement
2. if……………else statement
3. Nested if…………….else statement
4. Else if ladder
Simple if statement
General form of a simple if statement

if(test expression)
{
statement-block;
}
statement-x;
Simple if statement
• The statement-block may be a single statement or a
group of statements.
• If the test expression is true, the statement-block will be
executed; otherwise the statement block will be skipped
and the execution will jump to the statement-x.
• Remember, when the condition is true, both the
statement block and the statement-x are executed in
sequence.
Simple if statement
{ int a, b, c, d;
float ratio;
printf("Enter four integer values\n");
scanf("%d %d %d %d", &a, &b, &c, &d);

if (c-d!= 0) /* Execute statement block */


{
ratio = (float)(a+b)/(float)(c-d);
printf("Ratio = %f\n", ratio);
}
}
if………else statement
General format:
if(test expression)
{
True-block statement;
}
else
{
False-block statement;
}
Statement-x
if………else statement
•If the test expression is true, then the true-
block statement, immediately following the if
statements are executed; otherwise, the
false-block statement are executed.
•In either case, either true-block or false block
will be executed.
if………else statement
if………else statement
{
int number;
printf("Enter an integer: ");
scanf("%d",&number);
if( number%2 == 0 )
{printf("%d is an even integer",number);}
else
{printf("%d is an odd integer",number);}
return 0;
}
Nesting of if…….else statements
if (test condition-1)
{
if (test condition-2)
{ statement-1; }
else
{ statement-2; }
}
else
{statement-3;}
statement-x
Nesting of if…….else statements
• If condition-1 is false, statement 3 will be executed, otherwise it
continues to perform the second test.
• If condition-2 is true, the statement-1 will be executed;
otherwise statement-2 will be evaluated and the control is
transferred to the statement-x
C program to find the largest of three numbers
{ else
double n1, n2, n3; {
if(n2>=n3)
printf("Enter three numbers: ");
{printf("%.2lf is the largest number.", n2);}
scanf("%lf %lf %lf", &n1, &n2, &n3);
else
if (n1>=n2) {printf("%.2lf is the largest number.",n3);}
{ }
if(n1>=n3)
{printf("%.2lf is the largest number.", n1);} return 0;
else }

{printf("%.2lf is the largest number.", n3);}


}

You might also like