C – if Statement

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Overview

When situations comes in our real life we need to make some decisions and based on these decisions, we decide what should we do next. either we should do this thing-1 or we should do this thing-2. Similar situations occur in programming also where we need to make some decisions and based on these decisions we execute the next block of statement. So, in this article, we will see the If Statement in C.

What is If Statement in C?

In C language, the if statement is a simple decision-making and branching statement and it is used to control the flow of the program execution.
If statement is a two-way branching statement and it involves boolean expression. It means depending on the condition the control is transferred either to the true block or false block. It is also called a control statement.

How to use if Statement in C?

The given example demonstrate how to use the if statement in C:

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

How does an if Statement Work in C?

If statement allows to evaluate the test expression first and then, building upon whether the condition of the expression is true(non-zero) or false(zero), it shifts the control to a particular block of statement. This point of the program has two paths to follow, one path for the true condition and the other path for the false.

If a certain condition is true then it will execute a block of the statement below it otherwise not.

Some examples of control statement, using if statement in C:

Syntax of if Statement

In the above syntax. we apply conditions in the paranthesis brackets and depending on this condition the control is transferred either to the true block or false block

Flow Diagram of C If Statement

Flow Diagram of C If Statement

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Examples of if Statements in C

Example 1: C Program to check whether the number is even or odd.

Example 2: C Program to check whether a number is prime or not.

Advantages and Disadvantages of C if Statement

Advantages

  • It checks every condition, It also makes a program more robust by allowing only a portion of code to run if a condition has been met.
  • If statements are required to make decisions in the programs. Technically, this could be done with loops and goto(break). But in reality, the if statement is most concise.

Disadvantages During execution as it checks every condition:

  • This makes it difficult to test the code.
  • It is a little bit complex in terms of reading conditions of programs as compared to the switch case.
  • It takes more time for each possible condition because it does fall through all the if statements compared to switch case.

FAQs

Q. Define C if statement.

A. In the C programming language, an "if statement" is a conditional control structure that is used to make decisions in a program. It allows you to execute a block of code if a specified condition is true. The condition inside the "if" statement is evaluated, and if it is found to be true, the code inside the "if" block is executed.

Q. How many types of decision-making statements are there in the C language?

A. In the C language, there are three primary types of decision-making statements:

"if" statement: Used for simple decision-making based on a single condition.

"if-else" statement: Allows you to specify both the "if" and "else" blocks, so you can execute different code when the condition is true or false.

"switch" statement: Used for multi-way decision-making, where different code blocks are executed based on the value of an expression. Q. Can we specify multiple conditions in an if statement?

A. Yes, you can specify multiple conditions in an "if" statement using logical operators like "&&" (logical AND) or "||" (logical OR). For example, you can create compound conditions like this:

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

Conclusion

  • Using if statement we can control the flow of statement(s) in the program.
  • There are four types of if Statement in c: simple if, if-else, nested if-else, and else-if ladder.
  • In C, if statement supports two-way branching statement and multi-way branching statement.
  • We can ignore the ‘else’ part of the program statement and we can simply show the result of the ‘if’ condition/expression in our program.
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more