C++ Conditionals
Conditionals take an expression, which is code that evaluates to determine a value, and checks if it is true or false. If the expression results in true, the program can be instructed to do one thing — false can even be accounted for to perform another set of instruction(s).
For more complex programs, conditionals allow multiple scenarios to be addressed, making programs more robust.
If Statement
An if statement is used to test an expression for truth.
If the condition evaluates to true, then the code within the block is executed; otherwise, it will be skipped.
if (a == 10) {// Code goes here}
Else Clause
An else clause can be added to an if statement:
- If the condition evaluates to
true, code in theifpart is executed. - If the condition evaluates to
false, code in theelsepart is executed.
if (year == 1991) {// This runs if it is true}else {// This runs if it is false}
Else If Statement
One or more else if statements can be added in between the if and else to provide additional condition(s) to check.
if (apple > 8) {// Some code here}else if (apple > 6) {// Some code here}else {// Some code here}
Codebyte Example
Switch Statement
A switch statement provides a means of checking an expression against various cases. If there is a match, the code within starts to execute. The break keyword can be used to terminate a case.
default is executed when no case matches.
switch (grade) {case 9:std::cout << "Freshman\n";break;case 10:std::cout << "Sophomore\n";break;case 11:std::cout << "Junior\n";break;case 12:std::cout << "Senior\n";break;default:std::cout << "Invalid\n";break;}
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn C++ on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.
- Beginner Friendly.11 hours