Control Flow in C++
An overview of control flow in C++.
StartKey Concepts
Review core concepts you need to learn to master this subject
Conditional Statements
Switch Statements
Loops
Break and Continue
if
Statement
else
Clause
Relational Operators
else if
Statement
Conditional Statements
Conditional Statements
int temperature = 60;
if (temperature < 65) {
std::cout << "Too cold!";
}
else if (temperature > 75) {
std::cout << "Too hot!";
}
else // brackets may be omitted here
std::cout << "Just right...";
Conditional statements are used to control the flow of code execution by testing for a condition for truth.
if
statements execute code only if the provided condition istrue
.else
statements execute code only if the provided condition in theif
statement isfalse
.- one or more
else if
statements can be added in between theif
andelse
to provide additional condition(s) to check.
Some useful tricks with conditional statements:
- It is possible to condense an
if
-else
expression into a single statement using the following syntax:variable = (condition) ? condition_is_true : condition_is_false; - Curly brackets
{
}
may be omitted if there is only a single statement inside a conditional statement.
What you'll create
Portfolio projects that showcase your new skills