- Write
if,else ifandelsewith comparison and logical operators - Choose between several options with
switch - Repeat work with
for,whileanddo-whileloops - Control loops with
breakandcontinue
In a game, “Game over” appears when the character's health drops to zero, and the screen is redrawn in every frame. The first is a condition — the program makes a decision based on the situation. The second is a loop — the same work is done again and again. No serious program can be written without these two tools.
if, else if and else
After the if keyword you write a condition in parentheses. If the condition is true, the block in curly braces runs. else if checks the next condition, and else runs when none of the conditions holds. Conditions are checked from top to bottom, and only the block of the first true condition runs.
#include <iostream>
int main() {
int score = 78;
if (score >= 90) {
std::cout << "Excellent\n";
} else if (score >= 70) {
std::cout << "Good\n";
} else if (score >= 50) {
std::cout << "Satisfactory\n";
} else {
std::cout << "Try again\n";
}
return 0;
}Good
score is 78: the first condition (78 >= 90) is false and the second (78 >= 70) is true — Good is printed and the remaining conditions are not checked at all.
Comparison and logical operators
| Operator | Meaning | True example |
|---|---|---|
== | is equal to | 5 == 5 |
!= | is not equal to | 5 != 3 |
< > | less than, greater than | 3 < 5 |
<= >= | less or equal, greater or equal | 5 >= 5 |
&& | AND: both conditions are true | 4 > 0 && 4 < 10 |
|| | OR: at least one is true | 4 < 0 || 4 > 2 |
! | NOT: flips the value | !false |
#include <iostream>
int main() {
int age = 15;
bool hasTicket = true;
if (age >= 12 && hasTicket) {
std::cout << "Welcome to the cinema!\n";
}
if (age < 7 || age > 65) {
std::cout << "Free entry\n";
} else {
std::cout << "Standard price\n";
}
if (!hasTicket) {
std::cout << "Please buy a ticket\n";
}
return 0;
}Welcome to the cinema! Standard price
&& operator works like an AND gate: the output is 1 only when both inputs are 1.switch: choosing between options
When you need to compare one variable with many specific values, switch is convenient. Each case checks one value, and break leaves the switch. If you forget break, execution “falls through” into the next case — sometimes this is done on purpose: below, 6 and 7 give the same result. default runs when no case matches.
#include <iostream>
int main() {
int day = 6;
switch (day) {
case 6:
case 7:
std::cout << "Weekend\n";
break;
case 5:
std::cout << "Almost weekend\n";
break;
default:
std::cout << "Working day\n";
}
return 0;
}Weekend
for and while loops
A for loop is used when you know in advance how many times to repeat. Its header has three parts separated by semicolons:
int i = 1— start: the counter variable is created once.i <= 5— condition: checked before each repetition; when it is false, the loop ends.i++— step: runs after each repetition.
#include <iostream>
int main() {
for (int i = 1; i <= 5; i++) {
std::cout << i << " x 3 = " << i * 3 << '\n';
}
return 0;
}1 x 3 = 3 2 x 3 = 6 3 x 3 = 9 4 x 3 = 12 5 x 3 = 15
A while loop runs as long as its condition is true and is handy when you do not know the number of repetitions in advance. For example: Leyla has 50 manats and saves 40 manats every month. After how many months will she have at least 200 manats?
#include <iostream>
int main() {
int savings = 50;
int months = 0;
while (savings < 200) {
savings += 40;
months++;
}
std::cout << "Months: " << months << '\n';
std::cout << "Savings: " << savings << " AZN\n";
return 0;
}Months: 4 Savings: 210 AZN
There is also the do { ... } while (condition); loop: it checks the condition after running the block, so the body always runs at least once. It is handy, for example, for showing a menu to the user at least once.
break and continue
break stops the loop immediately, while continue skips the rest of the current repetition and moves on to the next one. The program below skips even numbers and ends the loop when it reaches a number greater than 7:
#include <iostream>
int main() {
for (int n = 1; n <= 10; n++) {
if (n % 2 == 0) {
continue;
}
if (n > 7) {
break;
}
std::cout << n << '\n';
}
return 0;
}1 3 5 7
Key points
if,else ifandelsecheck conditions from top to bottom and run the block of the first true one.==compares and=assigns;&&,||and!combine conditions.switchcompares one variable with specific values; do not forgetbreakat the end of eachcase.forsuits a known number of repetitions,whileruns while a condition holds, anddo-whileruns at least once.breakstops a loop;continuejumps to the next repetition.
Check yourself
10 questions. Every correct answer earns XP.
int t = 25;, what does this code print? if (t > 30) { std::cout << "Hot"; } else if (t > 20) { std::cout << "Warm"; } else { std::cout << "Cold"; }