C++ For Programmers - Control Flow in C++ Cheatsheet - Codecademy
C++ For Programmers - Control Flow in C++ Cheatsheet - Codecademy
Switch Statements
https://www.codecademy.com/learn/c-plus-plus-for-programmers/modules/control-flow-cpp/cheatsheet 1/5
10/7/24, 1:42 PM C++ for Programmers: Control Flow in C++ Cheatsheet | Codecademy
Loops
// for loop
for (int i = 0; i <= 10; i++) {
std::cout << i;
}
// for-each loop
int fibonacci[5] = { 0, 1, 1, 2, 3 };
for (auto number:fibonacci){
std::cout << number;
}
if Statement
https://www.codecademy.com/learn/c-plus-plus-for-programmers/modules/control-flow-cpp/cheatsheet 2/5
10/7/24, 1:42 PM C++ for Programmers: Control Flow in C++ Cheatsheet | Codecademy
else Clause
Relational Operators
Relational operators are used to compare two values and if (a > 10) {
return true or false depending on the comparison:
// ☝️ means greater than
== equal to
!= not equal to }
> greater than
< less than
>= greater than or equal to
<= less than or equal to
else if Statement
https://www.codecademy.com/learn/c-plus-plus-for-programmers/modules/control-flow-cpp/cheatsheet 3/5
10/7/24, 1:42 PM C++ for Programmers: Control Flow in C++ Cheatsheet | Codecademy
Conditional Statements
Conditional statements are used to control the flow of int temperature = 60;
code execution by testing for a condition for truth.
if statements execute code only if the provided
condition is true . if (temperature < 65) {
else statements execute code only if the std::cout << "Too cold!";
provided condition in the if statement is false .
}
one or more else if statements can be added in
else if (temperature > 75) {
between the if and else to provide additional
condition(s) to check. std::cout << "Too hot!";
Some useful tricks with conditional statements: }
It is possible to condense an if - else expression
else // brackets may be omitted here
into a single statement using the following syntax:
std::cout << "Just right...";
variable = (condition) ? condition_
https://www.codecademy.com/learn/c-plus-plus-for-programmers/modules/control-flow-cpp/cheatsheet 4/5
10/7/24, 1:42 PM C++ for Programmers: Control Flow in C++ Cheatsheet | Codecademy
// Prints: 012356789
for (int i = 0; i < 10; i++) {
if (i == 4) {
continue;
}
std::cout << i;
}
Print Share
https://www.codecademy.com/learn/c-plus-plus-for-programmers/modules/control-flow-cpp/cheatsheet 5/5