If-else Statement in C

If-else Statement in C

4 mins read1.6K Views Comment
Updated on Sep 8, 2023 18:25 IST

If-else Statement in C is a very important topic in C language.This article will teach you this concept with flowcharts and implementation in C language.

2022_08_What-is-8.jpg

This tutorial will discover how conditional statements are used to make decisions in C. This article will focus on the If-else Statement in C. These statements follow the concept that a block of code will only be run if a specific condition is true, and if it is not, the code will not execute.

Also, Explore Data Types in C.

Must Read Control Statements in C

We will be covering the following sections today:

Implementing Arrays in C Programming
Implementing Arrays in C Programming
In C programming, arrays are used to store multiple values of the same type in a single variable. To implement an array, you define its type and size, then initialize...read more
Working with Strings in C++
Working with Strings in C++
Strings in C++ is a very simple yet very important topic from exams as well as interview point of view.
Learn Data Types in C Programming With Examples
Learn Data Types in C Programming With Examples
Data types are the type of data stored in a C program. Data types are used while defining a variable or functions in C. It’s important for the compiler to...read more

Also explore: C Programming Basics Explained

Also explore Tokens in C Programming

“if” Statement in C

Here’s the syntax for a simple if statement in C:

if (condition) {
    // code to be executed if the condition is true
}

If the test expression, aka the condition inside the parenthesis ( ), is True, it is evaluated, and the statement(s) is/are executed. If the test expression is False, the statement(s) is/are not performed.

Curly brackets { } are used in C to denote the beginning and end of a loop body.

NOTE: 

  • In C, values other than zero are interpreted as True, while values other than zero and None are interpreted as False.
  • C respects the case. If or IF will produce an error since “if” is written in lowercase.

Let’s look at the following if Statement flowchart –

2022_08_image-16.jpg

“if” Statement Implementation Examples in C

Example 1:

 
#include <stdio.h>
int main() {
int num = 26;
if (num % 2 == 0) {
printf("The number is even ");
}
return 0;
}
Copy code

Output 1:

The number is even 

Example 2:

 
#include <stdio.h>
int main() {
int a = 78;
int b = 50;
if (a < b) {
printf("First number is smaller ");
}
return 0;
}
Copy code

Output 2

No Output is Generated

“if-else” Statement in C

Here’s the syntax for the if-else statement in C:

if (condition) {
    // code to be executed if the condition is true
} else {
    // code to be executed if the condition is false
}

In the C programming language, the if-else statement only executes the body of the if statement when the test expression is True. When the expression returns False, the body of the other part will be carried out.

Let’s examine the flowchart for the if-else statement –

2022_08_image-28.jpg

“if-else” Statement Implementation Examples in C

Example 1:

 
#include <stdio.h>
int main() {
int age = 12;
if (age < 18) {
printf("Cannot cast a vote ");
} else {
printf("Valid voter ");
}
return 0;
}
Copy code

Output 1:

Cannot cast a vote 

Example 2:

 
#include <stdio.h>
int main() {
int x = 3;
if (x < 0) {
printf("Not a whole number ");
}
else {
printf("This is a whole number ");
}
return 0;
}
Copy code

Output 2:

This is a whole number 

“if-else” Ladder in C

The if-else statement executes two blocks of codes depending on whether the test expression holds True or False. But sometimes, there might be situations where a choice has to be made from more than 2 possibilities.

This is where the if-else Ladder comes in. A ladder allows you to evaluate multiple test expressions and execute different statements.

Here’s the syntax for the if-else Ladder in C:

if (condition1) {
    // code to be executed if condition1 is true
} else if (condition2) {
    // code to be executed if condition1 is false and condition2 is true
} else if (condition3) {
    // code to be executed if condition1 and condition2 are false, but condition3 is true
} 
// ... (more else if clauses as needed)
else {
    // code to be executed if all conditions are false
}
  • Here, when the if statement’s test expression is evaluated to be True, the body of only the if statement gets executed.
  • Otherwise, it moves on to check the expression of the else if statement. The body of else is executed if the expression is true. Note that there can be multiple else if statements in the code.
  • Lastly, if all the test expressions return False, the body of else will be executed. 

Let’s examine the flowchart for the if-else Ladder –

2022_08_image-29.jpg

“if-else” Ladder Implementation Example in C

Example:

 
#include <stdio.h>
int main() {
int age = 8;
if (age >= 13 && age < 20) {
printf("Teenager ");
}
else if (age >= 0 && age < 13) {
printf("Prepubescent ");
}
else {
printf("Adult ");
}
return 0;
}
Copy code

Output:

Prepubescent 

Nested if-else Statements in C

Let’s start by defining nesting. In programming, nesting uses conditional statements or loops inside another set of statements or loops.

Any number of if-else statements can be “nested” inside one another.

Example Syntax

Here’s an example syntax for nested if-else statements in C:

if (condition1) {
    // code to be executed if condition1 is true
    if (condition2) {
        // code to be executed if condition1 and condition2 are both true
    } else {
        // code to be executed if condition1 is true but condition2 is false
    }
} else {
    // code to be executed if condition1 is false
}
  • The above snippet shows that the outer if statement consists of a nested if-else Ladder.
  • If the test expression of the outer if statement holds True, the nested if-else Ladder will be executed.
    • The body of nested if will be executed in case the test expression of the nested if is True.
    • Otherwise, the body of the nested else will be executed if the test expression of the nested else is True.
    • Otherwise, the body of nested else will be executed.
  • In case the expression of the outer if statement is False, the body of outer else will be executed.

Nested if-else Statements Implementation Example

Example:

 
#include <stdio.h>
int main() {
int x = 3;
int y = 7;
if (x > 0) {
if (y > 5) {
printf("x is positive and y is greater than 5\n");
} else {
printf("x is positive but y is not greater than 5\n");
}
} else {
if (y > 5) {
printf("x is not positive but y is greater than 5\n");
} else {
printf("Both x is not positive and y is not greater than 5\n");
}
}
return 0;
}
Copy code

Output:

x is positive and y is greater than 5

Endnotes

I hope this article was helpful for you to understand the different variations of the if-else statements and how they are used to perform decision-making in C. If you want to learn more about C and solidify your basics, you can explore our articles on the programming language.

Contributed by – Prerna Singh

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio