A switch
statement provides a means of checking an expression against various case
s. If there is a match, the code within starts to execute.
The break
keyword can be used to terminate a case
. If the break
keyword is missing from a case
, it will cause code execution to overflow to subsequent case
s.
The code within the default
block is executed when no other 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;}
In C++, loops repeatedly execute code as long as the provided condition is true
.
There are four main types of loops in C++:
while
loops: repeats a block of code as long as the given boolean condition is true
.do-while
loops: similar to while
loops, but run at least once.for
loops: repeats a block of code a specific number of times.for-each
loops: used to iterate through every item in an array or list-like structure.// while loopint count = 0;while (count <= 10) {std::cout << count;count++;}// do-while loopint price = 300;do {std::cout << "Too expensive!";} while (price > 500);// for loopfor (int i = 0; i <= 10; i++) {std::cout << i;}// for-each loopint fibonacci[5] = { 0, 1, 1, 2, 3 };for (auto number:fibonacci){std::cout << number;}
if
StatementAn if
statement is used to test an expression for truth.
true
, then the code within the block is executed; otherwise, it will be skipped.if (a == 10) {// Code goes here}
else
ClauseAn else
clause can be added to an if
statement.
true
, code in the if
part is executed. false
, code in the else
part is executed.if (year == 1991) {// This runs if it is true}else {// This runs if it is false}
Relational operators are used to compare two values and return true
or false
depending on the comparison:
==
equal to
!=
not equal to
>
greater than
<
less than
>=
greater than or equal to
<=
less than or equal to
if (a > 10) {// ☝️ means greater than}
else if
StatementOne 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}
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 is true
.else
statements execute code only if the provided condition in the if
statement is false
.else if
statements can be added in between the if
and else
to provide additional condition(s) to check.Some useful tricks with conditional statements:
if
-else
expression into a single statement using the following syntax:variable = (condition) ? condition_is_true : condition_is_false;
{
}
may be omitted if there is only a single statement inside a conditional statement.int temperature = 60;if (temperature < 65) {std::cout << "Too cold!";}else if (temperature > 75) {std::cout << "Too hot!";}else // brackets may be omitted herestd::cout << "Just right...";
In C++, the break
keyword is used to exit a switch or loop.
The continue
keyword is used to skip an iteration of a loop.
// Prints: 0123for (int i = 0; i < 10; i++) {if (i == 4) {break;}std::cout << i;}// Prints: 012356789for (int i = 0; i < 10; i++) {if (i == 4) {continue;}std::cout << i;}