C Conditions, if and Logical Operators
C Conditions, if and Logical Operators
A student passes when a mark is at least 40 and attendance is sufficient. Both requirements must hold before printing Passed.
An if statement chooses a branch according to whether its condition is zero or nonzero. Relational operators compare values; && requires both conditions and || accepts either condition.
Explain
Combine two requirements and predict which branch executes.
Trace
Keep 45 marks but lower attended days to 12. Predict the result.
Apply
Choose conditions that include the intended boundary mark.
Conditions: follow the data
Start
A student passes when a mark is at least 40 and attendance is sufficient. Both requirements must hold before printing Passed.
Operation
An if statement chooses a branch according to whether its condition is zero or nonzero. Relational operators compare values; && requires both conditions and || accepts either condition.
Result
Passed
Understand the operation
C treats zero as false and nonzero as true in a condition. Comparisons such as marks >= 40 produce an integer truth value. && and || evaluate left to right and short-circuit when the right side cannot change the result.
Use == to compare and = to assign. Braces make the branch boundaries clear even when each branch currently contains one statement. && is a logical test, while & is a bitwise operation with different behavior.
Read the complete C program
This complete C17 program uses a small school example. Read declarations before the statements that use their values. Each printf format describes how the corresponding value is written to standard output.
C17 · complete program
#include <stdio.h>
int main(void) {
int marks = 45;
int attended = 18;
if (marks >= 40 && attended >= 15) {
puts("Passed");
} else {
puts("Needs review");
}
return 0;
}
Expected output
Passed
Run in C compiler →Follow the values through the code
C17 · code used in this explanation
#include <stdio.h>
int main(void) {
int marks = 45;
int attended = 18;
if (marks >= 40 && attended >= 15) {
puts("Passed");
} else {
puts("Needs review");
}
return 0;
}
45 >= 40 is true, so the attendance test is evaluated.
18 >= 15 is also true. The program enters the first branch and prints Passed.
Follow the worked calculation
Read the starting values
45 >= 40 is true, so the attendance test is evaluated.
Perform the operation
18 >= 15 is also true. The program enters the first branch and prints Passed.
Check the observable result
Passed
Passed
Try a small variation
C17 · code used in this explanation
#include <stdio.h>
int main(void) {
int marks = 45;
int attended = 18;
if (marks >= 40 && attended >= 15) {
puts("Passed");
} else {
puts("Needs review");
}
return 0;
}
Keep 45 marks but lower attended days to 12. Predict the result.
Worked answer
The second requirement fails, so && produces false even though the mark is sufficient. The else branch explains that the combined rule was not met.
C17 · complete program
#include <stdio.h>
int main(void) {
int marks = 45;
int attended = 12;
if (marks >= 40 && attended >= 15) {
puts("Passed");
} else {
puts("Needs review");
}
return 0;
}
Expected output
Needs review
Run in C compiler →Recognize the important boundaries
A true comparison does not automatically validate that the original mark was in its allowed range. Reject invalid values before applying a pass rule.
Avoid writing 0 <= marks <= 100 in C. Use marks >= 0 && marks <= 100, because comparisons are evaluated as separate expressions.
Check your understanding
Choose the answer, then compare the reasoning for every option.
Use the idea independently
An if statement chooses a branch according to whether its condition is zero or nonzero. Relational operators compare values; && requires both conditions and || accepts either condition.
Continue with repeating operations.
Language reference: WG14 C11 committee draft. These examples use the C17-compatible subset of the explained language features.
Reaching the recap and completing the quizzes are separate milestones.