C if-else Practice Problems
This set is conditionals only. It has 42 problems, from the basic if and else branches to switch, the ternary operator and nested conditions.
1. Comparisons and if / else #1–11
C treats a condition as a number: 0 is false and everything else is true.
Writing =, as in if (x = 5), is an assignment and is always true, so always check that you wrote ==.
Wrapping the body in { } even when it is a single line is a habit that cuts down on mistakes.
Syntax refresher: C if Statement: if, else if, else
- A photo fitted to a square frame8P0254
- Three-day temperature trend8P0227
- Palindrome check8P0004
- Video call grid8P0424
- Member discount and delivery fee8P0191
- Second largest8P0031
- Admission by completed years of age8P0169
- Progressive income tax8P0137
- Trading robot: one action a day8P0330
- Comparing products rule by rule8P0301
- Picking the restaurant that disappoints no one much8P0334
2. Several branches with else if #12–17
The conditions are checked from the top in order, and only the first branch that is true runs.
So if you write the narrowest condition first, as in 90 or above → 80 or above → 70 or above, you can split the ranges without &&.
Syntax refresher: C if Statement: if, else if, else
3. Logical operators and membership #18–25
&& means and, || means or, and ! means not.0 <= x < 10 is not read the way it is in maths.
It is calculated as (0 <= x) < 10, which is always true, so you have to write 0 <= x && x < 10.
4. switch #26–31
Use it when the code branches on the value of a single integer or character.
Without a break in a case, execution carries on into the case below.
Leave break out only when you mean to group several case labels.
Values that match nothing are caught by default.
Syntax refresher: C switch Statement: case, break, default
5. The ternary operator #32–37
condition ? value if true : value if false is an expression that picks a value.
It suits short choices such as printf("%s\n", n % 2 ? "odd" : "even");.
Nested, it gets hard to read, so use if in that case.
6. Nested conditions #38–42
When you put an if inside an if, an else pairs with the nearest if.
The pairing is decided by { }, not by indentation, so always use braces when you nest.
Good to know
- Try the boundary values yourself. Whether the problem says "at least" or "more than" decides between
>=and>. - If a function that returns a value only has a
returnon one branch of a condition, you get the warning 'control reaches end of non-void function'.
See also
- C Loop Practice Problems: for, while, do-while — 50 problems
- 100 C Programming Practice Problems for Beginners — 100 problems
- C Array and String Practice Problems — 81 problems
- Learn C — lessons with explanations and examples
- C reference
- C error messages
- Other problem sets