`else` — C++ Keyword

`else` — C++ Keyword

The else keyword in C++: provides the alternative branch of an if statement.

How to use this reference page

Use reference pages to confirm names, categories, nearby facilities, and the constraints that matter before writing or reviewing code.

  • Scan the top of the page first to identify the primary types, functions, or algorithm families involved.
  • Use the nearby-page links when your question is really about a companion header, related algorithm family, or broader subsystem.
  • Validate tricky behavior with a small compileable example before relying on memory for details like invalidation, ordering, allocation, or lifetime rules.

else

Provides the alternative branch of an if statement, executed when the condition is false.

Syntax

if (condition) statement1
else statement2

if (condition) statement1
else if (condition2) statement2
else statement3

Example

#include <print>

int main() {
    int score = 72;

    if (score >= 90) {
        std::println("A");
    } else if (score >= 80) {
        std::println("B");
    } else if (score >= 70) {
        std::println("C");   // executed
    } else {
        std::println("F");
    }
}

Notes

Example in practice

int main() {
    // Pick one facility from this reference page.
    // Write the smallest program that exercises its main precondition,
    // complexity rule, or lifetime constraint before scaling up.
    return 0;
}