Article by Ayman Alheraki on January 11 2026 10:37 AM
if, else, switchConditional statements allow programs to execute different blocks of code based on boolean expressions. They form the foundation of decision-making in programming. Both C++ and Rust provide similar constructs but with important syntactic and semantic differences reflecting their language philosophies.
if and else StatementsThe if statement evaluates a condition; if the condition is true (non-zero), the following block executes; otherwise, an optional else block executes (cppreference).
Syntax:
if (condition) { // code if condition is true} else { // code if condition is false}Conditions must be convertible to bool; implicit conversion from integral or pointer types is allowed. Zero or nullptr evaluates as false, non-zero as true.
Nested if-else and else if chains are common.
Since C++17, if statements can include an initializer, introducing a new variable with limited scope:
if (int x = foo(); x > 0) { // use x here}This pattern enhances code clarity and limits variable scope to the if block (cppreference if statement).
In Rust, if is an expression and must evaluate to a boolean (bool) — no implicit conversion from integers or other types is allowed (Rust Reference).
Syntax:
if condition { // code if true} else { // code if false}Because if is an expression, it returns a value, allowing:
let x = if condition { 5 } else { 10 };There is no traditional ternary operator in Rust (?: in C++); instead, if expressions serve this role.
Rust requires explicit boolean conditions, enhancing type safety and reducing bugs.
switch StatementThe switch statement allows multi-way branching based on integral or enumeration types (cppreference).
Syntax:
switch (expression) { case value1: // code break; case value2: // code break; default: // code}Important features:
switch works only with integral, enumeration, or constexpr values convertible to integral types.
Each case label must be a compile-time constant.
Fallthrough between cases occurs unless explicitly broken with break.
Since C++17, [[fallthrough]]; attribute can document intentional fallthrough.
switch provides efficient jump table or binary search implementations by compilers.
Rust does not have a switch statement. Instead, it provides a more powerful match expression (Rust Reference).
match allows pattern matching on values of many types, not just integers.
Syntax:
match value { pattern1 => { /* code */ }, pattern2 => { /* code */ }, _ => { /* default case */ },}Features:
Exhaustiveness checking: all possible cases must be handled or covered by a wildcard _.
Patterns can be literals, ranges, enums, or destructured data.
match is an expression and returns a value.
Prevents bugs common in switch, such as missing cases or accidental fallthrough.
| Feature | C++ (if, else, switch) | Rust (if, else, match) |
|---|---|---|
| Condition type | Implicit conversions to bool allowed | Requires explicit bool condition |
if as expression | No (statement only) | Yes (returns value) |
| Ternary operator | Yes, ?: | No ternary; use if expressions |
| Multi-branching | switch supports integral/enums only | match supports pattern matching on many types |
| Fallthrough | Allowed by default; must use break to prevent | No fallthrough; exhaustive match required |
| Exhaustiveness | No compiler checks for missing cases | Compiler enforces exhaustive pattern matching |
| Variable binding | C++17 allows initializer in if | if and match allow variable bindings in patterns |
Use if and else for simple conditional branches in both languages.
Prefer switch in C++ for multiple discrete integer or enum cases with care for break statements.
Use Rust’s match for powerful, safe, and exhaustive multi-way branching that can destructure complex data types.
Exploit C++17's if initializer to limit scope of variables used in conditions.
Rust’s strict boolean conditions and exhaustive matching reduce runtime errors and improve code safety.
C++ if statement — cppreference
https://en.cppreference.com/w/cpp/language/if
C++ switch statement — cppreference
https://en.cppreference.com/w/cpp/language/switch
ISO C++17 Standard (for if initializer and [[fallthrough]])
https://isocpp.org/std/the-standard
Rust if expression — Rust Reference
https://doc.rust-lang.org/reference/expressions/if-expr.html
Rust match expression — Rust Reference
https://doc.rust-lang.org/reference/expressions/match-expr.html
Rust Book: Control Flow https://doc.rust-lang.org/book/ch03-05-control-flow.html