Table of Contents
What is an If Statement in C?
An if statement is a control structure that executes a block of code only if a specified condition evaluates to true.Syntax:
Explanation:if (condition) {
// code to be executed if condition is true
}
- The condition inside parentheses is evaluated first.
- If the condition is true, the code inside the block runs.
- If the condition is false, the code is skipped.
- The condition is evaluated.
- If the result is true, the program enters the block.
- If false, the program skips the block and continues.
Example 1: Basic If Statement
Here is a C program to show a basic if statement:
// C program to show a basic if statement
#include stdio.h
int main()
{
int number = 10;
if (number 0)
{
printf("The number is positive.\n");
}
return 0;
}
Output:
Explanation:The number is positive.
The condition number 0 is true because 10 is greater than 0. Therefore, the message is printed.
Example 2: If Statement with User Input
Below is the C program to show if statement with user input:
#include stdio.h
int main() {
int age;
printf("Enter your age: ");
scanf("%d", &age);
if (age = 18) {
printf("You are eligible to vote.\n");
}
return 0;
}
Output:
Explanation:Enter your age: 34
You are eligible to vote.
The program checks whether the age is greater than or equal to 18. If true, it prints the eligibility message.
Example 3: If Statement with Multiple Conditions
Below is the C program to show if statement with multiple conditions:
// C program to show a if statement with
// multiple statements
#include stdio.h
int main()
{
int marks = 85;
if (marks = 50 && marks = 100)
{
printf("You have passed the exam.\n");
}
return 0;
}
Output:
Explanation:You have passed the exam.
The condition uses the logical AND operator (&&). Both conditions must be true for the block to execute.
Important Points
Here are some important points to keep in mind:1. Condition Must Be Valid: The condition must evaluate to either true or false. In C, any non-zero value is considered true.
2. Braces Are Optional but Recommended: If there is only one statement, braces can be skipped, but using them improves readability and avoids errors.
3. Indentation Improves Readability: Proper formatting makes your code easier to understand and maintain.if (x 0)
printf("Positive\n");
4. Supports Complex Expressions: You can combine multiple conditions using logical operators like &&, ||, and !.
Common Mistakes to Avoid
Below are some common mistakes done while using an if statement:1. Missing Parentheses: The condition must be enclosed in parantheses or the syntax becomes valid.
2. Using Assignment Instead of Comparison: Writing = assigns a value instead of checking equality, leading to incorrect results.if x 0 // Incorrect
Correct version:if (x = 5) // Incorrect, assigns value instead of comparing
3. Forgetting Braces in Multiple Statements: Without braces, only the first statement is controlled by the if condition, which can cause logic errors.if (x == 5)
if (x 0)
printf("Positive\n");
printf("Check complete\n"); // This will run always
Conclusion
The if statement in C is one of the most essential tools for writing logical programs. It helps your program make decisions instead of running everything blindly. Once you understand how conditions work, you can build more complex and smarter programs. Mastering this concept is the first step toward control structures in C.Frequently Asked Questions
1. What is an if statement in C?2. What happens if the condition is false?An if statement is used to execute a block of code only when a specified condition is true.
3. Can we use multiple conditions in an if statement?If the condition is false, the code inside the if block is skipped.
4. Is it necessary to use braces in an if statement?Yes, you can use logical operators like && and || to combine multiple conditions.
5. What is the difference between = and == in if statements?Braces are not mandatory for a single statement, but they are recommended for better readability and to avoid mistakes.
The = operator assigns a value, while == compares two values. Using = inside an if condition is a common mistake.
0 Comments