Presentation is loading. Please wait.

Presentation is loading. Please wait.

Simple Control Structures IF, IF-ELSE statements in C.

Similar presentations


Presentation on theme: "Simple Control Structures IF, IF-ELSE statements in C."— Presentation transcript:

1 Simple Control Structures IF, IF-ELSE statements in C

2 The if statement The if statement: The if statement: if (expression){ if (expression){statement; } How it works: How it works: The expression is evaluated The expression is evaluated If the expression is TRUE the statement are executed and program continues to the next statement after IF If the expression is TRUE the statement are executed and program continues to the next statement after IF If the expression is FALSE, program continues to the next statement after IF If the expression is FALSE, program continues to the next statement after IF

3 Flowchart for the if statement expr statement true false

4 The if-else statement The if-else statement: The if-else statement: if (expression) { statement1;} else { statement2;}

5 IF-ELSE works as follows Expression is evaluated Expression is evaluated If the expression is TRUE, statement1 is executed and program continues to the next statement after if-else If the expression is TRUE, statement1 is executed and program continues to the next statement after if-else If the expression is FALSE, statement2 is executes and program continues to the next statement after if-else If the expression is FALSE, statement2 is executes and program continues to the next statement after if-else

6 Flowchart for the if-else statement expr true statement1 statement2 false

7 SYNTAX ROOLES and INDENTATION If the statement in IF statement is a single statement, the curly braces could be omitted If the statement in IF statement is a single statement, the curly braces could be omitted If the statement1 or/and statement2 is/are singe statements, the curly braces could be omitted If the statement1 or/and statement2 is/are singe statements, the curly braces could be omittedINDENTATION The statement part of the if, if-else statement should be shifted The statement part of the if, if-else statement should be shifted

8 EXAMPLES What would be the output of the following programming fragment? What would be the output of the following programming fragment? int a = 10; int a = 10; if (a >= 10){ if (a >= 10){ a = a + 1; printf(“%d\n”, a); } a = 0; printf(“%d\n”, a); printf(“%d\n”, a);

9 EXAMPLES What would be the output of the following programming fragment? What would be the output of the following programming fragment? int a = 9; int a = 9; if (a >= 10){ if (a >= 10){ a = a + 1; printf(“%d\n”, a); } a = 0; printf(“%d\n”, a); printf(“%d\n”, a);

10 EXAMPLES What would be the output of the following programming fragment? What would be the output of the following programming fragment? int a = 10; int a = 10; if (a >= 10) if (a >= 10) a = a + 1; else else a = 0; printf(“%d\n”, a); printf(“%d\n”, a);


Download ppt "Simple Control Structures IF, IF-ELSE statements in C."

Similar presentations


Ads by Google