Board index » delphi » For Loop & While Loop Example in C++

For Loop & While Loop Example in C++

Hi!

a simple for loop:

for(int i=0;i<end;i++)
{
    //some code

Quote
}

first parameter is the indikator for a loop-variable. second param is a
boolean for ending the loop. third param is the changing after each loop.

while loop:

bool ok;
while(ok==true)
{
    if(param) ok=true;
    //some code

Quote
}

the parameter in the head is a boolean for ending the loop. the if-clause is a
sample for setting the end of the loop.

cu Volker Gersabeck

Ahmad Hassouna schrieb:

Quote
> Hi, I am looking for some exmaple in C++, for a "For" loop & "While" loop,
> could U assist me in finding them in Web site, or if U have some example
> could U send via email?
> Thamks in advance

 

Re:For Loop & While Loop Example in C++


Hi, I am looking for some exmaple in C++, for a "For" loop & "While" loop,
could U assist me in finding them in Web site, or if U have some example
could U send via email?
Thamks in advance

Re:For Loop & While Loop Example in C++


All great, but there were some points that might've been confusing for a
beginner (like someone asking about for loops!). So hopefully we can
give a more accurate help together:

the for loop, as written below, has four parts:

int i; /* Let's create our variables first */
for (i = 2; i < 10; i++)
   {
   cout << "Hello\n";
   }

Now the important thing to remember is that the for has three definite parts
between the parenthesis. Each are seperated by a semi-colon (;).

i = 0

The first part is where you give an initial value to all variables you want.
You can assign more than one variable but let's keep it simple for now.

i < 10

The second part is the condition(s). If any of the given conditions is false
BEFORE one instance of the loop is completed, it will then 'go out' of the
loop.

i++

The third part happens AFTER every instance of your loop (we can consider
so MOST of the time, except for ++i and such).

So basically the for is done that way:

1. Give i the value 2
2. Check if i < 10 (true, go on - false, stop looping)
3. Execute the loop code (here, cout << "Hello\n")
4. Execute the third part of the for loop, which is i++ (increment i by one)
5. Go back to step 2

So you see that the for loop will run 8 times, from 2 (inclusive) to 10
(exclusive).

The while loop is actually simpler.

In the example below, the execution looks like this:

1. Check if (ok == true)
2. Execute the code loop between the {}'s
3. Go back to step 1

As you see, a for is an advanced while (actually there aren't many things that
you can't do with a while that you can do with for, again with a few
exceptions).

- Marc

Quote
> bool ok;
> while(ok==true)
> {
>     if(param) ok=true;
>     //some code
> }

> the parameter in the head is a boolean for ending the loop. the if-clause is a
> sample for setting the end of the loop.

> cu Volker Gersabeck

> Ahmad Hassouna schrieb:

> > Hi, I am looking for some exmaple in C++, for a "For" loop & "While" loop,
> > could U assist me in finding them in Web site, or if U have some example
> > could U send via email?
> > Thamks in advance

Re:For Loop & While Loop Example in C++


Quote
"Marc Bellemare" <x.mar...@oricom.ca> wrote in message

news:3AD07A69.8B124446@oricom.ca...

Quote
> All great, but there were some points that might've been confusing for a
> beginner (like someone asking about for loops!). So hopefully we can
> give a more accurate help together:

> the for loop, as written below, has four parts:

> int i; /* Let's create our variables first */
> for (i = 2; i < 10; i++)
>    {
>    cout << "Hello\n";
>    }

Since this is for a beginner and we don't want to confuse anyone, you should
mention that declaring the variables first puts their scope outside of the
for loop.  Declaring and initializing i inside the for loop gives them scope
only inside the loop.

Other Threads