Decision making and branching(if-else statement) in C
In this section we are going to discuss the concepts of branching in C program.
Here we will cover:- if statement
- if-else statement
- switch statement
- goto statement
- conditional operator statement
"
We will be dealing with switch, goto and conditional operator in our next post.
In a program, in certain conditions, we may have to change the order of execution or repeat a group of statement until the specified condition is satisfied or met.
C language supports this functionality by using the above mentioned statements.
Simple IF statement:
syntax: if (test expression)
{
statements/codes;
}
statements/codes;
The above shown syntax is very easy to understand and it is widely used in C program whenever it is needed.
Here if the test expression is true than and then only the block of codes inside the curly braces just after if is executed other wise it is skipped.
If-statement flow chart |
The IF-Else Statement
It is an extension of If statement where one of the two block of codes are executed compulsorily, unlike If statement where if the test condition comes to be false than the block of codes aren't executed and the program continuous with the rest of the line of codes.syntax: if (test expression)
{
statements/codes;
}
else
{
statements/codes;
}
statements/codes;
If- else statement flow chart |
Nesting of If-Else statement
- When there are some conditioned that are needed to be checked inside another condition than this kind of nesting If-Else statements are used.
- for example : If we want to check weather the number is even or odd inside a condition where we first check weather it is positive or negative, then this kind of nesting is used.
Syntax:
if (test - condition-1)
{
if (test condition-2)
{
statement -1;
}
else
{
statement-2;
}
}
else
{
statement-3;
}
statement-x;
Flow chart:
Nested If-else statement |
Here the above image shows the flow chart of Nested if- else statement of the syntax example given above that. It shows that if the first test condition-1 is true than the test condition-2 inside that if statement is tested and executed if true, else the else part, inside the first if part, is executed, and if the first test condition is false than the statements inside the first if part will be ignored and the statement -3 will be executed and then the program will continue with statement -x.
The Else-if Ladder
- The else if ladder is the chain of If statements in which multiple conditions are checked of which only one condition must be true, else the else part will be executed.
- For example: If we want to write a menu driven program and we want to select only one function,for example a simple calculator in which only one function i.e either addition subtraction, multiplication or division should be chosen by user to operate on two numbers than we can use this kind of statements.(The program of this question will be posted later)
- well the above example can be easily solved using the Switch case statement (which will be discussed in later posts)
if(condition 1)
statement-1;
else if(condition -2)
statement -2;
else if(condition -2)
statement -2;
else if(condition -2)
statement -2;
else if(condition -2)
statement -2;
else
default -statement;
- The above ladder is known as else-if ladder.
- Here the conditions are checked from first to last.
- If one of the conditions are satisfied than the rest of the else -if statements and the last else statements are neglected and skipped.
- In some situation, where no condition is satisfied, the statement inside the last else statement are executed.