Loops

Posts 1–5 of 5 · Page 1 of 1
Loops
So I'm still learning C++ and I came upon loops,
I get the all the loops but I'm just questioning if I really understand it.

The for loop:
[Highlight=c++]for (variable-your-using; test-condition; how-it-updates-itself)
{
body;
}[/Highlight]
Example:
[Highlight=c++]for (int i = 0; i < 10; i++)
{
cout << i;
}[/Highlight]

The while loop:
[Highlight=c++]variable-your-using;
while (test-condition)
{
body;
}[/Highlight]
Example:
[Highlight=c++]int i = 0;
while (i == 0)
{
cout << i;
}[/Highlight]

and the do while loop:
[Highlight=c++] variable-your-using;
do
{
body;
} while(test-condition);[/Highlight]
Example:
[Highlight=c++]int i = 0;
do
{
cout << i;
} while(i == 0);[/Highlight]

So this is right?
And if this is right, what's the functional difference between the while, and the do while loop?
while i equals 0? print? But it already starts as 0

You want to do something like

Code:
int i = 0;
do
{
     cout << i;
     i = i + 1;
}
while(i != 10);
Like:

Equals i To 0
Do:
Prints i
Adds 1 to i
Is i different than 10? (if yes, go back to Do, otherwise, leave the loop)
Quote Originally Posted by 'Bruno View Post
while i equals 0? print? But it already starts as 0

You want to do something like

Code:
int i = 0;
do
{
     cout << i;
     i = i + 1;
}
while(i != 10);
Like:

Equals i To 0
Do:
Prints i
Adds 1 to i
Is i different than 10? (if yes, go back to Do, otherwise, leave the loop)
Yeah I got that it would keep printing but it was just a fast made example...
So it should work?
yes it should
uhm... the loop would work... it's just an infinite loop xD
Posts 1–5 of 5 · Page 1 of 1

Post a Reply

Tags for this Thread

None

Need help?