11
OctIf Statement in C
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:
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:
- 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.
- 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:
- With the "if" statement, you can execute specific blocks of code based on whether a certain condition is true or false.
- "If" statements allow for flexible program flow.
- "If" statements are instrumental in error handling and validation.
- As your program grows in complexity, "if" statements remain scalable and adaptable.
Disadvantages of the if statement in C:
- As the number of nested "if" statements increases, code readability may decrease.
- 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.