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 = 5
Output 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 Even
Output 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 = 3
Output 2 : 
Enter any 4 numbers : 10 25 61 20
Max. Number = 61
Output 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 fail
Output 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 class

Output 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 Distinction

Output 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 class

Output 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 actions and choose 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. This 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

Unconditional Branching: goto statement

The goto statement changes the normal sequence of the program execution by transferring control to another part of the program. It is usually not used because C is completely structured language and most probably, the break and continue statements are used.

Syntax :

demo : -------;  //label
-----------;
if()
{
--------;
break;
}
else 
goto demo;   //goto statement

Example :

#include<stdio.h>
main()
{
a : goto c;
z : printf(" with");
goto e;
b : printf(" coding");
goto d;
c : printf("Learn");
goto b;
d : printf(" free");
goto z;
e : printf(" Code Forever");
}
Output :
Learn coding free with Code Forever

Unconditional Branching: Break Statement

The break statement is used to jump out of a loop. An early exit from a loop can be accomplished using the break statement. When a break statement is encountered inside a loop, the loop is immediately exited and the program continues with the statement immediately following the loop.

Syntax :

The break statement is used to jump out of a loop. An early exit from a loop can be accomplished using the break statement. When a break statement is encountered inside a loop, the loop is immediately exited and the program continues with the statement immediately following the loop.

Syntax :

Note 1: The break statement is only used in either switch statements or loop statements like for, do-while, and while.

Note 2: Normally it is used with if-statement in the loop but it is not compulsory.

Example :

#include<stdio.h>
main()
{
int i;
for(i=1; i<=10;i++)
{
printf("%d",i);
if(i==6)
break;
printf("###");
}
}
Output :
1###2###3###4###5###6

Unconditional Branching: Continue Statement

The continue statement causes the loop to continue with the next iteration skipping the remaining statements in that loop.

Note 1: The continue statement is only used in loop statements like for, do-while, and while.

Note 2: Normally it is used with if-statement in the loop but it is not compulsory.

Example :

#include<stdio.h>
main()
{
int j;
for(j=1; j<=10;j++)
{
printf("%d",j);
if(j==6)
continue;
printf("###");
}
}
Output :
1###2###3###4###5###67###8###9###10###

Remark: Remember that the break statement permanently breaks the loop, whereas the continue statement breaks only that iteration, and continues with the next iteration.

By Admin

11 thoughts on “Conditional And Unconditional Branching Statements In C”
  1. Good info. Lucky me I discovered your site by chance (stumbleupon).
    I have book marked it for later!

  2. I enjoy what you guys are up too. This sort of clever
    work and exposure! Keep up the good works guys I’ve incorporated you
    guys to my own blogroll.

  3. Your method of describing everything in this paragraph is
    really nice, every one be able to without difficulty be aware of it, Thanks a lot.

  4. Thanks for your marvelous posting! I certainly enjoyed reading it,
    you can be a great author.I will ensure that I bookmark your blog
    and definitely will come back later in life. I want to encourage one to continue
    your great work, have a nice afternoon!

  5. I visit each day a few web pages and websites to read articles or reviews, however this weblog gives feature based posts.

  6. I do consider all of the concepts you have introduced on your post.
    They’re very convincing and can definitely work. Still,
    the posts are too brief for beginners. Could you please prolong them a bit from next time?
    Thank you for the post.

  7. Aw, this was an extremely good post. Taking a few minutes and actual effort to produce
    a superb article… but what can I say… I hesitate a lot and never
    seem to get nearly anything done.

  8. Thanks for the marvelous posting! I genuinely enjoyed reading it, you could be a great author.I will ensure
    that I bookmark your blog and will eventually come back in the future.

    I want to encourage that you continue your great writing, have a
    nice holiday weekend!

Leave a Reply

Your email address will not be published. Required fields are marked *