Control flow · 05
Conditions
Do different things depending on a condition: if · else if · else, plus switch.
A conditional is "if this, then that" in code.
Conditions are checked from the top, and only the first branch that is true runs.
The example below turns a score into a grade and also says whether it is even.
score = int(input())if score >= 90: print("A")elif score >= 80: print("B")elif score >= 70: print("C")else: print("F")print("even" if score % 2 == 0 else "odd")const score = Number(require("fs").readFileSync(0, "utf8"));if (score >= 90) { console.log("A");} else if (score >= 80) { console.log("B");} else if (score >= 70) { console.log("C");} else { console.log("F");}console.log(score % 2 === 0 ? "even" : "odd");import { readFileSync } from "node:fs";const score: number = Number(readFileSync(0, "utf8"));if (score >= 90) { console.log("A");} else if (score >= 80) { console.log("B");} else if (score >= 70) { console.log("C");} else { console.log("F");}console.log(score % 2 === 0 ? "even" : "odd");import java.util.Scanner; public class Main { public static void main(String[] args) { int score = new Scanner(System.in).nextInt(); if (score >= 90) { System.out.println("A"); } else if (score >= 80) { System.out.println("B"); } else if (score >= 70) { System.out.println("C"); } else { System.out.println("F"); } System.out.println(score % 2 == 0 ? "even" : "odd"); }}using System; class Program{ static void Main() { int score = int.Parse(Console.ReadLine()); if (score >= 90) Console.WriteLine("A"); else if (score >= 80) Console.WriteLine("B"); else if (score >= 70) Console.WriteLine("C"); else Console.WriteLine("F"); Console.WriteLine(score % 2 == 0 ? "even" : "odd"); }}#include <iostream> int main() { int score; std::cin >> score; if (score >= 90) { std::cout << "A\n"; } else if (score >= 80) { std::cout << "B\n"; } else if (score >= 70) { std::cout << "C\n"; } else { std::cout << "F\n"; } std::cout << (score % 2 == 0 ? "even" : "odd") << '\n';}#include <stdio.h> int main(void) { int score; scanf("%d", &score); if (score >= 90) { puts("A"); } else if (score >= 80) { puts("B"); } else if (score >= 70) { puts("C"); } else { puts("F"); } puts(score % 2 == 0 ? "even" : "odd"); return 0;}score = Console.ReadLine().ToNumber();if (score >= 90){ Console.WriteLine("A");}else if (score >= 80){ Console.WriteLine("B");}else if (score >= 70){ Console.WriteLine("C");}else{ Console.WriteLine("F");}Console.WriteLine(score % 2 == 0 ? "even" : "odd");75
C odd
Key points
- Conditions are checked from the top, so put the strictest test first.
Ifscore >= 70comes first, even 95 becomes C. - If every condition is false, the
elsebranch runs.elseis optional. - Logical operators join conditions: and
&&, or||, not!.
Python usesand,or,not.
Once the answer is known, the rest is skipped (x != 0 && 10 / x > 1is safe). - To compare one value against many cases, switch is easier to read.
Languages differ on whether a case "falls through" into the next one. - A short either/or can use the ternary operator:
cond ? if true : if false(Python:a if cond else b).
How each language writes it
| Python | if cond: / elif cond: / else: — : and indentation instead of braces.switch since 3.10: match x: + case 1 | 2: + case _: |
|---|---|
| JavaScript | if (cond) { } else if (cond) { } else { } — in switch, a case without break falls through to the next case.Values are compared with === |
| TypeScript | Same as JavaScript. Comparing values of different types is caught at compile time |
| Java | if (cond) { } else if (cond) { } else { } — switch needs break.Since Java 14, case 1, 2 -> … does not fall through |
| C# | if (cond) { } else if (cond) { } else { } — in switch, every non-empty case must end with break (or return); leaving it out is a compile error |
| C++ | if (cond) { } else if (cond) { } else { } — switch works only on integers (characters included); without break it falls through to the next case |
| C | Same as C++. True and false are any non-zero number and 0 |
| Craykon | if (cond) { } else if (cond) { } else { } — switch (x) { case 1, 2: … default: … } runs only the matching case and never falls through |
Common mistakes
Chaining 1 < x < 10JavaScript · C · C++ · Craykon
let x = 50;
console.log(1 < x < 10);→ true — (1 < x) becomes true first, and true < 10 is true
Split it: 1 < x && x < 10.
Only Python chains comparisons like math (TypeScript, Java and C# give a compile error).
= instead of ==C · C++ · JavaScript · TypeScript
if (x = 5) {
printf("five");
}→ 5 is stored in x, and the condition is always true
Compare with ==.
Java, C#, Python and Craykon report this mistake as an error.
Forgetting break in a switchC · C++ · Java · JavaScript
switch (n) {
case 1: puts("one");
case 2: puts("two");
}→ When n is 1, both "one" and "two" are printed
End each case with break;.
C# makes this a compile error, and Craykon's switch never falls through in the first place.
In JavaScript and TypeScript, use
=== for equality: == converts types first, so "1" == 1 and "" == 0 are both true.Craykon's
== is strict like ===, so "1" == 1 is false.