C++ · ОСНОВЫ · LESSON 05

Choosing a path

if and else · about 12 minutes

Learning with progressRead the summary for free. To save the work, log in to the account.
STEP 1

How it works.

if checks the condition. If the answer is correct, the first block works; otherwise, else.

if (points >= 5) {
    cout << "Сдано" << endl;
} else {
    cout << "Ещё раз" << endl;
}

✦ Each block has its own brackets.

STEP 2

Try to understand.

Each block has its own brackets.

int points = 7;
if (points >= 5) {
    cout << "Супер" << endl;
} else {
    cout << "Попробуй ещё" << endl;
}

✦ Compare this to the first one. Then do the cool job yourself.

IN-CLASS PRACTICE

Type the code yourself

Manually type the program from the sample. So you'll remember commands and brackets.

#include <iostream>
using namespace std;

int main() {
    int points = 7;
    if (points >= 5) {
        cout << "Супер" << endl;
    } else {
        cout << "Попробуй ещё" << endl;
    }
    return 0;
}
Open the editor for class work
HOMEWORK

Solve it yourself

Create int score = 3. If score is at least 5, output «Сдано», otherwise «Тренируйся».

The answer is hidden. Use similar examples from the lesson notes.

Opening up your homework