Conditional And Unconditional Branching Statements In C
What are the Conditional And Unconditional Branching Statements In C?
Conditional Branching: if Statement
Simple if/if Statement: It is a two-way decision statement. Depending on whether the value of the expression is ‘true‘ or ‘false‘, it transfers the control to a particular statement.
Syntax :
if(test_expression)
{
true-block statements;
}
Write a program to accept any number, and find out, whether it is negative or positive If it is negative then make it positive and print it.
#include<stdio.h>
void
main()
{
int
n;
printf("\n Enter any number ");
scanf("%d",&n);
if(n<0)
{
n= -n;
}
printf("\n Number = %d",n);
}
Output 1 :
Enter any number 5 Number = 5Output 2 : Enter any number -7 Number = 7
Conditional Branching: if…else Statement
This statement allows selecting one of the two available options depending upon the output of the test expression.
Syntax :
if(test_expression)
{
true-block statements;
}
else
{
false-block statements;
}statement - x;
Write a program to accept any number and find out whether it is even or odd.
/* Program to find out whether number is even or odd */
#include<stdio.h>
void
main()
{
int
n;
printf("\n Enter any number : ");
scanf("%d",&n);
if(n%2==0)
printf("\n The %d is Even ",n);
else
printf("\n The %d is Odd ",n);
}
Output 1 :
Enter any number : 6 The 6 is EvenOutput 2 :
Enter any number : 3 The 3 is Odd
Conditional Branching: Nested if…else Statements
This statement is simply an if statement embedded with an if statement. This allows us to perform multiple actions in a single instruction.
Syntax :
if(test_expression1)
{
if(test_expression2)
{
stmt -1;
}
else
{
stmt -2;
}
}
else
{
stmt -3;
}
stmt -n;
Write a program to find the largest number out of four numbers.
//Program to find out greatest out of numbers
#include<stdio.h>
void
main()
{
int
i,j,k,l;
printf("\n Enter any 4 numbers : ");
scanf("%d %d %d %d",&i,&j,&k,&l);
if(i>j)
{
if(i>k)
{
if(i>l)
printf("\n Max. Number = %d ",i);
else
printf("\n Max. Number = %d ",l);
}
else
{
if(k>l)
printf("\n Max. Number = %d ",k);
else
printf("\n Max. Number = %d ",l);
}
}
else
{
if(j>k)
{
if(j>l)
printf("\n Max. Number = %d ",j);
else
printf("\n Max. Number = %d ",l);
}
else
{
if(k>l)
printf("\n Max. Number = %d ",k);
else
printf("\n Max. Number = %d ",l);
}
}
}
Output 1 :
Enter any 4 numbers : 1 0 1 3 Max. Number = 3Output 2 :
Enter any 4 numbers : 10 25 61 20 Max. Number = 61Output 3 :
Enter any 4 numbers : 100 150 60 200 Max. Number = 200
Conditional Branching: if…else Ladder
Syntax :
if(condition -1)
stmt -1;
else if(condition -2)
stmt -2;
else if(condition -3)
stmt -3;
|
|
|
|
else if(condition -n)
stmt -n;
else
default -statement;
Write a program to accept names and marks in 5 subjects find out percentage marks and print grades to students according to the following conditions. i) If a student is failed in one subject then the grade is FAIL. ii) If percentage >=75 Grade = Distinction. iii) If percentage <75 and percentage >=60 then Grade = First Class. iv) If percentage <60 and percentage >=50 then Grade = Second Class. v) If percentage <50 and percentage >=40 then Grade = Third Class.
#include<stdio.h>
void
main()
{ int
m1,m2,m3,m4,m5,T;
float
per;
char
nm;
printf("Enter the name of a Student \n");
scanf("%s",&nm);
printf("Marks of a 5 subjects \n");
scanf("%d%d%d%d%d",&m1,&m2,&m3,&m4,&m5);
printf("English %d\n Statistics %d \n C Programming %d \n Mathematics %d \n Electronics %d \n",m1,m2,m3,m4,m5);
T=m1+m2+m3+m4+m5;
printf("Total marks of student =%d \n",T);
per=T/5;
printf("Percentage of a Student =%f \n",per);
if(m1<40 || m2<40 || m3<40 || m4<=40 || m5<=40)
{
printf("Student is fail \n");
}
else
if(per>=75)
{
printf("Student get Distinction \n");
}
else
if(per>=60 && per<75)
{
printf("Student get first class \n");
}
else
if(per>=50 && per<60)
{
printf("Student get second class \n");
}
else
if(per>=40 && per<50)
{
printf("Student get third class \n");
}
}
Output 1 :
Enter the name of a Student
xyz
Marks of a 5 subjects
20
60
50
40
65
English 20
Statistics 60
C Programming 50
Mathematics 40
Electronics 65
Total marks of student =235
Percentage of a Student =47.000000
Student is failOutput 2 :
Enter the name of a Student
abc
Marks of a 5 subjects
55
50
70
85
74
English 55
Statistics 50
C Programming 70
Mathematics 85
Electronics 74
Total marks of student =334
Percentage of a Student =66.000000
Student get first classOutput 3 :
Enter the name of a Student
ijk
Marks of a 5 subjects
99
78
80
64
75
English 99
Statistics 78
C Programming 80
Mathematics 64
Electronics 75
Total marks of student =396
Percentage of a Student =79.000000
Student get DistinctionOutput 4 :
Enter the name of a Student
lmn
Marks of a 5 subjects
55
65
50
45
60
English 55
Statistics 65
C Programming 50
Mathematics 45
Electronics 60
Total marks of student =275
Percentage of a Student =55.000000
Student get second classOutput 5 :
Enter the name of a Student
pqr
Marks of a 5 subjects
41
49
50
42
45
English 41
Statistics 49
C Programming 50
Mathematics 42
Electronics 45
Total marks of student =227
Percentage of a Student =45.000000
Student get third class
Conditional Branching: Switch Statement
Switch Statement: We use if-else-if statements to choose one of the many alternatives. But as the number of alternatives increases, the complexity of such a program also increases. Thus, to make it simpler. C has a multi-way decision statement known as a switch.
A case expression can be repeatedly used in a switch statement. That is it allows several alternate courses of action and chooses one to be executed at runtime.
The use of a break statement in every case is used to quite the switch statement after a particular case is matched. Thus only one case gets executed. If the break statement is not used, then all the statements following the matched case will get executed. Thus break statement is compulsory for the proper execution of the switch statement.
Syntax :
switch(expression)
{
case value1:
statement1; //value1 is a constant and is known as case label
break;
case value2: //case labels end with a colon(:)
statement2;
break;
default: //Optional
default-statement;
break;
}
statement-n;
Write a program to accept the month numbers and display the season.
#include<stdio.h>
void
main()
{
int
m;
printf("Enter the month number\n");
scanf("%d",&m);
switch(m)
{
case
1:
case
2:
case
3:
printf("Summer is around the corner");
break;
case
4:
case
5:
case
6:
printf("Spring is around the corner");
break;
case
7:
case
8:
case
9:
printf("Autumn is around the corner");
break;
case
10:
case
11:
case
12:
printf("Winter is around the corner");
default
:
break;
}
}
Output :
Enter the month number
12
Winter is around the corner
Click here: Unconditional Branching Statement