Navratri Sale: Get Upto 30% OFF on Live Training! Offer Ending in
D
H
M
S
Get Now
If Statement in C

If Statement in C

27 May 2024
Beginner
1.97K Views
4 min read
Learn via Video Course & by Doing Hands-on Labs

Free C Programming Online Course With Certificate

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.

Flowchart:

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.

Output

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.
Summary

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.

FAQs

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

Live Classes Schedule

Our learn-by-building-project method enables you to build practical/coding experience that sticks. 95% of our learners say they have confidence and remember more when they learn by building real world projects.
Angular Certification TrainingOct 13SAT, SUN
Filling Fast
08:30PM to 10:30PM (IST)
Get Details
Full-Stack .NET Developer Certification TrainingOct 13SAT, SUN
Filling Fast
07:00AM to 09:00AM (IST)
Get Details
ASP.NET Core ProjectOct 19SAT, SUN
Filling Fast
10:00AM to 12:00PM (IST)
Get Details
Advanced Full-Stack .NET Developer Certification TrainingOct 20SAT, SUN
Filling Fast
09:30AM to 11:30AM (IST)
Get Details
.NET Solution Architect Certification TrainingOct 20SAT, SUN
Filling Fast
05:30PM to 07:30PM (IST)
Get Details
.NET Microservices Certification TrainingOct 20SAT, SUN
Filling Fast
05:30PM to 07:30PM (IST)
Get Details
ASP.NET Core Certification TrainingOct 20SAT, SUN
Filling Fast
09:30AM to 11:30AM (IST)
Get Details
Microsoft Azure Cloud Architect with AINov 10SAT, SUN
Filling Fast
07:00AM to 09:00AM (IST)
Get Details

Can't find convenient schedule? Let us know

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.

Accept cookies & close this