C++ Else If Statement

In C++, the else if statement allows programs to evaluate multiple conditions sequentially. If the first condition is false, the program moves to the next condition. This ensures different outcomes based on varying conditions.

Tutorials dojo strip



How the Else If Statement Works

When an if condition fails, an else if statement provides another chance to check for a different condition. If all conditions are false, the else block executes.

Syntax

C++




Example: Evaluating Speed Zones

The following program determines a message based on a vehicle’s speed.

C++

Explanation of the Example:

  • speed = 90
  • The first condition (speed < 40) is false, so it skips that block.
  • The else if condition (speed < 100) is true, so "You're cruising at a safe speed." gets printed.
  • If speed were 120, neither the if nor else if would be true, so the else block would execute.
Tutorials dojo strip