Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Else Expression | Conditional Statements
Introduction to C++
course content

Зміст курсу

Introduction to C++

Introduction to C++

1. Basics
2. Variables
3. Conditional Statements
4. Loops
5. Intro to Arrays

bookElse Expression

In the previous task, we mentioned what the program should do if our statement is true, but what to do if we need to specify actions if the condition is also false. For such situations in C++ exists the else statement.

The syntax is the following:

Let’s define if the number is even (can be divided by 2 into two equal numbers) or not, using the else statement:

The code accepts a number from the user. If the number by division by 2 gives 0 as the remainder (for example 6/2 = 3 the remainder is 0) then the condition is true and the user printed an even number, otherwise, the number is not even.

question-icon

In America, you are allowed to buy alcohol from 21. Fill the gaps in the code, that checks if the customer can or not buy the alcohol by the age.

#include
using namespace std;

int main() {
    int age;
    cin >> age;
    
(age21) {
        cout << "You can buy alcohol.";
    }
{
        cout << "You can't buy alcohol.";
    }

    return 0;

Натисніть або перетягніть елементи та заповніть пропуски

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 3. Розділ 4
We're sorry to hear that something went wrong. What happened?
some-alt