Aim
To understand Boolean logic, relational and logical operators
(&&, ||, !), and
if-else conditional branching in the C programming language.
Theory
True and False in C
In C, conditions are represented as integers. The integer 0 means false,
and any non-zero value is treated as true. When C evaluates a relational or logical
expression, the result is always either 1 (true) or 0 (false).
Relational Operators
Relational operators compare two values and produce 1 if the comparison
holds and 0 if it does not. The six relational operators in C are:
== (equal to), != (not equal to), < (less than),
> (greater than), <= (less than or equal to), and
>= (greater than or equal to).
Logical Operators
Logical operators combine or negate conditions:
&&(Logical AND): Evaluates to1only when both operands are true.||(Logical OR): Evaluates to1if at least one operand is true.!(Logical NOT): Inverts a condition (!1is0, and!0is1).
| A | B | A && B | A || B | !A |
|---|---|---|---|---|
| 1 (True) | 1 (True) | 1 (True) | 1 (True) | 0 (False) |
| 1 (True) | 0 (False) | 0 (False) | 1 (True) | 0 (False) |
| 0 (False) | 1 (True) | 0 (False) | 1 (True) | 1 (True) |
| 0 (False) | 0 (False) | 0 (False) | 0 (False) | 1 (True) |
Short-Circuit Evaluation
C evaluates && and || from left to right and stops as
soon as the final truth value is known. For A && B, if A
is false (0), the whole expression must be false, so B is
skipped. For A || B, if A is true (non-zero), the whole
expression must be true, so B is skipped.
Conditional Branching (if-else)
An if-else statement chooses between two code blocks based on a condition.
If the condition evaluates to a non-zero value, the if block runs; otherwise,
the else block runs.
if (condition) {
/* runs when condition is true (non-zero) */
} else {
/* runs when condition is false (0) */
}
Objective
- Write valid conditional expressions in C using relational operators.
- Combine multiple conditions using logical operators (
&&and||) with proper parentheses. - Handle edge cases in decision logic, such as century years in leap year checks.
- Trace step-by-step Boolean evaluations and verify outputs using a trace table.
Procedure
- Open the Simulation tab from the left sidebar.
- Select one of the three C programs: Odd or Even, Leap Year Checker, or Scholarship Eligibility.
- Enter your test input values into the provided input field(s).
- Click Run to execute the condition check.
- Observe the step-by-step Boolean evaluation, the final output line, and the recorded row in the Trace Table.
- Try several test cases, including boundary cases, and use Reset Table if you want to clear the trace history.
- After completing the simulations, open the Quiz tab to test your understanding.
Simulation
Program 1: Odd or Even
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
if (n % 2 == 0) {
printf("%d is Even\n", n);
} else {
printf("%d is Odd\n", n);
}
return 0;
}
Step-by-Step Evaluation
Trace Table
| Run # | Input n | n % 2 | n % 2 == 0 | Output |
|---|
Program 2: Leap Year Checker
#include <stdio.h>
int main() {
int year;
scanf("%d", &year);
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
printf("%d is a Leap Year\n", year);
} else {
printf("%d is Not a Leap Year\n", year);
}
return 0;
}
Step-by-Step Boolean Breakdown
Trace Table
| Run # | Year | % 4 == 0 | % 100 != 0 | Left Side (&&) | Right Side (% 400 == 0) | Final (||) | Output |
|---|
Program 3: Scholarship Eligibility
#include <stdio.h>
int main() {
float gpa, income;
scanf("%f %f", &gpa, &income);
if (gpa >= 9.0 || (gpa >= 6.5 && income <= 3.0)) {
printf("Eligible for Scholarship\n");
} else {
printf("Not Eligible for Scholarship\n");
}
return 0;
}
Step-by-Step Boolean Breakdown
Trace Table
| Run # | GPA | Income (L) | Merit (>= 9.0) | Need (>= 6.5 && <= 3.0) | Eligible | Output |
|---|
Quiz
Select one answer for each question and click Submit Quiz to check your score.
Conclusion
In this lab, we saw how C evaluates relational and logical expressions to
1 (True) or 0 (False) and uses those results to choose between
branches in if-else statements. Testing the Odd or Even program showed how a
single modulus check separates integers cleanly into two branches.
Tracing the Leap Year Checker highlighted why edge cases matter: century years such as
1900 pass the divisibility-by-4 check but fail the compound condition because
they are divisible by 100 and not by 400. Finally, the Scholarship Eligibility program
demonstrated how compound rules using || and && work
together, including how short-circuit evaluation skips the second track once the merit
condition is already satisfied.