if-else Statement in C

Challenge Inside! : Find out where you stand! Try quiz, solve problems & win rewards!

Learn via video course

C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
By Prateek Narang
Free
star5
Enrolled: 1000
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
Prateek Narang
Free
5
icon_usercirclecheck-01Enrolled: 1000
Start Learning

Overview

Decisions are always taken based on different conditions, whether it is real life or programming, it applies to both. In C programming language, if-else statement is used to perform the operations based on some specific condition. If the given condition is true, then the code inside the if block is executed, otherwise else block code is executed. It specifies an order in which the statements are to be executed. If-else statement controls the flow of a program and hence also termed as control statements.

Scope

  • This article defines the implementation of the decision-making process in C language using the if-else statement.
  • We also discover different programs implemented using the if-else statement.
  • Advantages and Disadvantages of if-else statement.

What is if-else statement in C

In real life, we come across various situations where we need to take a decision to choose one of the many options available. For e.g., when we come across a traffic signal, there are three different colours of light which indicate different decisions to be made according to the colour of the light. If the colour of light is red, then we stop, for yellow, we wait and for green, we are ready to go. So, here we need to make decisions according to different conditions.

In the C programming language, the if-else statement is used for decision-making. If the given condition is true, then the code inside if block is executed, otherwise else block code is executed.

In the C programming language, any non-zero and non-null values are assumed as true, and zero or null values are assumed as false values.

Syntax

Flowchart of the if-else statement in C

Flowchart of the if-else statement in C

if-statement in C

In the if-else statement, the else block is not necessary, so the if-else statement can be used as only the if statement also.

The if statement is used to evaluate a particular condition. If the condition is true, then the if block statement is executed else the if block is skipped and further program is executed.

It is generally used when we have to perform a single set of operations for a particular condition.

Syntax

How if-else statement in C works?

If-else statement allows one to make a decision according to the given conditions. If the given condition is true, then the statements inside the body of logical 'if' are executed, and the statements inside the body of else are not executed. Similarly, if the condition is false, then the statements inside the body of ‘if’ are ignored, and the statements inside the ‘else’ are executed.

For a more clear understanding of the concept, let’s take an example of xyz expression:

If the "xyz expression" is true:

  • statements inside the body of if are executed
  • statements inside the body of else are ignored

If the "xyz expression" is false:

  • statements inside the body of if are ignored
  • statements inside the body of else are executed

Basically, the if-else statement controls the flow of a program and is hence also termed as the Control Flow statement.

How if else statement works In the above example, the value of test is 10. Since the value of test is less than 20, so the if the block on LHS is executed, whereas the condition is false on RHS, so the if block code is not executed and it goes to the else block.

Interesting Fact

We can print "Hello World" without using a single semicolon in the complete program. This is done with the help of if statement.

Output

Isn't that exciting..? It is possible because the printf statement evaluates to True, and hence the expression is executed.

From the above example, we also learn that the if or else block could be empty, and it is not necessary to add statements in the if or else block.

Example of if-else statement in C

Program to check whether a given number is even or odd.

We provided 4 as the input number, since 4 is an even number so the condition of if statement evaluates to true and hence the if block code is executed and we get the below output.

Output

Program to check whether a person is eligible to vote or not

We input 18 as the age which is equal to 18, hence the if condition evaluates to true and we get "You are eligible to vote" as output. Input

Output

Input

Output

Advantages and Disadvantages of If else statement in C

Advantages:

  • if-else statement helps us to make decision in programming and execute the right code.
  • It also helps in the debugging of code.

Disadvantages:

  • if-else statements increase the number of code paths to be tested.
  • If there are a lot of if statements the code sometimes becomes unreadable and complex, in such cases we use Switch case statement.

Conclusion

  • if-else statement is used for decision making in programming.
  • If the given condition is true, then the code inside if block is executed, otherwise else block code is executed.
  • Since the if-else statement controls the flow of the program, it is also called as Control Flow statement.