simple if statement and its types with syntax, flowchart and example program
Size: 6.26 MB
Language: en
Added: Oct 13, 2021
Slides: 19 pages
Slide Content
Hello! Myself Archana R Assistant Professor In Dept Of CS SACWC. I am here because I love to give presentations. 1
Decision Making And Branching In Programming In C
INTRODUCTION ‘C’ language processes decision making capabilities supports the flowing statements known as control or decision making statements There are 4 Types: If statement switch statement conditional operator statement g oto statement 3
If statement The if statement is powerful decision making statement and is used to control the flow of execution of statements. if statement is the most simple decision-making statement. It is used to decide whether a certain statement or block of statements will be executed or not. 1
5 Types of if Statement (a) Simple if statement (b) If else statement (d) Else –If ladder 5 (c) Nesting of If-else statement
Simple if Statement If the test expression is true then a true block statement are executed, otherwise the false – block statement are executed. In both cases either true-block or false-block will be executed not both. Syntax: if (test expression) { statement block; } statement-x ;
7 Flow Chart For Simple If Statement 7
If the student belongs to the sports category then additional bonus marks are added to his marks before they are printed. For others bonus marks are not added . 8 Simple If Statement Example i f (category = sports) { marks = marks + bonus marks; } printf (“% d”,marks );
If Else Statement If the test expression is true then a true block statement are executed, otherwise the false block statement are executed. In both cases either true-block or false-block will be executed not both. Syntax: i f (test expression) { true-block statements; } else { false-block statements; } statement – x;
10 Flow Chart For If else Statement 10
Here if the code is equal to ‘1’ the statement boy=boy+1; Is executed and the control is transferred to the statement st -n , after skipping the else part. If code is not equal to ‘1’ the statement boy =boy+1; is skipped and the statement in the else part girl =girl+1; is executed before the control reaches the statement st -n. 11 If else Statement Example If (code == 1) boy = boy + 1; else girl = girl + 1; st -x;
Nesting of i f else statement When a series of decisions are involved we may have to use more than one if-else statement in nested form of follows. Syntax: if ( Test Condition1) { if ( Test Condition2) { Statement -1; } else { Statement -2; } else { Statement -3;} }
13 Flow Chart For nesting of If else Statement 13
14 Nesting of If else Statement Example
Else-If ladder A multi path decision is charm of its in which the statement associated with each else is an If. It takes the following general form. This construct is known as the wise-If ladder. The conditions are evaluated from the top of the ladder to down wards. As soon as a true condition is found the statement associated with it is executed and the control the is transferred to the st -X (i.e.., skipping the rest of the ladder). when all the n-conditions become false then the final else containing the default – st will be executed.
16 Else-If ladder SYNTAX if ( Test Condition - 1 ) { Statement - 1 ; } else if ( Test Condition - 2 ) { Statement - 2 ;} else if ( Test Condition - 3 ) { Statement - 3 ; } else if ( Test Condition –n ) { Statement –n ; } else { default statement ; } Statements - X ;
17 Else-If ladder Flow Chart
18 18 The above construction is known as else if ladders. The conditions are evaluated from top to bottom. As soon as a true condition is found, the statement associated with it is executed and The control is transferred to the Rest of the Program Statement–X (skipping rest of the ladder). When all the “n” conditions become false, then the final else containing the default statement will be executed. Ex : If (code = = 1) Color = “red”; Else if ( code = = 2) Color = “green” Else if (code = = 3) Color = “white”; Else Color = “yellow”; If code number is other than 1,2 and 3 then color is yellow. Else-If ladder