Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Do/While Loop | Loops
Introduction to C++ | Mobile-Friendly
course content

Зміст курсу

Introduction to C++ | Mobile-Friendly

Introduction to C++ | Mobile-Friendly

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

Do/While Loop

As an alternative to while loop you can use do/while loop. The difference is that the do/while loop will execute the code in the block once before checking if the condition returns true.

Syntax:

The example of using do/while loop:

12345
int i = 0; do { &nbsp;&nbsp;&nbsp;&nbsp; cout << i << endl; &nbsp;&nbsp;&nbsp;&nbsp; i++; } while (i < 3);

The first iteration will be firstly executed and i becomes 1. By the third iteration, i becomes 3 and the condition returns false after that, and do/while loop stops.

Pay attention, that the first iteration will be executed even if the condition is always false:

12345
int i = 0; do { &nbsp;&nbsp;&nbsp;&nbsp; cout << i << endl; &nbsp;&nbsp;&nbsp;&nbsp; i++; } while (i < 0);
question-icon

Monica, Joey, and Janice put their money in the bank at 10% annual income. Fill the gaps in the program that accepts a user’s input the money of each person and calculate the annual income by multiplying the sum of money by 0.1 (10%).

#include
using namespace std;

int main() {
    // Create variable
    int i = 0;
    int amountOfMoney;

    // Use do/while loop to calculate the income
    
{
        cout << "Print the amount of money to put: ";
        
>> amountOfMoney;
        cout << "The annual income = " << amountOfMoney * 0.1 << endl;
        
;
    }
(i < 3);

    return 0;
}

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

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

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