Skip to content
Justice Tech School

If ... Else

if runs code when a condition is true. Use else if for more cases and else for the rest. A short form is the ternary operator: condition ? a : b.

Example
int score = 72;
if (score >= 85) {
  printf("Distinction\n");
} else if (score >= 70) {
  printf("Pass\n");
} else {
  printf("Retake\n");
}
printf("%s\n", score >= 50 ? "ok" : "low");

Result

Pass
ok

Exercise

Run when the condition is false.

if (x) {}  {}

Quiz Test

Which is the ternary operator?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.