CONDITIONAL AND
UNCONDITIONAL
BRANCHING STATEMENTS
A.Sheshanka
Roll no:03
Csm 1A
Aurora’s Technological and Research Institute
INDEX
❖ TWO TYPES OF BRANCHING .
❖ CONDITIONAL BRANCHING AND UNCONDITIONAL BRANCHING.
1. CONDITIONAL BRANCHING
If , else if , nested else if , if else ladder , switch.
2. UNCONDITIONAL BRANCHING
goto, break, continue.
BRANCHING
When a program breaks sequential flow and jumps to another part of the code it is called
as branching.
CONDITIONAL BRANCHING
When a branching is based on a particular condition,it is known as conditional
branching.
UNCONDITIONAL BRANCHING
If branching takes place without any decision it is known as unconditional
branching.
BRANCHING
BRANCHING
CONDITIONAL UNCONDITIONAL
If goto break continue
switch
If else Nested
if else If else
ladder
CONDITIONAL BRANCHING
if statement
Two way decision statement
(True or false)
Syntax
if
(condition)
//do something if true;
}
If else - statements
Allows selecting one of the two available options.
Depending upon the output of the condition.
Syntax
If (condition)
{
//do something if true:
}
else (condition)
{
//do something if false;
}
Write a c program to find the given number is odd or even.
#include <stdio.h>
int main()
{
int n;
printf(“\n Enter any number “);
scanf(“%d”, n);
if (n%2 == 0)
printf(“\n The number is
even”,n);
else
printf(“\n The number is odd
“,n);
output
Output 1:
Enter any number
6 The number
is even
Output 2:
Enter any number
3 The number is
odd
Nested if else - statements
If statement is embedded with an if statement and it allows to perform multiple actions
in a single instruction.
Syntax
If (condition)
{
If (condition)
{
statement 1;
}
else
{
statement 2;
}
else
{
statement 3;
}
}
Write a c program to find the largest number among 4
numbers.
output
Output 1:
Enter the Four Numbers 1 0 2 3
3 is big
Output 2:
Enter the Four Numbers 100 29 150 90
150 is big
If else ladder
Syntax
If (condition 1)
statement 1;
else if (condition 2)
statement 2;
else if (condition 3)
statement 3;
|
|
|
|
else if (condition -n)
statement -n;
else
statement;
Consider c program which display the grade of student
using else if ladder.
output
Output 1:
Enter the marks of a student : 79
Grade=C
Output 2:
Enter the marks of a student : 96
Grade=A
CONDITIONAL BRANCHING - SWITCH STATEMENTS
A condition can be repeatedly used in switch statement.
Syntax
Switch (condition)
{
case 1:
// do something
Break;
case 2:
// do something
Break;
default:
// do
something
}
Write a c program to print the 1 to 3 using switch .
UNCONDITIONAL BRANCHING
GOTO STATEMENT
It changes the normal sequence of the program execution by transferring
control to other part of a program.
Syntax
goto label;
Statement 2
Statement
3
Statement
4
label;
Example
#include <stdio.h>
int main()
a :goto c;
z : printf(“armulla”)
goto e;
b : printf(“name”)
goto d;
C : printf(“my”)
goto b;
d. : printf(“is”)
goto z;
e. :
printf(“shes
hanka”)
}
BREAK STATEMENT
It is used to jump out a loop .
Syntax
while(------)
{
……..
……..
if(condition)
……
break;
……
…..
}
CONTINUE STATEMENT
It causes the loop to continue with the next iteration skipping the remaining
statements in that loop.
Syntax
For (-----)
{
……
……
if(condition)
…….
Continue;
……..
……..
}
● NOTE POINTS
NOTE 1 : The continue statement is only used in loop statements like for,
Do -while, while.
NOTE 2 : it is used with if statement in the loop but it is not
compulsory.
Break statement permanently breaks the loop ,where as continue statement
breaks only that iteration and continue it with the next iteration.
THANK
YOU.