Branching statements

2,406 views 27 slides Jun 28, 2021
Slide 1
Slide 1 of 27
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27

About This Presentation

BRANCHING STATEMENTS
if statement
if – else statement
if – else if ladder
Nested if
Goto
Switch case
programs
output
flowchart
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...


Slide Content

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 i f – else statement i f – 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 circle\n"); printf ("2. Area of a triangle\n"); printf ("3. Area of a rectangle\n"); 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