Learn
So what happens if you want more than two possible outcomes?
This is where else if
comes in!
if (condition) {
some code
} else if (condition) {
some code
} else {
some code
}
The else if
statement always comes after the if
statement and before the else
statement. The else if
statement also takes a condition.
And you can have more than one of them! Here’s an example with three of them:
if (grade == 9) { std::cout << "Freshman\n"; } else if (grade == 10) { std::cout << "Sophomore\n"; } else if (grade == 11) { std::cout << "Junior\n"; } else if (grade == 12) { std::cout << "Senior\n"; } else { std::cout << "Super Senior\n"; }
Instructions
1.
In chemistry, pH is a scale used to specify the acidity or basicity of an aqueous solution.
Write an if
, else if
, else
statement that:
- If
ph
is greater than 7, output “Basic”. - If
ph
is less than 7, output “Acidic”. - If neither, output “Neutral”.
Remember to take a look at the hint if you are stuck.
Take this course for free
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.