Lesson 5 of 31
Conditionals
Conditionals
C uses if, else if, and else for conditional execution.
if / else
int x = 10;
if (x > 0) {
printf("positive\n");
} else if (x < 0) {
printf("negative\n");
} else {
printf("zero\n");
}
Red alert conditions: "Shields up!" only triggers when sensors detect a threat. That's an
ifstatement protecting the ship.
Comparison Operators
| Operator | Meaning |
|---|---|
== | Equal to |
!= | Not equal to |
< | Less than |
> | Greater than |
<= | Less than or equal |
>= | Greater than or equal |
Logical Operators
| Operator | Meaning |
|---|---|
&& | Logical AND |
|| | Logical OR |
! | Logical NOT |
The Ternary Operator
A compact way to write simple conditionals:
int x = 5;
const char *label = (x > 0) ? "positive" : "non-positive";
Your Task
Write a function classify that takes an int and prints "positive", "negative", or "zero" followed by a newline. Call it three times from main with the values 42, -7, and 0.
TCC compiler loading...
Loading...
Click "Run" to execute your code.