Free Courses

if-else Statement in C

Learn about if...else statement in C.

Updated - 11 Mar 20225 mins readPublished : 5 Jan 2022
Published : 5 Jan 2022
quiz
Challenge Inside! : Find out where you stand! Try quiz, solve problems & win rewards!

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 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 if-else statement.
  • We also discover different programs implemented using 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 colors of light which indicate different decisions to be made according to the color of the light. If the color 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

if (condition or expression) {
     // statement(s) will execute if the condition or expression is true 
} else {
     // statement(s) will execute if the condition or expression is false
}

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

if (condition) {
   // statement(s) will execute if the condition or expression is true 
}

//statement(s) outside if body

How if-else statement in C works?

If-else statement allows 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 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 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.

#include <stdio.h>
#include <stdlib.h>

int main() {
	if (printf("Hello World!!"))

		return 0;
}

Output

Hello World!!

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

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

Example of if-else statement in C

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

#include <stdio.h>
#include <stdlib.h>

int main() {    
    int n;    
    printf("Enter a number:");    
    scanf("%d",&n);    
    if (n % 2 == 0) {    
        printf("%d is even number",n);    
    } else {
        printf("%d is a odd number",n);
    }
    return 0;  
}    

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

Enter a number:4
4 is even number

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

#include <stdio.h>
#include <stdlib.h>

int main() {
	int age;
	printf("Enter your age: ");
	scanf("%d", &age);
	if (age >= 18) {
		printf("You are eligible to vote");
	} else {
		printf("You are not eligible to vote");
	}

	return 0;
}

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

Enter your age: 18

Output

You are eligible to vote

Input

Enter your age: 15

Output

You are not eligible to vote

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 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 if-else statement control the flow of the program, it is also called as Control Flow statement.
Challenge Time!
quiz
quiz
Time to test your skills and win rewards! Note: Rewards will be credited after the next product update.
certificate icon
Certificates
C Tutorial
This program includes modules that cover the basics to advance constructs of C Tutorial. The highly interactive and curated modules are designed to help you become a master of this language.'
If you’re a learning enthusiast, this is for you.
Module Certificate
Criteria
Upon successful completion of all the modules in the hub, you will be eligible for a certificate.
You need to sign in, in the beginning, to track your progress and get your certificate.
Free Courses by top Scaler instructors