- 1 1. Introduction
- 2 2. Basic Structure of the if Statement in C
- 3 3. Types and Roles of Logical Operators in C
- 4 4. How to Combine Multiple Conditions in C
- 5 5. Detailed Conditional Branching with else if
- 6 6. Advanced Conditional Branching with Nested if Statements
- 7 7. Practical Example: Sample Program with Multiple Conditions
- 8 8. Frequently Asked Questions (FAQ)
- 9 9. Summary
1. Introduction
The C language is widely used as a simple yet powerful programming language. Among its many features, “conditional branching” is essential for controlling program behavior. In particular, knowing how to use the if
statement to handle multiple conditions is crucial for implementing complex logic.
In this article, we will explain, in an easy-to-understand manner even for beginners, how to specify multiple conditions with the if
statement in C. We cover everything from the basic structure to advanced examples, so feel free to use this as a reference.
2. Basic Structure of the if Statement in C
What is an if statement?
An if
statement evaluates whether a specified condition is true, and if it is, executes a specific block of code. In C, the syntax for an if
statement is written as follows:
if (condition) {
// Code executed if the condition is true
}
Basic Example
The following is an example of an if
statement that checks whether the variable x
has a positive value:
#include <stdio.h>
int main() {
int x = 10;
if (x > 0) {
printf("x is a positive value.\n");
}
return 0;
}
In this code, printf
is executed if x
is greater than 0.
Key Points for if Statement Conditions
- You can combine numbers, variables, and logical operators (
&&
,||
,!
) in the condition. - In C, any nonzero value is considered “true,” and 0 is considered “false.”
3. Types and Roles of Logical Operators in C
What are logical operators?
When working with multiple conditions, you need to combine them using logical operators. C provides three main logical operators:
Logical AND (&&
)
- Evaluates to true only if both conditions are true.
- Example: Check if variable
a
is between 10 and 20 inclusive.
if (a >= 10 && a <= 20) {
printf("a is between 10 and 20.\n");
}
Logical OR (||
)
- Evaluates to true if at least one of the conditions is true.
- Example: Check if variable
b
is either 0 or 100.
if (b == 0 || b == 100) {
printf("b is either 0 or 100.\n");
}
Logical NOT (!
)
- Reverses the condition: “false” becomes “true,” and “true” becomes “false.”
- Example: Check if variable
c
is not equal to 0.
if (!(c == 0)) {
printf("c is not zero.\n");
}
Notes
- When combining logical operators, use parentheses to improve readability.
- Example:
(a > 0 && b < 10) || c == 5
4. How to Combine Multiple Conditions in C
What does combining multiple conditions mean?
In C, you can combine multiple conditions in an if
statement to express more complex logic concisely. When doing so, use logical operators (&&
, ||
) and parentheses to structure the conditions clearly.
Example of Combining Multiple Conditions
The following example checks if x
is between 10 and 20 inclusive, and y
is less than 30:
#include <stdio.h>
int main() {
int x = 15;
int y = 25;
if ((x >= 10 && x <= 20) && y < 30) {
printf("x is between 10 and 20, and y is less than 30.\n");
} else {
printf("Conditions not met.\n");
}
return 0;
}
Tips for Writing Readable Conditions
- Use parentheses
- Parentheses make the priority of conditions clear.
- Break conditions into parts
- Assign complex conditions to variables to keep the code concise.
5. Detailed Conditional Branching with else if
What is else if?
Using else if
allows you to evaluate multiple conditions sequentially and execute different code depending on the result.
Example of else if
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num > 0) {
printf("The number is positive.\n");
} else if (num < 0) {
printf("The number is negative.\n");
} else {
printf("The number is zero.\n");
}
return 0;
}
6. Advanced Conditional Branching with Nested if Statements
Example of Nested if
#include <stdio.h>
int main() {
int x = 10;
int y = 5;
if (x > 0) {
if (y > 0) {
printf("Both x and y are positive.\n");
} else {
printf("x is positive, but y is not.\n");
}
} else {
printf("x is not positive.\n");
}
return 0;
}
7. Practical Example: Sample Program with Multiple Conditions
Example 1: Checking if a Number is Within Range
#include <stdio.h>
int main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);
if (number >= 10 && number <= 20) {
printf("The number is between 10 and 20.\n");
} else {
printf("The number is out of range.\n");
}
return 0;
}
8. Frequently Asked Questions (FAQ)
Q1. Why aren’t multiple conditions working as expected?
- Cause: Not understanding the precedence of conditions correctly.
- Solution: Use parentheses to make your intent clear.
9. Summary
By mastering the use of if
statements with multiple conditions in C, you can write more flexible and efficient programs.