Simple if Statement
The if statement is used to execute a block of code only if a specified condition is true.The if statement evaluates an expression to determine if it is true or false. If the expression is true, the code inside the curly braces is executed. If the expression is false, the code is skipped and the program continues to the next statement.
Syntax:
if (condition) {
// Code to execute if the condition is true
}
Example1:
#include <stdio.h>
int main() {
int number = 10;
if (number > 0) {
printf("The number is positive.\n");
}
return 0;
}
Output
The number is positive.
Example 2: Check if a Number is Even
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num % 2 == 0) {
printf("The number is even.\n");
}
return 0;
}
Output
Enter a number: 4
The number is even.
Example 3: Check if a Character is Uppercase
#include <stdio.h>
int main() {
char ch;
printf("Enter a character: ");
scanf("%c", &ch);
if (ch >= 'A' && ch <= 'Z') {
printf("The character is uppercase.\n");
}
return 0;
}
Output
Enter a character: H
The character is uppercase.
Key points
- Conditions:
- Conditions must evaluate to a boolean (true or false).
- In C, any non-zero value is treated as true, and 0 is treated as false.
- Relational Operators:
- <, >, <=, >=, ==, != are commonly used in conditions.
- Logical Operators:
Combining Multiple Conditions: - && (AND): True if both conditions are true.
- || (OR): True if at least one condition is true.
- ! (NOT): Inverts the condition.
- Best Practices:
- Always use braces {} for clarity, even if optional.
- Keep conditions simple and readable.
Nested if Statement
A nested if statement is an if statement placed inside another if statement. It is used when you need to check multiple conditions where one depends on the other.
Syntax
for (initialization1; condition1; increment/decrement1) { // Outer loop
for (initialization2; condition2; increment/decrement2) { // Inner loop
// Code to execute inside the inner loop
}
// Code to execute after the inner loop but inside the outer loop
}
Example 1: Evaluate if a student passes based on marks, and determine if they are eligible for a distinction.
#include <stdio.h>
int main() {
int marks;
printf("Enter your marks: ");
scanf("%d", &marks);
if (marks >= 40) {
printf("You passed.\n");
if (marks >= 75) {
printf("Congratulations! You scored a distinction.n");
}
} else {
printf("You failed.\n");
}
return 0;
}
Output
Enter your marks: 80
You passed.
Congratulations! You scored a distinction.
Enter your marks: 30
You failed.
Example 2: Categorize a number as positive, negative, or zero, and check if it is even.
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num > 0) {
printf("The number is positive.\n");
if (num % 2 == 0) {
printf("It is also even.\n");
} else {
printf("It is also odd.\n");
}
} else if (num < 0) {
printf("The number is negative.\n");
} else {
printf("The number is zero.\n");
}
return 0;
}
Output
Enter a number: 10
The number is positive.
It is also even.
Enter a number: -5
The number is negative.
Enter a number: 0
The number is zero.
Key points
- Execution Flow:
- The inner if executes only if the outer if condition is true.
- If the outer if is false, the program skips all nested code.
- Readability:
- Avoid too many levels of nesting as it reduces readability.
- For deeply nested logic, consider using logical operators (&&, ||) to combine conditions.
- Braces:
- Always use braces {} in nested if statements for clarity.