7 - Decision Making and Branching
7 - Decision Making and Branching
Branching
Introduction
▶ Generally C program statement is executed in a order in which they
appear in the program.
▶ But sometimes we use decision making condition for execution
only a part of program, that is called control statement.
▶ Control statement defined how the control is transferred from one
part to the other part of the program.
▶ There are several control statement like if...else, switch, while,
do....while, for loop,break, continue, goto etc.
If statement
➢The if statement may be implemented in different
forms depending on the complexity of conditions to be
tested.
➢The different forms are :
1. Simple if statement
2. if....else statement
3. Nested if....else statement
4. Using else if statement
Simple if statement
▶ General syntax :
if(i>10) if(i>10)
printf("i is greater than 15\n"); printf("i is greater than 15\n");
printf("not inside if\n"); printf("not inside if\n");
} }
Assignment( using if statement)
● Write a C program to check if a given number is even or odd.
● Write a C program to check if a given number is positive,
negative or zero.
● Write a C program to check if entered character is alphabet or
not.
If else statement
➢ If the expression is true, the statement-block1 is executed, else
statement-block1 is skipped and statement-block2 is executed.
➢ General form of if-else statement :
If-else statement execution flow / flowchart
guess the output
#include<stdio.h> #include<stdio.h>
int main() int main()
{ {
int i =1; int i = 5, j = 6, k = 7;
if(i>10) if(i > j == k)
printf("i is greater than 10\n"); printf("%d %d %d", i++, ++j, --k);
else else
printf("i is lesser than 10\n"); printf("%d %d %d", i, j, k);
printf("\noutside if else statement\n"); return 0;
} }
guess the output
if(a==10.5f)
printf("Matched !!!");
else
printf("Not matched !!!");
guess the output
}
Assignment( using if else staement)
➢ A label is an identifier required for goto statement to a place where the branch is to be
made.
➢ A label is a valid variable name which is followed by a colon and is put immediately
before the statement where the control needs to be jumped/transferred
unconditionally.
Forward jump statement
example
#include<stdio.h>
void main()
{
printf("\nbefore label");
goto label;
printf("\nstatement 1");
printf("\nstatement 2");
label:
printf("\nskipped previous statements\n");
}
Backward jump statement
Example
#include <stdio.h> #include<stdio.h>
int main() int main()
{ {
int num = 1234, rev = 0; int num;
loop: Read:
if(num > 0) printf ("Enter a number:");
{ scanf("%d",&num);
rev = rev*10 + num%10; if(num>0)
num = num/10; {
goto loop; printf("Entered number is %dn",num);
} goto Read;
printf("\nreverse num = %d",rev); }
} }
#include <stdio.h>
int main()
{
int num;
printf("Enter a positive or negative integer number:\n ");
scanf("%d",&num);
if(num >= 0)
goto pos;
else
goto neg;
pos:
{
printf("%d is a positive number",num);
return 0;
}
neg:
{
printf("%d is a negative number",num);
return 0;
}
Guess the output??
int main() #include <stdio.h>
{ void main()
printf("%d ", 1); {
goto l1; int i = 0, k;
printf("%d ", 2); label: printf("%d", i);
l1:goto l2;
printf("%d ", 3); if (i == 0)
l2:printf("%d ", 4); goto label;
} }