Simple if else statement,nesting of if else statement & else if ladder

moniadhikary 713 views 13 slides Feb 08, 2019
Slide 1
Slide 1 of 13
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

About This Presentation

1. Introduction
2. Simple if else statement
3. Nesting of if else statement
4. Else if Ladder


Slide Content

Daffodil institute of it Active Learning Assignment Topic:Simple if else statement,Nesting of if else statement & Else If Ladder Prepared by : - Manimay Adhikary ( 180084 )

Brief flow of presentation 1. Introduction 2. Simple if else statement 3. Nesting of if else statement 4. Else if Ladder

The if else Statement if else statement : A control structure that executes one block of statements if a certain condition is true, and a second block of statements if it is false. We refer to each block as a branch . General syntax: if ( <test>) { < statement(s)> ; } else { < statement(s)> ; } Example: # include< stdio.h > int main() { int number; printf ("Enter an integer: "); scanf ("% d",&number );

Flow chart // True if remainder is 0 if ( number%2 == 0 ) printf ("%d is an even integer .",number ); else printf ("%d is an odd integer .",number ); return 0; } Output: Enter an integer: 7 7 is an odd integer

Nested if...else statement -The if...else statement executes two different codes depending upon whether the test expression is true or false . - Sometimes, a choice has to be made from more than 2 possibilities. -The nested if...else statement allows you to check for multiple test expressions and execute different codes for more than two conditions .

Syntax of nested if...else statement if (testExpression1) { // statements to be executed if testExpression1 is true } else if(testExpression2) { // statements to be executed if testExpression1 is false and testExpression2 is true } else if ( testExpression 3) { // statements to be executed if testExpression1 and testExpression2 is false and testExpression3 is true } . else { // statements to be executed if all test expressions are false }

Example #include < stdio.h > int main() { int number1, number2; printf ("Enter two integers: "); scanf ("%d %d", &number1, &number2); // checks if two integers are equal. if(number1 == number2) { printf ("Result: %d = %d",number1,number2); } // checks if number1 is greater than number2. else if (number1 > number2) { printf ("Result: %d > %d", number1, number2); }

// if both test expression is false else { printf ("Result: %d < %d",number1, number2); } return 0; } Output : Enter two integers: 12 23 Result: 12 < 23

Nested if else if Flow Chart if ( <test>) { < statement(s)>; } else if ( <test>) { < statement(s)>; } else if ( <test>) { < statement(s)>; }

The else if ladder

Example main () { int units, custnum ; float charges; printf ("Enter customer no. and units consumed\n"); scanf ("% d%d",&custnum,&units ); if(units <=200) charges=0.5*units ; else if(units<=400) charges=100+0.65 *(units-200); else if(units<=600) charges=230+0.8 *(units-400); else charges=390 +(units-600); printf ("\n\nCustomer no:%d Charge=%.2f\n", custnum, charges ); }

Thank You..
Tags