Learn C Language : Decision Control Structure (if, if-else Statement)

if Statement

Like most languages, C uses the keyword if to implement the decision control instruction.

The general form of if statement looks like this:

      if (this statement is true)
execute this statement;

 

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.If the condition is not true then the statement is not executed; instead the program skips past it.

A simple program which demonstrates the use of if statement:

/* Demonstration of if statement */

main()
{
int num;
printf(“Enter a number”);
scanf(“%d”,&num);
if(num == 10)
printf(“number is equal to 10”);
}

 

On execution of this program, if you type a number equal to 10, you get a message on the screen through printf().If you type some other number the program doesn’t do anything.

Multiple Statement within if

It may so happen that in a program we want more than one statement to be executed if the condition following if is satisfied.If such multiple statements are to be executed then they must be placed within a pair of braces.

Example of multiple statements within if:

/* Calculation of bonus */

main()
{
int bonus,i,j,yr_of_ser;
printf(“Enter current and joining year”);
scanf(“%d %d”,&i,&j);
yr_of_ser = i – j;
if(yr_of_ser > 3)
{
bonus = 2500;
printf(“bonus=Rs.%d”,bonus);
}
}

 

if-else Statement

The if statement by itself will execute a single statement, or a group of statements,when the condition following if is true.It does nothing when the condition is false.Used of if-else statement is, execute one group of statements if the condition is true and execute another group of statements if the condition is false.This is what is the purpose of if-else statement.

A simple program which demonstrates the use of if-else statement:

main()
{
int num;
printf(“Enter a number”);
scanf(“%d”,&num);
if(num == 10)
{
printf(“Number is equal to 10”);
}
else
{
printf(“Number is not equal to 10”);
}
}

 

On execution of this program, if you type a number equal to 10, you get a message on the screen that Number is equal to 10.If you type some other number then, you get a message on the screen that Number is not equal to 10.

Nested if-else Statement

It is perfectly alright if we write an entire if-else construct within either the body of the if statement or the body of an else statement.This is called ‘nesting’ of ifs.

A simple program which demonstrates the use of nested if-else statement:

/* A quick demo of nested if-else */

main()
{
int num;
printf(“Enter either 1 or 2”);
scanf(“%d”,&num);
if(num == 1)
{
printf(“Number is equal to 1”);
}
else
{
if(num == 2)
{
printf(“Number is equal to 2”);
}
else
{
printf(“enter number is not correct”);
}
}
}

 

Note that the second if-else construct is nested in the first else statement.If the condition in the first statement is false, then the condition in the second if statement is checked.If it is false as well, the final else statement is executed.

In the above program an if-else occurs within the else block of the first if statement.Similarly, in some other program an if-else may occur in the if block as well.There is no limit on how deeply the ifs and the elses can be nested.

Important

A few points worth noting…

(1) The group of statements after the if upto and not including the else is called an ‘if block’.Similarly, the statements after the else form the ‘else block’.
(2) The else statement is written exactly below the if.
(3) Had there been only one statement to be executed in the if block and only one statement in the else block we could have dropped the pair of braces.
(4) As with the if statement, the default scope of else is also the statement immediately after the else.

Forms of if…

if (condition)
do this;

 

if (condition)
{
do this;
and this;
}

 

if (condition)
do this;
else
do this;

 

if (condition)
{
do this;
and this;
}
else
{
do this;
and this;
}

 

if (condition)
do this;
else
{
if(condition)
do this;
else
{
do this;
and this;
}
}

 

if (condition)
{
if(condition)
do this;
else
{
do this;
and this;
}
}
else
do this;

Leave a Comment