BRANCHING STATEMENTS
Branching / Decision Making Statements
• The statements in the program that helps to
transfer the control from one part to other parts
of the program.
• Facilitates program in determining the flow of
control
• Involves decision making conditions
• See whether the condition is satisfied or not
Branching / Decision Making Statements
• if statement
• if – else statement
• if – else if ladder
• Nested if
• Goto
• Switch case
If statement
# include <stdio.h>
# include <conio.h>
void main()
{
int age ;
clrscr() ;
printf("Enter the age : ") ;
scanf("%d", &age) ;
if(age >= 18)
printf("nThe person is eligible to vote.") ;
getch() ;
}
/* C Program to check you are eligible for voting */
Enter the age : 35
The person is eligible to vote.
Output
• Execute a set of command line or
one command line when the
logical condition is true.
• It has only one option
Syntax
If (condition)
Statement;
If else statement
# include <stdio.h>
# include <conio.h>
void main()
{
int age ;
clrscr() ;
printf("Enter the age : ") ;
scanf("%d", &age) ;
if(age >= 18)
printf("nThe person is eligible to vote.") ;
else
printf("nThe person is not eligible to vote.") ;
getch() ;
}
/* C Program to check you are eligible for voting or not */
Enter the age : 35
The person is eligible to vote.
Enter the age : 11
The person is not eligible to
vote.
Output
• if else statement takes care of
both true and false conditions.
• If block is for true condition
• Else block is for false condition
If else if ladder
#include <stdio.h>
int main()
{
int weekday;
printf(" Please Enter the Day Number 1 to 7
(Consider 1= Monday, and 7 = Sunday) : ");
scanf("%d", &weekday);
if (weekday == 1)
{
printf("n Today is Monday");
}
else if ( weekday == 2 )
{
printf("n Today is Tuesday");
}
else if ( weekday == 3 )
{
printf("n Today is Wednesday");
}
else if ( weekday == 4 )
{
printf("n Today is Thursday");
}
else if ( weekday == 5 )
{
printf("n Today is Friday");
}
else if ( weekday == 6 )
{
printf("n Today is Saturday");
}
else if ( weekday == 7 )
{
printf("n Today is Sunday");
}
else
printf("n Please enter Valid Number
between 1 to 7");
return 0;
}
/* C Program to Print Day Name of Week using Else If Statement */
Please Enter the Day Number
1 to 7 (Consider 1= Monday,
and 7 = Sunday) : 5
Today is Friday
Please Enter the Day Number
1 to 7 (Consider 1= Monday,
and 7 = Sunday) : 35
Please enter Valid Number
between 1 to 7
Output
• Number of logical statements are checked
for executing various statement
• If the first condition is true the compiler
executes the block followed by first if
condition.
• If false it skips the block and checks for
the next logical condition followed by else
if.
• Process is continued until a true condition
is occurred or an else condition is
satisfied.
Nested if else
#include <stdio.h>
int main()
{
float r n1, n2, n3;
printf("Enter three numbers: ");
scanf("%lf %lf %lf", &n1, &n2, &n3);
if (n1 > n2) {
if (n1 > n3)
printf("%.2lf is the largest number.", n1);
else
printf("%.2lf is the largest number.", n3);
} else {
if (n2 > n3)
printf("%.2lf is the largest number.", n2);
else
printf("%.2lf is the largest number.", n3);
}
return 0;
}
/* C Program to find Largest of Three numbers using Nested Else If Statement */
Enter three numbers: -4.5
3.9
5.6
5.60 is the largest number.
Output
Float – it is composed of a number that
is not an integer, because it includes a
fraction representation in digital format. It
is used in c programming when more
precision is needed than what integers
can provide.
lf – (Long Float) it is used to represent
long number
.2 – used to limit the decimal points to
two places.
• When a series of decisions are involved
we use more than one if-else statement.
• If condition is true control passes to first
block i.e., if block. In this case there may
be one more if block.
• If condition is false control passes to else
block. There we may have one more if
block.
Switch case
#include<stdio.h>
int main()
{
float r, area, base, height, l, b;
int choice;
printf("1. Area of a circlen");
printf("2. Area of a trianglen");
printf("3. Area of a rectanglen");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("Enter the radius of the circle: ");
scanf("%f",&r);
area = 3.14 * r * r;
printf("Area of circle = %.2f",area);
break;
case 2:
printf("Enter the base and height of the
triangle: ");
scanf("%f %f",&base, &height);
area = 0.5 * base * height;
printf("Area of triangle = %.2f",area);
break;
case 3:
printf("Enter the length and breadth of the
rectangle: ");
scanf("%f %f",&l, &b);
area = l*b;
printf("Area of circle = %.2f",area);
break;
default:
printf("Invalid choice.");
}
return 0;
}
/* C Program to find the area of the circle, area of the triangle and area of the rectangle according to
the user’s input choice. */
1. Area of a circle
2. Area of a triangle
3. Area of a rectangle
Enter your choice: 1
Enter the radius of the circle: 5
Area of circle = 78.50
1. Area of a circle
2. Area of a triangle
3. Area of a rectangle
Enter your choice: 2
Enter the base and height of the triangle:
10 25
Area of triangle = 125.00
1. Area of a circle
2. Area of a triangle
3. Area of a rectangle
Enter your choice: 3
Enter the length and breadth of the
rectangle: 8 6
Area of circle = 48.00
Output
• Multiway branch statement
• It only requires one argument of any
type, which is checked with number of
cases.
• If the value matches with the case
constant, that particular case constant is
executed. If not the default statement is
executed.
• Break statement – used to exit from
current case structure
Goto
#include<stdio.h>
void main()
{
int n;
clrscr();
printf("Enter a number:");
scanf("%d",&n);
if(n%2==0)
goto even;
else
goto odd;
even:
printf("nThe number is EVEN");
goto end;
odd:
printf("The number is ODD");
goto end;
end:
getch();
}
/* C program to check if a number is even or not using goto statement */
syntax
• This statement does not require any
condition, this is an
UNCONDITIONAL CONTROL JUMP.
• The control is passed to another
part of the program without testing
any condition.
Thank You

Branching statements

  • 1.
  • 2.
    Branching / DecisionMaking Statements • The statements in the program that helps to transfer the control from one part to other parts of the program. • Facilitates program in determining the flow of control • Involves decision making conditions • See whether the condition is satisfied or not
  • 3.
    Branching / DecisionMaking Statements • if statement • if – else statement • if – else if ladder • Nested if • Goto • Switch case
  • 4.
  • 5.
    # include <stdio.h> #include <conio.h> void main() { int age ; clrscr() ; printf("Enter the age : ") ; scanf("%d", &age) ; if(age >= 18) printf("nThe person is eligible to vote.") ; getch() ; } /* C Program to check you are eligible for voting */ Enter the age : 35 The person is eligible to vote. Output
  • 6.
    • Execute aset of command line or one command line when the logical condition is true. • It has only one option Syntax If (condition) Statement;
  • 7.
  • 8.
    # include <stdio.h> #include <conio.h> void main() { int age ; clrscr() ; printf("Enter the age : ") ; scanf("%d", &age) ; if(age >= 18) printf("nThe person is eligible to vote.") ; else printf("nThe person is not eligible to vote.") ; getch() ; } /* C Program to check you are eligible for voting or not */ Enter the age : 35 The person is eligible to vote. Enter the age : 11 The person is not eligible to vote. Output
  • 9.
    • if elsestatement takes care of both true and false conditions. • If block is for true condition • Else block is for false condition
  • 10.
    If else ifladder
  • 12.
    #include <stdio.h> int main() { intweekday; printf(" Please Enter the Day Number 1 to 7 (Consider 1= Monday, and 7 = Sunday) : "); scanf("%d", &weekday); if (weekday == 1) { printf("n Today is Monday"); } else if ( weekday == 2 ) { printf("n Today is Tuesday"); } else if ( weekday == 3 ) { printf("n Today is Wednesday"); } else if ( weekday == 4 ) { printf("n Today is Thursday"); } else if ( weekday == 5 ) { printf("n Today is Friday"); } else if ( weekday == 6 ) { printf("n Today is Saturday"); } else if ( weekday == 7 ) { printf("n Today is Sunday"); } else printf("n Please enter Valid Number between 1 to 7"); return 0; } /* C Program to Print Day Name of Week using Else If Statement */
  • 13.
    Please Enter theDay Number 1 to 7 (Consider 1= Monday, and 7 = Sunday) : 5 Today is Friday Please Enter the Day Number 1 to 7 (Consider 1= Monday, and 7 = Sunday) : 35 Please enter Valid Number between 1 to 7 Output
  • 14.
    • Number oflogical statements are checked for executing various statement • If the first condition is true the compiler executes the block followed by first if condition. • If false it skips the block and checks for the next logical condition followed by else if. • Process is continued until a true condition is occurred or an else condition is satisfied.
  • 15.
  • 17.
    #include <stdio.h> int main() { floatr n1, n2, n3; printf("Enter three numbers: "); scanf("%lf %lf %lf", &n1, &n2, &n3); if (n1 > n2) { if (n1 > n3) printf("%.2lf is the largest number.", n1); else printf("%.2lf is the largest number.", n3); } else { if (n2 > n3) printf("%.2lf is the largest number.", n2); else printf("%.2lf is the largest number.", n3); } return 0; } /* C Program to find Largest of Three numbers using Nested Else If Statement */
  • 18.
    Enter three numbers:-4.5 3.9 5.6 5.60 is the largest number. Output Float – it is composed of a number that is not an integer, because it includes a fraction representation in digital format. It is used in c programming when more precision is needed than what integers can provide. lf – (Long Float) it is used to represent long number .2 – used to limit the decimal points to two places.
  • 19.
    • When aseries of decisions are involved we use more than one if-else statement. • If condition is true control passes to first block i.e., if block. In this case there may be one more if block. • If condition is false control passes to else block. There we may have one more if block.
  • 20.
  • 21.
    #include<stdio.h> int main() { float r,area, base, height, l, b; int choice; printf("1. Area of a circlen"); printf("2. Area of a trianglen"); printf("3. Area of a rectanglen"); printf("Enter your choice: "); scanf("%d", &choice); switch(choice) { case 1: printf("Enter the radius of the circle: "); scanf("%f",&r); area = 3.14 * r * r; printf("Area of circle = %.2f",area); break; case 2: printf("Enter the base and height of the triangle: "); scanf("%f %f",&base, &height); area = 0.5 * base * height; printf("Area of triangle = %.2f",area); break; case 3: printf("Enter the length and breadth of the rectangle: "); scanf("%f %f",&l, &b); area = l*b; printf("Area of circle = %.2f",area); break; default: printf("Invalid choice."); } return 0; } /* C Program to find the area of the circle, area of the triangle and area of the rectangle according to the user’s input choice. */
  • 22.
    1. Area ofa circle 2. Area of a triangle 3. Area of a rectangle Enter your choice: 1 Enter the radius of the circle: 5 Area of circle = 78.50 1. Area of a circle 2. Area of a triangle 3. Area of a rectangle Enter your choice: 2 Enter the base and height of the triangle: 10 25 Area of triangle = 125.00 1. Area of a circle 2. Area of a triangle 3. Area of a rectangle Enter your choice: 3 Enter the length and breadth of the rectangle: 8 6 Area of circle = 48.00 Output
  • 23.
    • Multiway branchstatement • It only requires one argument of any type, which is checked with number of cases. • If the value matches with the case constant, that particular case constant is executed. If not the default statement is executed. • Break statement – used to exit from current case structure
  • 24.
  • 25.
    #include<stdio.h> void main() { int n; clrscr(); printf("Entera number:"); scanf("%d",&n); if(n%2==0) goto even; else goto odd; even: printf("nThe number is EVEN"); goto end; odd: printf("The number is ODD"); goto end; end: getch(); } /* C program to check if a number is even or not using goto statement */
  • 26.
    syntax • This statementdoes not require any condition, this is an UNCONDITIONAL CONTROL JUMP. • The control is passed to another part of the program without testing any condition.
  • 27.