Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

Decision Making and Branching

Department of Computer Science and


1
Engineering
• Instructions of programs are executed either
1.Sequential manner 2. Branching
• In C programming it support sequential program statements
which execute one statement immediately after another
• Decision making structures require that the programmer
specifies one or more conditions to be evaluated or tested by
the program, along with a statement or statements to be
executed if the condition is determined to be true, and other
statements to be executed if the condition is determined to be
false.
• Branching statements allow the flow of execution to jump to a
different part of the program.

Department of Computer Science and


2
Engineering
Conditional branching statement
• C supports the following decision making and
branching
1.if statement
2.switch statement
3.Conditional operator statement(?:operator)

Department of Computer Science and


3
Engineering
Decision making with if statement
• The if statement may be implemented in
different forms depending on the complexity
of conditions to be tested. The different forms
are,
• Simple if statement
• if....else statement
• Nested if....else statement
• Using else if ladder statement
Department of Computer Science and
4
Engineering
Simple if statement
The statements inside the body of “if” only execute if the
given condition returns true. If the condition returns false
then the statements inside “if” are skipped.
Syntax:

if (Test condition)
{
//Block of C statements here
//These statements will only execute if the condition is true
}

Department of Computer Science and


5
Engineering
Department of Computer Science and
6
Engineering
#include <stdio.h>
int main ()
{
/* local variable definition */
int a = 10;

/* check the boolean condition using if statement */


if( a < 20 )
{
/* if condition is true then print the following */
printf("a is less than 20\n" );
}
printf("value of a is : %d\n", a);
return 0;
}
When the above program is compiled and executed it dispalys
a is less than 20;
value of a is : 10

Department of Computer Science and


7
Engineering
If else statement
• If the test expression is evaluated to true,
-statements inside the body of if are executed.
-statements inside the body of else are skipped from execution.
• If the test expression is evaluated to false,
- statements inside the body of else are executed
- statements inside the body of if are skipped from execution.
Syntax:
if (test expression)
{
// statements to be executed if the test
expression is true
}
else
{ // statements to be executed if the test
expression is false
} Department of Computer Science and
8
Engineering
Department of Computer Science and
9
Engineering
/ Program to find largest of two numbers using if else statement
#include <stdio.h>
int main()
{
int number1, number2;
printf("Enter two numbersers: ");
scanf("%d %d", &number1, &number2);
//check is number1 is greater.
if (number1 > number2)
{
printf("Result: %d > %d", number1, number2); }
else
//print number2 is greater
{
printf("Result: %d > %d",number2, number1);
}
return 0;
}

Department of Computer Science and


10
Engineering
Nested if statement
• It is always legal in C programming to nest if-else statements, which means you can use one if or
else if statement inside another if or else if statement(s).
Syntax:
• if ( test condition 1)
• {
• //If the test condition 1 is TRUE then these it will check for test condition 2
• if ( test condition 2)
• {
• //If the test condition 2 is TRUE then these statements will be executed Test condition 2 True
statements;
• }
• else
• { //If the c test condition 2 is FALSE then these statements will be executed Test condition 2 False
statements;
• }
• else
• { //If the test condition 1 is FALSE then these statements will be executed Test condition 1 False
Department of Computer Science and
• statements; } 11
Engineering
Department of Computer Science and
12
Engineering
#include<stdio.h>
main()
{
int x=20,y=30;
if(x==20)
{
if(y==30)
{
printf("value of x is 20, and value of y is 30.");
}
}
}

Department of Computer Science and


13
Engineering
Else if ladder
• When we have multiple options available or we need to take
multiple decisions based on available condition, we can use another
form of if statement called else…if ladder
• else…if ladder each else is associated with another if statement.
• Evaluation of condition starts from top to down.
• If condition becomes true then the associated block with if
statement is executed and rest of conditions are skipped.
• If the condition becomes false then it will check for next condition
in a sequential manner.
• It repeats until all conditions are cheeked or a true condition is
found.
• If all condition available in else…if ladder evaluated to false then
default else block will be executed.
• It has the following syntax:

Department of Computer Science and


14
Engineering
• if (condition 1)
• { // block 1 }
• else if (codition 2)
• { // block 2 }
• else if(codition 3)
• { // block 3 }
• else { // default block }

Department of Computer Science and


15
Engineering
Department of Computer Science and
16
Engineering
• else…if ladder provides multi way decision
making when any one alternative is to be select
among the available option.

Department of Computer Science and


17
Engineering
Programming example
#include<stdio.h>
#include<conio.h>
void main()
{
int x, y, z, ch;
clrscr(); printf("\n1.Addition\n2.Subtraction\n3.Multiplication\4.Division\n");
printf("\nEnter your choice :");
scanf("%d", &ch);
printf("\nEnter X and Y :");
scanf("%d %d", &x, &y);
if (ch == 1)
{
z = x + y;
}

Department of Computer Science and


18
Engineering
else if (ch == 2)
{
z = x - y;
}
else if(ch == 3)
{ z = x * y;
}
else if(c == 4)
{
z = x/y ;
}
else
{
printf("\n Invalid choice! Please try again!");
exit(0);
}
printf("\n Answer is %.2f", z);
getch();
}
Department of Computer Science and
19
Engineering
Switch statement

• Switch case statements are a substitute for long if statements


that compare a variable to several integral values
• The switch statement is a multiway branch statement. It
provides an easy way to dispatch execution to different parts
of code based on the value of the expression.
• Switch is a control statement that allows a value to change
control of execution.
• The switch case statement is used when we have multiple
options and we need to perform a different task for each
option.

Department of Computer Science and


20
Engineering
Syntax:
switch (variable or an integer expression)
{
case value1: //C Statements ;
case value2: //C Statements ;
…..
default: //C Statements ;
}

Department of Computer Science and


21
Engineering
Department of Computer Science and
22
Engineering
• The following rules apply to a switch statement −
• The expression used in a switch statement must have an
integral or enumerated type, or be of a class type in which
the class has a single conversion function to an integral or
enumerated type.
• You can have any number of case statements within a
switch. Each case is followed by the value to be compared
to and a colon.
• The constant-expression for a case must be the same data
type as the variable in the switch, and it must be a constant
or a literal.
• When the variable being switched on is equal to a case, the
statements following that case will execute until
a break statement is reached.
Department of Computer Science and
23
Engineering
• When a break statement is reached, the switch terminates,
and the flow of control jumps to the next line following
the switch statement.
• Not every case needs to contain a break. If
no break appears, the flow of control will fall through to
subsequent cases until a break is reached.
• A switch statement can have an optional default case,
which must appear at the end of the switch. The default
case can be used for performing a task when none of the
cases is true. No break is needed in the default case.

Department of Computer Science and


24
Engineering
Programming Example using Switch
#include <stdio.h>
int main ()
{
/* local variable definition */
char color;
printf(“enter the color”);
scanf(“%c”,color);
switch(color)
{
case ‘G' : printf(“Green\n" );
break;

case ‘R' : printf(“Red color\n" );


break;
case ‘B' : printf(“Blue color\n" );
break;
default : printf("Invalid color\n" );
};
} Department of Computer Science and
25
Engineering

You might also like