Decision Making Statements in C - 1622445973328
Decision Making Statements in C - 1622445973328
The statements which alter the flow of execution of the program are known as
control statements. In the absence of control statements, the instructions or
statements are executed in the same order in which they appear in the program.
However, in practice we have to do certain calculations or task depending on
whether a condition or test is true or false. Similarly, sometimes it is necessary to
perform repeated actions or skip some statements. For these statements, control
statements are needed. They control the flow of program so that it is not
necessary to execute statements in the same order in which they appear in the
program.
If statement
If….else statement
Else if statement
Switch statement
Do….while loop
While loop
For loop
If Statement:
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 executed.
Flow Diagram:
For Example:
Output:
Another Example:
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, i.e., one is for the correctness of that condition,
and the other is for the incorrectness of the condition. Here, we must notice that
if and else block cannot be executed simultaneously. Using if-else statement is
always preferable since it always invokes an otherwise case with every if
condition. The syntax of the if-else statement is given below.
Flowchart:
For Example:
Output:
Another Example:
Output:
else if statement:
For Example:
Output:
Another Example:
Output:
Nested if….else statement:
When an if else statement is present inside the body of another “if” or “else” then
this is called nested if else.
Syntax:
For Example:
Output: