Gandhinagar Institute of Technology(012) Subject : CPU (21100 03 ) Active Learning Assignment Branch : Computer : A-2 Prepared by : - Vishvesh jasani (160120107042) Guided By: Prof. Nirav Pandya topic:Nesting of if else statement & Else If Ladder
Brief flow of presentation Introduction 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 Output
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 if ( test condition-1 ) statement-1 else if ( test condition-2 ) statement-2; else if (condition-3) statement-3; else if ( condition-n) statement-n; else default-statement; statement-x;
The else if ladder false true true false false false test condition1 statement- nn default statement statement-1 statement-2 true true Entry statement-3 statement-x test condition1 test condition3 test condition-n
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 ); }