Hit enter after type your search item

Why we use C if else statement?

In this tutorial, you will learn why we use if, else if, and else statements. You will also see examples of how to use the if, if else, and else statement in C.

The decision-making statement

In C and many other programming languages, the “if” is a decision making statement. What does “decision making” means can be related to the real-world scenarios.

For example, you have to choose from black and white colors (only two options). If you chose black, the store will give you a pair of black socks, otherwise white.

Similarly, as you shut down a system, let us say OS asks you: “Do you really want to shut down the computer?”

The only two options are “Yes” and “No”. If you select “Yes”, system will shut down otherwise it does not.

Now, to translate this into C programming; let us take the Yes/No example. As you run the C program, it asks the user to enter “Y” or “N”.

If “Y” is entered, it should run the commands to shut down the system, otherwise, nothing is done. This can be done by using the if and else statements.

Syntax of simple if statement

The syntax for using the simple if statement is:

if (expression)

{

// Give statements here to execute if condition is true

}

An example of if statement in C language

In this example, we have two variables; a and b. We assigned values to both int type variables.

In the if statement, we checked if the value of variable a is less than or equal to b. If the value is less or equal to b then printf statement will display a message. Otherwise, nothing displays:


The result:

The value of a is less than or equal to b

As you can see, the value of variable a is less than b, so the condition in the if statement is true. As a result, the printf statement executed and displayed the message.

You may try with the bigger value of variable “a” and then see the result.

The else statement in C?

In the above example, you saw we only checked if a condition is True or False and displayed the result if it’s True only.

In order to perform some action or display a message if a condition is False, you may use the else statement with if.

Syntax of if..else statement:

if(expression){

//Give statements here to execute if condition is true

}else{

//Give statements here to execute if condition is false

}

The example below shows how to use the if statement:


The result:

The value of a is greater!

As the value of ‘a’ in this example is 30 which is greater than ‘b’. The condition is false and so the else statement executed.

The else..if Ladder in C

Suppose, rather than two colors (black and white) in the introductory section’s example, we have a few more colors to choose from. A user is asked to select the colors from:

  • Black
  • White
  • Green
  • Yellow
  • Blue
  • Any other color.

So, now a user has six options to choose from and for each selection, you have to perform a different action. This is where you may use the else if statements.

How to use else if statement?

if(first_condition){

//Give statements here to execute if first condition is true

}else if(second_condition){

//Give statements here to execute if second condition is true

}

else if(third_condition){

//Give statements here to execute if third condition is true

}

else{

//If all conditions fail then statements here will execute

}

An example of if, else if and else statements

In this example to explain how if, else if, and else ladder works, the program asks to enter the first character of a color. If you enter any of these characters: (b, w, r, y, g), the code displays the color name. In that case, one of the five if..else if conditions are true.

If you enter any other character, all the conditions evaluate as false and the program executes the else statement:


As I entered character g, the result is:

The color is GREEN!

The Nested If statement

When an if statement is used inside another if or else statement then this is called nested if. The C language supports nested if statement and this can be useful in many situations.

How to use nested if?

The general way of using the nested if statement is:

if(expression) {

//Give statements here to execute for main if statement

if(expression) {

//Give statements here to execute if condition is true in the nested if

}

else {

//Give statements here to execute if condition is false in nested if

}

}

else {

//Statements to execute if outer if condition is false

}

An example of using the nested if statement:

The example below shows a simple usage to show how nested if statement works:


The output of the above code should be:

The value of x=5 and y=10

This div height required for enabling the sticky sidebar