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

Contenuti del Corso

Introduction to C++ | Mobile-Friendly

Introduction to C++ | Mobile-Friendly

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

book
Else If Expression

In the last task, you might have noticed that we didn't consider the case when we print not valid age (for example, -1 or -6). Therefore, there should be a way to modify our code to predict such things.

If you want to add one more condition to check it, use else if statement:

if (condition#1) {
    // code here will be executed if the condition#1 is true
} else if (condition#2) {
    // code here will be executed if the condition#1 is false and 
condition#2 is true
} else {
    // code here will be executed if the condition#1 is false and 
condition#2 is false
}

So our modified code for the previous task will look as follows:

if (age < 0){
    cout << "The age you typed isn't valid. Try again, please.";
} else if (age >= 21) {
    cout << "You can buy alcohol.";
} else {
    cout << "You can't buy alcohol.";
}

The code initially checks if the age is valid. If the first statement is true the program prints a message if not - goes to the second condition. If the second condition is true and the user is over (or equal to) 21 years old the code prints a message, otherwise, both conditions are false and the customer is less than 21 and can’t buy alcohol.

You can add as many conditions using else if statement as you want.

question mark

In which case should you use else if statement?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 3. Capitolo 5

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

course content

Contenuti del Corso

Introduction to C++ | Mobile-Friendly

Introduction to C++ | Mobile-Friendly

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

book
Else If Expression

In the last task, you might have noticed that we didn't consider the case when we print not valid age (for example, -1 or -6). Therefore, there should be a way to modify our code to predict such things.

If you want to add one more condition to check it, use else if statement:

if (condition#1) {
    // code here will be executed if the condition#1 is true
} else if (condition#2) {
    // code here will be executed if the condition#1 is false and 
condition#2 is true
} else {
    // code here will be executed if the condition#1 is false and 
condition#2 is false
}

So our modified code for the previous task will look as follows:

if (age < 0){
    cout << "The age you typed isn't valid. Try again, please.";
} else if (age >= 21) {
    cout << "You can buy alcohol.";
} else {
    cout << "You can't buy alcohol.";
}

The code initially checks if the age is valid. If the first statement is true the program prints a message if not - goes to the second condition. If the second condition is true and the user is over (or equal to) 21 years old the code prints a message, otherwise, both conditions are false and the customer is less than 21 and can’t buy alcohol.

You can add as many conditions using else if statement as you want.

question mark

In which case should you use else if statement?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 3. Capitolo 5
some-alt