If Statement in C

If Statement in C

31 Mar 2024
4 min read
Learn via Video Course & by Doing Hands-on Labs

C Programming For Beginners Free Course

If Statement in C: An Overview

In the previous tutorial on conditional statements in C, we learned in brief about the if statement in C. In this article from the C tutorial, you'll understand it in deep.

In the world of programming, making decisions is essential, just like in everyday life. Imagine you have to decide whether to go outside or stay indoors based on the weather. In C programming, the 'if' statement helps computers make decisions too. Let's explore what 'if' statements are and how they work. To learn C programming from scratch, consider enrolling in our C Certification Course.

What is an if statement?

An 'if' statement in C is like asking a question to the computer. It checks whether a condition is true or false. If the condition is true, the computer performs the task specified in the block of code. If it's false, the computer skips the ‘if’ block and moves on to the next instruction.


if statement in c programming

Syntax and Structure of If Statement

This is how the structure of an if statement looks like:

   if (condition)
    // code to execute if the condition is true

How ‘if’ statement Work in C?

When using the if statement, 'condition' is something the computer evaluates. If it's true, the code inside the curly braces is executed. If it's false, the code inside the braces is skipped.

Here is how it works:

  1. Condition Checking- Firstly, you need to write the ‘if’ keyword followed by a condition inside the parenthesis. This condition is like a question the program will ask itself.
  2. Making a Decision- If the condition turns out to be true, meaning the answer to the question is “yes”, the code inside the curly braces ‘{}’ following the ‘if’ statement is executed. But if, in case, the condition turns out to be false, the code inside the curly braces is skipped, and the program continues to the next statement after the ‘if’ block.

Example in C Compiler

Here is a simple example that will make you understand the working of ‘if’ statement better:

    #include <stdio.h>

int main() {
    int number = 10;

    // Check if the number is positive
    if (number > 0) {
        printf("The number is positive.\n");
    return 0;

In the above example, the program checks if the variable ‘number’ holds a positive value or not. If the condition ‘number > 0’ is true, meaning ‘number’ is greater than 0, the program prints “The number is positive.” Otherwise, if the condition turns out to be false, nothing will be printed, and the program will end.


The number is positive. 

Advantages of if statement:

  1. With the "if" statement, you can execute specific blocks of code based on whether a certain condition is true or false.
  2. "If" statements allow for flexible program flow.
  3. "If" statements are instrumental in error handling and validation.
  4. As your program grows in complexity, "if" statements remain scalable and adaptable.

Disadvantages of the if statement in C:

  1. As the number of nested "if" statements increases, code readability may decrease.
  2. When dealing with multiple "if" statements that perform similar actions based on different conditions, there's a risk of code duplication.

In conclusion, the ‘if’ statement in C allows for conditional execution of code based on a specified condition. The condition is provided within the parenthesis, the program evaluates it and executes the code within the curly braces ‘{}’ if the condition is true. If the condition is false, the code inside the ‘if’ block is skipped, and program execution continues to the next statement.


Q1. Define if statement in C.

The "if" statement in C allows you to execute a block of code only if a certain condition is true.

Q2. What happens when there are multiple conditions?

When there are multiple conditions in an "if" statement in C, each condition is evaluated in sequence. If any of the conditions is false, the corresponding block of code inside the "if" statement is not executed. If all conditions are true, then the code inside the "if" statement is executed. Or we can also use an if-else statement in such cases.

Q3. How can we use nested if statements in C?

Nested "if" statements in C allow you to put one "if" statement inside another. This means you can check multiple conditions within one another.
Share Article
Batches Schedule
About Author
Sakshi Dhameja (Author and Mentor)

She is passionate about different technologies like JavaScript, React, HTML, CSS, Node.js etc. and likes to share knowledge with the developer community. She holds strong learning skills in keeping herself updated with the changing technologies in her area as well as other technologies like Core Java, Python and Cloud.

Self-paced Membership
  • 22+ Courses
  • 750+ Hands-On Labs
  • 300+ Quick Notes
  • 55+ Skill Tests
  • 45+ Interview Q&A
  • 10+ Real-world Projects
  • Career Coaching
  • Email Support
Upto 66% OFF

To get full access to all courses

Accept cookies & close this