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:

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

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:

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.