Table of Contents
C – If Statement
The “if” statement is a fundamental control structure in the C programming language that enables the execution of specific code blocks based on a conditional expression. It plays a pivotal role in decision-making within C programs, allowing developers to create dynamic and responsive applications. With the “if” statement, programmers can instruct the program to execute certain instructions when a given condition is true while providing an alternative set of instructions when the condition is false. In this article, we will delve into the syntax, usage, and versatility of the “if” statement, exploring how it empowers C programmers to build logic-driven and efficient software solutions.
What is if in C?
In C programming, the “if” statement is like a traffic signal. It helps your program make decisions by running different code based on a condition. It’s a fundamental tool for adding choices to your program.
Syntax of if Statement in C
if(condition)
{
// if body
// Statements to execute if condition is true
}
How to use if statement in C?
// C Program to demonstrate the syntax of if statement
#include <stdio.h>
int main()
{
int gfg = 9;
// if statement with true condition
if (gfg < 10) {
printf("%d is less than 10", gfg);
}
// if statement with false condition
if (gfg > 20) {
printf("%d is greater than 20", gfg);
}
return 0;
}
Output
9 is less than 10
How if in C work?
- Step 1: The program checks a condition in the “if” statement.
- Step 2A: If the condition is true, it runs the code inside the “if” block.
- Step 2B: If the condition is false, it skips the code inside the “if” block.
- Step 3: After the “if” block, the program continues with the code outside of it.
Examples of if Statements in C
Example 1: C Program to check whether the number is even or odd.
In this program, we’re checking whether a number is even (divisible by 2) or odd (not divisible by 2), except for one specific case.
// C Program to check if the number is even or odd
#include <stdio.h>
int main()
{
int n = 4956;
// condition to check for even number
if (n % 2 == 0) {
printf("%d is Even", n);
}
// condition to check for odd number
else {
printf("%d is Odd", n);
}
return 0;
}
Output
4956 is Even
Example 2: C Program to check whether a number is prime or not.
This program checks the smallest factor of the given number from 2 to sqrt N using a loop. Whenever there is a factor, ultimately we’ll set the flag and exit the loop.
The code to be executed will be put inside the if statement
// C program to check whether a number is prime or not
#include <math.h>
int main()
{
int n = 19;
int flag = 0;
for (int i = 2; i * i <= n; i++) {
// If n is divisible by any number between
// 2 and n/2, it is not prime
if (n % i == 0) {
flag = 1;
break;
}
}
printf("%d is ", n);
if (flag == 1) {
// it is only printed if the number is not prime
printf("not ");
}
printf("a prime number.\n");
return 0;
}
Output
19 is a prime number.
Advantages of if statement
The following are the advantages of if statement in C
- Simple Decision-Making Statement: The
if
statement provides a straightforward way to make decisions in your code. It allows you to specify a condition, and if that condition is true, the associated block of code is executed. - Ease of Use and Understanding:
if
statements are easy to read and understand, making your code more human-readable. They are a fundamental concept in programming, and most developers are familiar with their usage. - Evaluate Expressions of All Types:
if
statements are versatile and can evaluate expressions of various data types, including integers, characters, booleans, and more. As long as the condition provided evaluates to eitherTrue
orFalse
, it can be used in anif
statement.
Disadvantages of if Statement
- Single Block: In a typical
if-elif-else
structure, each condition is checked one by one until a true condition is found, and then the associated block is executed. Once a matchingif
orelif
block is found, subsequent conditions are not tested. So, it does not necessarily test all blocks when a match is found at the start. - Complexity and Readability: When you have a large number of conditions and associated code blocks, the code can become complex and less readable. This is a valid concern, and it’s important to structure your code effectively to maintain readability. In such cases, you might consider using other constructs like dictionaries or switch-case statements if they are available in your programming language to improve code organization.
- Performance for Many Conditions: In some situations, especially when dealing with a large number of conditions, using multiple
if
statements can lead to slower performance compared to other data structures optimized for this purpose, like hash tables or decision trees. However, the impact on performance largely depends on the programming language and the specific use case. In practice, for most everyday programming tasks, the performance difference is negligible.
FAQ- C – If Statement
Q1. How to use if function in C?
Ans. STEP 1: Evaluate the if
condition.
STEP 2A: If the condition is true, execute the code inside the if
block.
STEP 2B: If the condition is false, skip the if
block and continue with the next code.
Q2. What is a compound if statement in C?
Ans. To execute multiple statements when an if
condition is true, enclose them within braces {}
to create a block.
Q3. What are the selection statements in C?
Ans. Selection Statements:if
and switch
: Used for conditional branching based on expressions.
Iteration Statements:while
, do
, and for
: Create loops to execute code based on conditions.
Jump Statements:break
, continue
, and goto
: Control program flow within loops and conditions.return
: Exit functions and optionally return values.
Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together