Intro if-else
In C language there are three major decision making instruction as if statement, the if-else statement, and the switch statement. And one also minor decision making instruction is conditional operator.
The keyword if tells the compiler that what follows is a decision control instruction. The condition following the keyword if is always enclosed within a pair of parentheses. If the condition, whatever it is, is true, then the statement is executed, and If the condition is not true, then statement is not executed.Let's demonstrates the use of if-else with c program:
/*demonstrates of if-else statement*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n=10;
if(n>=15)
printf("n is greater!!");
else
printf("n is less than!!");
getch();
}
Output :
n is less than!!
On execution of this program, when compiler check condition 10>=15, the condition is false so its execute else part statement i.e. n is less than!!.
forms of if-else
There are many way to write if-else statement in program,it can be single if statement,both if-else statement,if-else if statement and etc. There are no limit on how deeply the if's and the else's can be nested. Now let's start to form of if-else :
1. if(condition)
do this;
2. if(condition)
{
do this;
and this;
}
3. if(condition)
do this;
else
do this;
4. if(condition)
{
do this;
and this;
}
else
do this;
5. if(condition)
{
do this;
and this;
}
else
{
do this;
and this;
}
6. if(condition)
do this;
else
{
if(condition)
do this;
else
{
do this;
and this;
}
}
7. if(condition)
{
if(condition)
do this;
else
{
do this;
and this;
}
}
else
do this;
8. if(condition)
do this;
else if(condition)
do this;
else if(condition)
do this;
else
do this;
And now you thinking, what the hell condition? Don't worry, its quite simple and easy to use in programming. In condition part we put data,numeric values with with appropriate operator like as Logical operator, Arithmetic operator etc.
else
{
do this;
and this;
}
}
7. if(condition)
{
if(condition)
do this;
else
{
do this;
and this;
}
}
else
do this;
8. if(condition)
do this;
else if(condition)
do this;
else if(condition)
do this;
else
do this;
And now you thinking, what the hell condition? Don't worry, its quite simple and easy to use in programming. In condition part we put data,numeric values with with appropriate operator like as Logical operator, Arithmetic operator etc.
More if-else
Notice the following if statement :
if(expression)
statement;
Here the expression can any valid expression including a relational expression. We can even use arithmetic expression in the if statement. For example all the following if statement are valid :
if(10 + 5 % 7)
statement;
if(n=20)
statement;
if(-15)
statement;
if(-2.0)
statement;
In C every expression is evaluated in terms of zero and non-zero values.
Note: that in C a non-zero value is considered to betrue, whereas a 0 is considered to be false.
Note: that in C a non-zero value is considered to betrue, whereas a 0 is considered to be false.
The default scope of the if statement is only the next statement. So, to execute more than one statement they must be written in a pair of braces.
if else statement and flowchart
Decision Control Statements and Flowchart
The if Statement
It is used to execute an instruction or sequence/block of instruction only if a condition is fulfilled.
Difference forms of implements if-statement are:
- Simple if statement
- if-else statement
- Nested if-else statement
- else if statement
![]() |
| Figure: Simple if statement syntax and flowchart |
![]() |
| Figure: if-else statement syntax and flowchart |
Nested if-else statement
In nested if...else statement, an entire if...elseconstruct is written within either the body of the if statement or the body of an else statement.
The syntax is as follows:
if(condition_1)
{
if(condition_2)
{
block statement_1;
}
else
{
block statement_2;
}
}
else
{
block statement_3;
}
block statement_4;
![]() |
| Figure: nested if-else statement flowchart |
Else if statement
It is used in multi-way decision on several conditions. This works by cascading comparisons. As soon as one of the conditions is true, the statement or block of statements following them is executed and no further comparison are performed.
The else...if syntax is as follows:
if(condition_1)
block statement_1;
else if(condition_2)
block statement_2;
else if(condition_n)
block statement_n;
else
default statement;
![]() |
| Figure: flowchart for else...if statement |




No comments:
Post a Comment