Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Else If 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 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, 0 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:

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

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.

Завдання

Children have written a test, for which they can become from 0 to 100 points. Let’s write the converter from point to mark. The variable points type of int we become from the user and convert them to mark, assigning it to the variable mark type of bool.

  1. Accept the number of points from the user and store it in the variable points.
  2. Using if, else if and else statements assign to the mark the character 'A' if the number of points is greater than 80, the character 'B' if the number is greater than 50 but less than 80 and 'C' if the number is less than 50.
  3. Print the variable mark.

Please, don’t forget to type the semicolumn at the end of the lines.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

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

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

Секція 3. Розділ 5
toggle bottom row

bookElse 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, 0 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:

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

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.

Завдання

Children have written a test, for which they can become from 0 to 100 points. Let’s write the converter from point to mark. The variable points type of int we become from the user and convert them to mark, assigning it to the variable mark type of bool.

  1. Accept the number of points from the user and store it in the variable points.
  2. Using if, else if and else statements assign to the mark the character 'A' if the number of points is greater than 80, the character 'B' if the number is greater than 50 but less than 80 and 'C' if the number is less than 50.
  3. Print the variable mark.

Please, don’t forget to type the semicolumn at the end of the lines.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

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

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

Секція 3. Розділ 5
toggle bottom row

bookElse 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, 0 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:

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

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.

Завдання

Children have written a test, for which they can become from 0 to 100 points. Let’s write the converter from point to mark. The variable points type of int we become from the user and convert them to mark, assigning it to the variable mark type of bool.

  1. Accept the number of points from the user and store it in the variable points.
  2. Using if, else if and else statements assign to the mark the character 'A' if the number of points is greater than 80, the character 'B' if the number is greater than 50 but less than 80 and 'C' if the number is less than 50.
  3. Print the variable mark.

Please, don’t forget to type the semicolumn at the end of the lines.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

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

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

In the last task, you might have noticed that we didn't consider the case when we print not valid age (for example, 0 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:

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

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.

Завдання

Children have written a test, for which they can become from 0 to 100 points. Let’s write the converter from point to mark. The variable points type of int we become from the user and convert them to mark, assigning it to the variable mark type of bool.

  1. Accept the number of points from the user and store it in the variable points.
  2. Using if, else if and else statements assign to the mark the character 'A' if the number of points is greater than 80, the character 'B' if the number is greater than 50 but less than 80 and 'C' if the number is less than 50.
  3. Print the variable mark.

Please, don’t forget to type the semicolumn at the end of the lines.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Секція 3. Розділ 5
Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
some-alt