If Else Statement in C Programming: Syntax and Examples

If Else Statement in C Programming: Syntax and Examples

7 mins read2.8K Views Comment
clickHere
Updated on Nov 29, 2023 11:18 IST

Have you heard of the if-else statement in the c programming language? Let us understand this concept in detail and understand it using examples!

The conditional statements in C are if-else. They work when their defined condition is True or False. These statements make it easy to decide when we have many options. Other than if-else, the switch is another conditional statement in C language. In this tutorial, I will elaborate on using conditional if-else statements with examples. By the end of this article, you will get answers to how to use if-else statements in C and what the purpose of conditional statements is in programming languages.

Table of Content

If else Statement in C

In C programming, the if-else statement is called a conditional statement. They are used in the decision-making process. When there are multiple conditions, programmers can use these statements to drive the output.

If-else is often used in pairs where the if statement lies above the else statement. The compiler will first check the “if ” statement, and go to the else part only when the “if” condition fails. 

If the “if” condition satisfies, the “if ” statement will execute. When “if ” returns False, the else statement body will execute.

If-else is also known as control-flow statements as they control the flow of the program. They are useful when the same situation has multiple conditions.

Explore free C programming courses

Syntax of if else statement

if (condition) {
    // code to execute if condition is true
} else {
    // code to execute if condition is false
}

Working of the if-else statements

  • The C compiler will go first to the “if” condition.
  • If the given situation satisfies the “if” condition, its body will execute.
  • In case the given situation is against the “if” condition, the C compiler will search for the else part.
  • The body of the “else” statement will execute.

Examples of if-else Statements in C

Example 1

To understand the logic of these conditional statements, consider a program to find prime numbers.


 
#include <stdio.h>
int main() {
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
if (n <= 1) {
printf("Number is not prime.\n");
return 0;
}
for(i = 2; i <= n/2; ++i) {
if(n % i == 0) {
flag = 1;
break;
}
}
if (flag == 0)
printf("%d is a prime number.\n", n);
else
printf("%d is not a prime number.\n", n);
return 0;
}
Copy code

Output

Enter a positive integer: 8
8 is not a prime number.

Explanation

In the above code, the user input number n is 8.

  1. The compiler will check the if condition: 8%2 == 0? Yes, the “if” condition is True. It will enter the “if” body and will execute all printf statements.
  2. The compiler will jump over the else part and the program will end.

Example 2

Let us consider another example of if-else statements:


 
#include <stdio.h>
int main() {
int age;
printf("Enter your age to check if you can enter this pub: ");
scanf("%d", &age);
// if statement to check age range
if (age >= 20 && age <= 60) {
// Body of if statement
printf("You are invited.\n");
} else {
// Body of else statement
printf("Sorry, you cannot enter.\n");
}
return 0;
}
Copy code

Output

Enter your age to check if you can enter this pub: 13
Sorry, you cannot enter.

Explanation

In the above code, a pub defined a certain age limit for its customers. Customers from 20 to 60 can enjoy the facilities of this pub. 

  1. The program asks the user to input his age.
  2. If input age satisfies the if condition, then the “if” statement will execute.
  3. In case the “if” condition is false, the “else” statement will execute.

Nested if-else Statements in C

C also provides nested if-else statements in case of multiple conditions. The Nested if-else means, if else statement will contain one or more if-else statements. Nested if-else statements contain several if-else statements inside each other. 

It is used to refine the decision-making process.

Syntax of Nested if-else Statement

if (condition1) {
    // code to execute if condition1 is true
    if (condition2) {
        // code to execute if both condition1 and condition2 are true
    } else {
        // code to execute if condition1 is true and condition2 is false
    }
} else {
    // code to execute if condition1 is false
}

Working of Nested If Else Statement

Initially, the first “if” statement will be checked, if the condition is true, program control will enter it and check other “if” statement conditions. When the inner, “if” condition is also True, its body will execute. If the inner “if” statement returns False, its else part will execute. 

When the outer “if” condition is False, program control will go to “else” and execute its body.

Example of Nested If Else Statement in C


 
#include <stdio.h>
int main() {
int n;
printf("Enter the number: ");
scanf("%d", &n);
if (n % 2 == 0) { // condition to check if the number is even
if (n <= 100) {
printf("Number is even and less than or equal to 100.\n");
} else {
printf("Number is even and greater than 100.\n");
}
} else { // this block will execute if the number is odd
printf("Number is odd.\n");
}
return 0;
}
Copy code

Output

Enter the number: 44
Number is even and less than or equal to 100.

Explanation

In the above code, we used two “if” statements, resulting in Nested if-else statements. In the code, there are two “if” conditions for finding the even number:

  1. Input number less than or equal to 100
  2. Input number divisible by 2.

The program asks the user to input a number. After getting the number, the program will check the first “if” condition, i.e., number%2 == 0. If it is True, the program will check the next “if” condition: number <= 100.

When the inner “if ” condition is False, its else part will execute. When the outer “if ” condition is False, its else part will execute.

Introduction to For Loop in C
Introduction to For Loop in C
A loop is one of the most fundamental logical structures in computer programming. Defining loops in code allows computers to repeat specific tasks. Defining the loop in a computer program...read more
Recursion in C++
Recursion in C++
Recursion in a programming language is function calling itself. This article will give a brief of how to use Recursion in C++.
All About While Loop in C
All About While Loop in C
Have you ever wondered how a while loop works in C programming? A control flow statement allows code to be executed repeatedly based on a given Boolean condition. The while...read more

Conclusion

if else statements are used in decision-making and control the flow of the program. Too many “if else” statements make the program unreadable and increase complexity. In that situation, it is preferred to use switch statements. To know more, you can explore our C articles to find out more about the language and consolidate your knowledge of the fundamentals.

Explore free online courses with certificates.

Contributed by: Sonal Meenu

FAQs

What is the syntax of if-else?

The syntax of the if else statement in C is if (condition/ expression) { Body of if statement } else { body of else statement }

What is an if-else statement with an example?

Program to check number is less than 15. #include int main() { int number = 10, i; printf(u201cu201d); if(number < 15); { printf(u201cYou win the game.u201d); } else { printf(u201cYou lost u201d): } return 0; }

How to write if statements in C?

if statements have simple syntax with a condition and its body. The Syntax is as: if (condition) { Body of the if } else { Body of else } Consider a simple example to check if a person is eligible to vote or not. #include int main() { int age, i; printf(u201center your agenu201d); scanf(u201c%du201d, &age); if(age >= 18); { printf(u201cYou can vote.u201d); } else { printf(u201cYou are not eligible to vote in India u201d): } return 0; }

Do if-else statements need {}?

The {} braces define the body of the if-else statements. The opening braces ({) is the starting of the if else statement. The closing braces (}) is the end of the if -else statement. If these condition statements have only one statement in their body, no need to use curly braces {}.

What are the different types of if statement?

There are 4 types of if statements: if, If else, Nested if else and If else Ladder.

What is the if-else conditional structure?

if else is a conditional statement, and they define conditions for their execution. The working of this statement depends on the conditions, when the if condition returns True, u201cfu201d will execute. When the u201cifu201d condition returns False, the else statement will execute.

How do you write two if conditions?

Two u201cifu201d conditions are called nested if. One u201cifu201d contains the other u201cifu201d statement. Its syntax is as: if (condition1) { if (condition 2) { Body of if } } else { Body of else }

What are the advantages of if-else?

The advantages of if-else conditional statements in C are as: Makes decision-making easy. Code looks manageable, only when if-else are used in limit.

Is there a limit to if-else?

No, you can declare any number of if-else statements in your program. But, having many control statements will make code unreadable and confusing. If you want to use many if conditions, use switch statements. Multiple if else statements are called if else ladder in C programming language.

What are the disadvantages of if-else?

The if-else statements in C have some disadvantages if they are not used properly. The disadvantages are as follow: Multiple if-else statements make code complex and unreadable. Increases the testing paths.

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

Comments