Skip to content
Educora
Beginner18 min3 / 10

Conditions and loops

Teach your program to make decisions and repeat work: `if`, `switch`, comparison and logical operators, `for`, `while`, `break` and `continue`.

Check yourself
In this lesson you will learn
  • Write if, else if and else with comparison and logical operators
  • Choose between several options with switch
  • Repeat work with for, while and do-while loops
  • Control loops with break and continue

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.

C++
#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;
}
Expected output
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

OperatorMeaningTrue example
==is equal to5 == 5
!=is not equal to5 != 3
< >less than, greater than3 < 5
<= >=less or equal, greater or equal5 >= 5
&&AND: both conditions are true4 > 0 && 4 < 10
||OR: at least one is true4 < 0 || 4 > 2
!NOT: flips the value!false
C++
#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;
}
Expected output
Welcome to the cinema!
Standard price
Interactive
Loading simulation…
The && 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.

C++
#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;
}
Expected output
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:

  1. int i = 1 — start: the counter variable is created once.
  2. i <= 5 — condition: checked before each repetition; when it is false, the loop ends.
  3. i++ — step: runs after each repetition.
C++
#include <iostream>

int main() {
    for (int i = 1; i <= 5; i++) {
        std::cout << i << " x 3 = " << i * 3 << '\n';
    }
    return 0;
}
Expected output
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?

C++
#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;
}
Expected output
Months: 4
Savings: 210 AZN
Values: 90, 130, 170, 210 — after the fourth month the condition becomes false.

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:

C++
#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;
}
Expected output
1
3
5
7

Key points

  • if, else if and else check conditions from top to bottom and run the block of the first true one.
  • == compares and = assigns; &&, || and ! combine conditions.
  • switch compares one variable with specific values; do not forget break at the end of each case.
  • for suits a known number of repetitions, while runs while a condition holds, and do-while runs at least once.
  • break stops a loop; continue jumps to the next repetition.

Check yourself

10 questions. Every correct answer earns XP.

1 / 10
With 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"; }