simple control structures if, if-else statements in c
TRANSCRIPT
Simple Control Simple Control StructuresStructures
IF, IF-ELSE statements in CIF, IF-ELSE statements in C
The The ifif statement statement The The ifif statement: statement:
if (expression){if (expression){statement;statement;
}} How it works:How it works:
The expression is evaluatedThe expression is evaluated If the expression is TRUE the statement If the expression is TRUE the statement
are executed and program continues to are executed and program continues to the next statement after IFthe next statement after IF
If the expression is FALSE, program If the expression is FALSE, program continues to the next statement after IF continues to the next statement after IF
Flowchart for the Flowchart for the if if statementstatement
expr statementtrue
false
The The if-elseif-else statement statement
The The if-else if-else statement:statement:if (expression) {if (expression) {
statement1;statement1;}}else {else {
statement2;statement2;}}
IF-ELSE works as followsIF-ELSE works as follows
Expression is evaluatedExpression is evaluated If the expression is TRUE, If the expression is TRUE,
statement1 is executed and program statement1 is executed and program continues to the next statement after continues to the next statement after if-elseif-else
If the expression is FALSE, If the expression is FALSE, statement2 is executes and program statement2 is executes and program continues to the next statement after continues to the next statement after if-elseif-else
Flowchart for the if-else Flowchart for the if-else statementstatement
exprtrue
statement1 statement2
false
SYNTAX ROOLES and SYNTAX ROOLES and INDENTATIONINDENTATION
If the statement in IF statement is a If the statement in IF statement is a single statement, the curly braces single statement, the curly braces could be omittedcould be omitted
If the statement1 or/and statement2 If the statement1 or/and statement2 is/are singe statements, the curly is/are singe statements, the curly braces could be omittedbraces could be omitted
INDENTATIONINDENTATION The statement part of the if, if-else The statement part of the if, if-else
statement should be shiftedstatement should be shifted
EXAMPLESEXAMPLES What would be the output of the following What would be the output of the following
programming fragment?programming fragment? int a = 10;int a = 10; if (a >= 10){if (a >= 10){
a = a + 1;a = a + 1;printf(“%d\n”, a);printf(“%d\n”, a);
}}a = 0;a = 0;
printf(“%d\n”, a);printf(“%d\n”, a);
EXAMPLESEXAMPLES What would be the output of the following What would be the output of the following
programming fragment?programming fragment? int a = 9;int a = 9; if (a >= 10){if (a >= 10){
a = a + 1;a = a + 1;printf(“%d\n”, a);printf(“%d\n”, a);
}}a = 0;a = 0;
printf(“%d\n”, a);printf(“%d\n”, a);
EXAMPLESEXAMPLES
What would be the output of the What would be the output of the following programming fragment?following programming fragment? int a = 10;int a = 10; if (a >= 10)if (a >= 10)
a = a + 1;a = a + 1; else else
a = 0;a = 0; printf(“%d\n”, a);printf(“%d\n”, a);