Log in from a computer to take this course

You'll need to log in from a computer to start Learn C++. But you can practice or keep up your coding streak with the Codecademy Go app. Download the app to get started.

apple storegoogle store

We can also add an else clause to an if statement to provide code that will only be executed if the condition is false. Here’s a form of an if statement that includes an else clause:

if (condition) {

  do something

} else {

  do something else

  • If condition is true, statement1 is executed. Then the program skips statement2 and executes any code statements following the if/else clause.
  • If condition is false, statement1 is skipped and statement2 is executed. After statement2 completes, the program executes any code statements following the if/else clause.
if (coin == 1) { std::cout << "Heads\n"; } else { std::cout << "Tails\n"; }

So in the code above, if coin is equal to 1, the program outputs “Heads”; if it does not, then it outputs “Tails”.

Note: It’s either or — only one of them will execute!



Add an else statement that outputs “Fail”.

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?