0% found this document useful (0 votes)
7 views5 pages

C++ For Programmers - Control Flow in C++ Cheatsheet - Codecademy

This document is a cheatsheet for control flow in C++, covering switch statements, loops, if statements, else clauses, relational operators, and conditional statements. It explains the syntax and usage of various control flow constructs, including examples for clarity. Additionally, it addresses the use of break and continue keywords in loops.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views5 pages

C++ For Programmers - Control Flow in C++ Cheatsheet - Codecademy

This document is a cheatsheet for control flow in C++, covering switch statements, loops, if statements, else clauses, relational operators, and conditional statements. It explains the syntax and usage of various control flow constructs, including examples for clarity. Additionally, it addresses the use of break and continue keywords in loops.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

10/7/24, 1:42 PM C++ for Programmers: Control Flow in C++ Cheatsheet | Codecademy

Cheatsheets / C++ for Programmers

Control Flow in C++

Switch Statements

A switch statement provides a means of checking an switch (grade) {


expression against various case s. If there is a match, the
case 9:
code within starts to execute.
The break keyword can be used to terminate a case . If std::cout << "Freshman\n";
the break keyword is missing from a case , it will cause break;
code execution to overflow to subsequent case s. case 10:
The code within the default block is executed when no
std::cout << "Sophomore\n";
other case matches.
break;
case 11:
std::cout << "Junior\n";
break;
case 12:
std::cout << "Senior\n";
break;
default:
std::cout << "Invalid\n";
break;
}

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

In C++, loops repeatedly execute code as long as the // while loop


provided condition is true .
int count = 0;
There are four main types of loops in C++:
1. while loops: repeats a block of code as long as while (count <= 10) {
the given boolean condition is true. std::cout << count;
2. do-while loops: similar to while loops, but run at count++;
least once.
3. for loops: repeats a block of code a specific
}
number of times.
4. for-each loops: used to iterate through every // do-while loop
item in an array or list-like structure.
int price = 300;
do {
std::cout << "Too expensive!";
} while (price > 500);

// 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

An if statement is used to test an expression for truth. if (a == 10) {


If the condition evaluates to true , then the code
// Code goes here
within the block is executed; otherwise, it will be
skipped. }

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

An else clause can be added to an if statement. if (year == 1991) {


If the condition evaluates to true , code in the
// This runs if it is true
if part is executed.
If the condition evaluates to false , code in the }
else part is executed. else {
// This runs if it is false
}

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

One or more else if statements can be added in if (apple > 8) {


between the if and else to provide additional
// Some code here
condition(s) to check.
}
else if (apple > 6) {
// Some code here
}
else {
// Some code here
}

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_

Curly brackets { } may be omitted if there is


only a single statement inside a conditional
statement.

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

Break and Continue

In C++, the break keyword is used to exit a switch or // Prints: 0123


loop.
for (int i = 0; i < 10; i++) {
The continue keyword is used to skip an iteration of a
loop. if (i == 4) {
break;
}
std::cout << i;
}

// 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

You might also like