Skip to main content

Conditionals

Conditional statements allow your program to make decisions. Here's how if/else and switch statements work across different languages.

If/Else Statements​

// Basic if/else
if (age >= 18) {
console.log("Adult");
} else {
console.log("Minor");
}

// Ternary operator
const status = age >= 18 ? "Adult" : "Minor";

// Nested ternary
const result = x > 0 ? (y > 0 ? "Both positive" : "X positive") : "X not positive";

// Chained ternary
const grade = score >= 90 ? "A" : score >= 80 ? "B" : "C";

// Else if
if (score >= 90) {
grade = "A";
} else if (score >= 80) {
grade = "B";
} else {
grade = "C";
}

Switch/Case Statements​

// Switch statement
switch (day) {
case 1:
console.log("Monday");
break;
case 2:
console.log("Tuesday");
break;
default:
console.log("Other day");
}

// Switch expression (no break needed)
switch (day) {
case 1: return "Monday";
case 2: return "Tuesday";
default: return "Other";
}

Key Takeaways​

  • If/else syntax -- C-family languages (C, C++, Java, JavaScript, C#) use braces and else if; Python uses elif with colon-and-indent; Ruby uses elsif and end; PHP uses elseif (one word). The Python-style if cond: without braces reduces noise but requires consistent indentation. If you prefer explicit block delimiters and come from C/Java, choose a brace-based language; if you value conciseness and readability, Python or Ruby may feel cleaner.

  • Switch vs match -- C-style switch (C, C++, Java, JavaScript, PHP) requires break to prevent fallthrough; forgetting it is a common bug. Rust's match is expression-based—it returns a value and has no fallthrough, so let x = match n { 1 => "one", _ => "other" }; is idiomatic. Python 3.10+ and C# 8+ add match/switch expressions with pattern matching. For multi-branch logic, prefer Rust or Swift match when exhaustiveness matters; use C-style switch when you need explicit fallthrough or work in legacy codebases.

  • Ternary operators -- Most languages use cond ? a : b (C, C++, Java, JavaScript, C#, Swift, PHP). Go deliberately omits ?: and uses if cond { a } else { b } for assignment, favoring clarity over brevity. Python uses a if cond else b (condition in the middle). Rust and Zig use if as expressions: let x = if cond { a } else { b };. If you want compact inline conditionals, use ternary; if you prefer Go-style explicitness, avoid nested ternaries and favor simple if/else blocks.

  • Expression vs statement -- In Rust, Zig, and Ruby, if can be an expression that yields a value; C, Java, and Go treat if as a statement only. JavaScript uses ternary for expressions. This affects how you assign conditionally: Rust allows let grade = if score >= 90 { "A" } else { "B" }; without a separate variable and multiple assignments. Choose expression-based languages when you want to avoid mutable state and write more declarative code.

  • When to choose which -- For systems programming or maximum explicitness, Go's if/else and lack of ternary reduce surprises. For functional-style, expression-oriented code, Rust and Zig shine. For rapid prototyping and readability, Python's elif and a if cond else b are approachable. For enterprise or web backends, C#, Java, and PHP offer familiar C-style conditionals with good tooling.