Decision Making & Branching PDF
Decision Making & Branching PDF
BRANCHING
TYPES OF CONTROL CONSTRUCTS
Classification of control constructs
Control Constructs
Conditional Unconditional
Two types
Selective construct
Loop construct
CONDITIONAL EXECUTION…
Conditional execution…
Selective construct
C supports
- if-else
- if-else-if
- Conditional expression
- switch-case
CONDITIONAL EXECUTION…
• Conditional execution…
– Loop construct
• Sometimes the execution of certain statements need to be
repeated until a given condition is satisfied or it may have to be
repeated for a known number of times.
• Such repetitions are carried out by using a loop construct.
• The loop construct is also known as an iterative construct or
a repetitive construct.
• C supports
– for
– while
– do-while
UNCONDITIONAL EXECUTION
Unconditional execution
C supports
break statement
continue statement
goto construct
DECISION MAKING &
BRANCHING
C program is a set of statements which are executed
sequentially
We have situations where we may have to change the
order of execution of statements based on certain
conditions, or repeat a group of statements until certain
specified conditions are met
C language possesses such decision making capabilities
and supports the statements known as control or
decision making statements
if statement
switch statement
conditional operator statement
goto statement
DECISION MAKING WITH IF
STATEMENT
The if statement is the powerful decision making
statement
Control the flow of execution of statements
Two-way decision statement
If (test_expression)
The computer evaluate the expression first and then,
depending on whether the value of the expression
(relation or condition) is ‘true’ (non-zero) or ‘false’ (zero),
it transfer the control to a particular statement
It has two paths to follow one for the true condition and
the other for the false condition
DECISION MAKING WITH IF
STATEMENT
The if statement is implemented in different
forms depending on the complexity of
conditions to be tested
Simple if statement
if…….else statement
else if ladder
SIMPLE IF STATEMENT
Example:
if (bank balance is zero)
Borrow money
if (room is dark)
Put on lights
if (code is 1)
Person is male
if (age is more than 55)
Person is retired
SIMPLE IF STATEMENT
Syntax:
if (test expression)
{
Statement 1;
}
Next_Statement;
EXAMPLE
printf(“a is printf(“b is
big”); big”);
/* PROGRAM TO FIND WHETHER AN INTEGER IS
ODD OR EVEN */
#include <stdio.h>
main( )
{
int x,y;
printf("Enter an integer\n");
scanf("%d",&x);
printf("Given number is %d\n",x);
SAMPLE INPUT AND
y = x%2;
OUTPUT
if(y==1)
Enter an integer
printf("The given number %d is
5
ODD\n",x);
Given number is 5
else
The given number 5 is ODD
printf("The given number %d is
EVEN\n",x);
Enter an integer
}
8
Given number is 8
The given number 8 is EVEN
EXAMPLE
/* PROGRAM TO CHECK WHETHER A CHAR IS A LETTER OR NOT
*/
#include <stdio.h>
main( )
{
char ch;
printf("Enter any character \n");
ch = getchar( );
printf("Given character is ");
putchar(ch);
if(ch>='A' && ch<= ‘Z’ || ch>='a' && ch <= ‘z’ )
printf("\n and it is an alphabet.\n",ch);
else
printf("\n and it is not an alphabet.\n");
}
SAMPLE INPUT AND OUTPUT
Enter any character
x
Given character is x
and it is an alphabet
NESTED IF-ELSE
• When an if-else construct appears The same example with braces
as a statement in another if-else,
it is known as a nested if-else
for clearer understanding:
if(expression_1)
construct. For example, {
if(expression_1) if(expression_2)
{
if(expression_2) if(expression_3)
if(expression_3) {
statement_1;
{ }
statement_1; else
{
} statement_2;
else }
}
{ else
statement_2; {
statement_3;
} }
else }
{
statement_3;
}
NESTED IF-ELSE…
Nested if-else flow
chart
true expr_ false
1
stmt_1 stmt_2
SELECTING LARGEST OF THREE
VALUES
#include<stdio.h>
main()
{
float A, B, C; OUTPUT:
printf("Enter three values\n"); Enter three values
scanf("%f %f %f", &A, &B, &C); 23 67 88
printf("\nLargest Value is ");
if(A>B)
{ Largest Value is
if (A>C) 88.000000
printf("%f\n",A);
else
printf("%f\n",C);
}
else
{
if (C>B)
printf("%f\n",C);
else
printf("%f\n",B);
}
}
THE ELSE IF LADDER
Multi-path decision making
It is a chain of if ’s in which the statement
associated with each else is an if
if (condition 1)
statement-1;
else if(condition 2)
statement-2;
else if(condition 3)
statement-3;
---------------
else if(condition n)
statement-n;
else
default-statement;
statement-x;
FLOW CHART – ELSE..IF LADDER
EXAMPLE
#include<stdio.h>
main()
{
int marks;
Char grade[10];
scanf(“%d”,&marks);
if (marks > 80)
grade = "Honours";
else if (marks > 70)
grade = "first Division";
else if (marks > 60)
grade = "Second Division";
else if (marks > 50)
grade = "Third Division";
else
grade = "Fail";
printf("%s\n", grade);
}
/* PROGRAM TO CHECK A CHAR AN ALPHABET OR
DIGIT */
#include <stdio.h>
main( ) SAMPLE INPUT AND OUTPUT
{ Enter any character
C
char ch; C is an uppercase letter
printf("Enter any character \n");
ch = getchar();
if(ch >= 'A' && ch <= 'Z')
printf("%c is an uppercase letter \n",ch);
else if(ch >= 'a' && ch <= 'z')
printf("%c is an lowercase letter \n",ch);
else if(ch >= '0' && ch <= '9')
printf("%c is a digit\n",ch);
else if(ch == '\n' || ch == '\t' || ch == ' ')
printf("It is a whitespace character \n");
else
printf("%c is a special character \n");
}
CONDITIONAL EXPRESSION
• Evaluation is as follows:
– The expression_1 is
evaluated to find the logical true expression_1 false
value true (non-zero) or
false (zero).
– If the expression_1 is true,
expression_2 expression_3
then the expression_2 is
evaluated and that is the
resulting value of the
conditional expression.
– If the expression_1 is false, Flow chart representation of conditional expression
then the expression_3 is
evaluated and that is the
resulting value of the
conditional expression.
THE SWITCH STATEMENT
Format:
switch(expr)
expr==cons true
case_1 block
t1
false
expr==cons true
case_2 block
t1
false
expr==cons true
case_3 block
t1
false
…
…
expr==cons true
case_n block
t1
false
default
default block
/*PROGRAM TO PRINT THE DIGITS IN WORDS */
main( )
{ int digit;
printf("Enter a single digit number \n");
scanf("%d",&digit);
switch(digit)
{
case 0: printf("The number entered is ZERO \n");
break;
case 1: printf("The number entered is ONE \n");
break;
case 2: printf("The number entered is TWO \n");
break;
case 3: printf("The number entered is THREE \n");
break;
case 4: printf("The number entered is FOUR \n");
break;
case 5: printf("The number entered is FIVE \n");
break;
case 6: printf("The number entered is SIX \n");
break;
SAMPLE INPUT AND
case 7: printf("The number entered is SEVEN \n");
OUTPUT
break; Enter a single digit number
case 8: printf("The number entered is EIGHT \n");
7
break; The number entered is SEVEN
case 9: printf("The number entered is NINE \n");
break;
}
}
printf("\n\nEnter your
#include<stdio.h>
choice:");
void main()
scanf("%d",&ch);
{
switch(ch)
float a,b,res;
{
int ch;
case 1:res=a+b;
printf("Enter the value of A:");
break;
scanf("%f",&a);
case 2:res=a-b;
printf("Enter the value of B(B<>0):");
break;
scanf("%f",&b);
case 3:res=a*b;
printf("\n\nMathematical
break;
Operations");
case 4:res=a/b;
printf("\n\t1->Add");
break;
printf("\n\t2->Subtract");
}
printf("\n\t3->Multiply");
printf("Answer =%.2f",res);