Programming With C++
Programming With C++
Loops
Why Loops
• Loops are used to repeat a block of
code. Being able to have your
program repeatedly execute a block
of code is one of the most basic but
useful tasks in programming
Do
{
<code for execution>
} while ( condition );
DO WHILE - Loop
#include <iostream>
using namespace std;
int main()
{
int x;
x = 0;
do
{
// "Hello, world!" is printed at least one time
// even though the condition is false
cout<<"Hello, world!\n";
}
while ( x != 0 );
cin.get();
}
Important
• Keep in mind that you must include a
trailing semi-colon after the while in
the above example.
• A common error is to forget that a
do..while loop must be terminated with
a semicolon (the other loops should
not be terminated with a semicolon,
adding to the confusion).
• Notice that this loop will execute once,
because it automatically executes
before checking the condition.