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:
python9123do {// The code block} while (condition);
The example of using do/while
loop:
9
1
2
3
4
5
int i = 0;
do {
cout << i << endl;
i++;
} while (i < 3);
12345int i = 0; do { cout << i << endl; 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:
9
1
2
3
4
5
int i = 0;
do {
cout << i << endl;
i++;
} while (i < 0);
12345int i = 0; do { cout << i << endl; i++; } while (i < 0);
To pick up a draggable item, press the space bar.
While dragging, use the arrow keys to move the item.
Press space again to drop the item in its new position, or press escape to cancel.
Alt var klart?
Takk for tilbakemeldingene dine!
Seksjon 4. Kapittel 2
Spør AI
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår