2.1 Control statements in C programming (sequential & conditional)
Control statements in C: -
- C is a structure-orientated programming language; the statements that are used to control the flow of execution of a program are called control structures.
- The statements provide a specific order to execute the C program.
- In C programming, we have four types of control structures.
- Sequential control structures
- Conditional control structures
- Iterative control structures
- Jumping control structures
1) Sequential control structures: -
- The C program is a collection of instructions or set of statements which are executed sequentially in a user-specific order.
- No statement is slipped, and no statement is executed more than once.
Syntax: -
Flow chart: -
Example: -
Output: -
1) Conditional control structures: -
- It is also known as selection, branching, and decision-making statements.
- It selects a statement to execute on the basis of condition.
- Statements get executed when the condition is true and execute alternative statements or get ignored when it is false.
- In C, we have 5 types of conditional control statements.
a. Simple if statement.
b. If-else statement.
c. Nested if statement.
d. Else-if ladder statement.
e. Switch statement.
a) Simple if statement: -
- Simple if is a conditional control statement which works on a condition to select a simple statement.
- If the given condition is true, it gives the statement under the condition.
- If the given condition is false, the statements are ignored to display any statement.
- There is no alternative statement in a simple if because it is a one-way decision-making statement.
- In simple-if, one initialisation, one condition and one statement get executed.
Syntax: -
Flow chart: -
Example: -
Output: -
2) If-else statement: -
- If-else is a conditional control statement which is an extension of simple-if and works on one condition to select a simple statement.
- There is an alternative statement in the if-else statement because it is a two-way decision-making statement.
- If the given condition is true, then the if-block statement gets executed.
- If the given condition is false, then the else-block statement get executed.
- In if-else, one initialisation, one condition, two statements and one statement get executed.
Syntax: -
Flow chart: -
Example: -
Output: -
3) Nested if statement: -
- The nested if is a conditional control statement; it works on more than one condition to select a simple statement.
- A nested if means an if statement inside the if statement.
- If the given condition is true, then another if-block statement executes.
- If the given condition is false, then another if-block statement executes.
- It is used when we need to test multiple conditions, where one condition depends on another condition.
- In a nested if, one initialisation, more than one condition and one statement get executed.
Syntax: -
Flow chart: -
Example: -
Output: -
4) Else-if ladder statement: -
- An else-if ladder is a conditional control statement which is used to check multiple conditions in sequence.
- Conditions are independent; only one block executes.
- The program checks conditions from top to bottom. Once a condition is found true, its block is executed, and the rest of the ladder is skipped.
- In an else-if ladder, there is one initialisation, more than one condition, and more than one statement, but only one statement get executed.
Syntax: -
Flow chart: -
Example: -
Output: -
5) Switch statement: -
- The switch statement is a multi-way conditional control statement.
- It is also known as a selective switch statement.
- The control statement that allows the user to make a decision from the number of choices is called a switch statement.
- We can also use the keywords 'case', 'break', and 'default' to provide multi-selection of statement choices.
- It is similar to if-else-if ladder, but the only difference is that in if-else-if multiple conditions are specified, while in a switch case only one condition is specified.
Syntax: -
Flow chart: -
Example: -
Output: -