{ }codelper

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.

42 problemsLevel 1–4Solve in C

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

  1. A photo fitted to a square frame8P0254
  2. Three-day temperature trend8P0227
  3. Palindrome check8P0004
  4. Video call grid8P0424
  5. Member discount and delivery fee8P0191
  6. Second largest8P0031
  7. Admission by completed years of age8P0169
  8. Progressive income tax8P0137
  9. Trading robot: one action a day8P0330
  10. Comparing products rule by rule8P0301
  11. 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

  1. Letter grades8P0011
  2. Clap game8P0002
  3. Kite-flying notice board8P0315
  4. Bike path slope signs8P0420
  5. Parcel rate table8P0167
  6. Hands of five dice6P0198

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.

  1. Leap year8P0019
  2. Count the vowels8P0009
  3. Password rule checker8P0098
  4. Due dates that skip closed days8P0431
  5. Checking school trip forms8P0324
  6. The longest stretch typed with one hand8P0269
  7. The date some days later or earlier8P0170
  8. Checking product codes against a template8P0273

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

  1. Four-operation calculator8P0021
  2. Which way the turtle faces6P0077
  3. Transposing a melody by k semitones8P0401
  4. Order state machine6P0156
  5. TV remote keys8P0242
  6. Multi-tap phone keypad8P0429

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.

  1. Locker extra fee8P0372
  2. Where is the square root8P0049
  3. Switching between AM/PM and 24-hour times8P0422
  4. Multi-month calendar8P0126
  5. Darts countdown game8P0140
  6. Retry with backoff8P0138

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.

  1. Middle of three8P0020
  2. Parking fee calculator8P0136
  3. Vending machine change8P0127
  4. Following an ATM's state8P0139
  5. Imitating Python's a:b:c slicing8P0274

Good to know

See also