Lesson 6
Control Flow – Decision Making
1. Types of Flow Control Statements
2. if Statement
3. if...else Statement
4. if...else if Ladder
5. switch case Statement
1.Types of Flow Control Statements
There are four types of control statements in C:
• Decision making statements.
• Selection statements.
• Iteration statements.
• Jump statements.
2.if Statement
An if statement consists of a Boolean expression followed by one or more statements.
Syntax
The syntax of an 'if' statement in C programming language is −
if(boolean_expression) {
/* statement(s) will execute if the boolean expression is true */
}
If the Boolean expression evaluates to true, then the block of code inside the 'if' statement will be executed. If the Boolean expression evaluates to false, then the first set of code after the end of the 'if' statement (after the
Closing curly brace) will be will be executed.C programming language assumes any non-zeroFlowDiagram
3.if...else Statement
The if-else statement is used to perform two operations for a single condition. The if-else statement is an extension to the if statement using which, we can perform two different operations.
Syntax
The syntax of an if...else statement in C programming language is −
if(boolean_expression) {
/* statement(s) will execute if the boolean expression is true */
} else {
/* statement(s) will execute if the boolean expression is false */
}
If the Boolean expression evaluates to true, then the if block will be executed, otherwise, the else block will be executed.
C programming language assumes any non-zero and non-null values as true, and if it is either zero or null, then it is assumed as false value.
Flow Diagram
4. if...else if Ladder
The if-else-if ladder statement is an extension to the if-else statement. It is used in the scenario where there are multiple cases to be performed for different conditions. In if-else-if ladder statement, if a condition is true then the statements defined in the if block will be executed, otherwise if some other condition is true then the statements defined in the else-if block will be executed, at the last if none of the condition is true then the statements defined in the else block will be executed. There are multiple else-if blocks possible. It is similar to the switch case statement where the default is executed instead of else block if none of the cases is matched.
1. if(condition1){ //code to be executed if condition1 is true
2. }else if(condition2){ //code to be executed if condition 2 is true
3. }
4. else if(condition3){ //code to be executed if condition 3 is true
5. }
6. else{ //code to be executed if all the conditions are false
7. }
Flowchart of else-if ladder statement in C
5,switch case Statement
A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each switch case.
Syntax
The syntax for a switch statement in C programming language is as follows −
switch(expression) {
case constant-expression :
statement(s);
break;
case constant-expression :
statement(s);
break;
default :
statement(s);
}
Flow Diagram