delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2001/02/09/11:46:34

From: Yong-Kwang Goh <ykgoh AT email DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: for loop
Date: Fri, 09 Feb 2001 22:56:24 +0800
Organization: Singapore Telecommunications Ltd
Lines: 129
Message-ID: <3A840517.5A987F16@email.com>
References: <EuJf6.247$GB3 DOT 58858 AT sapphire DOT mtt DOT net>
NNTP-Posting-Host: 58echo050.singnet.com.sg
Mime-Version: 1.0
X-Mailer: Mozilla 4.73 [en] (Win95; U)
X-Accept-Language: en,en-US,en-GB,zh,zh-CN,zh-TW
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

I assume you know the purpose of setw(), clrscr() and getch() and the other
stuffs not related to the looping.
OK Let's tackle this step-by-step.

As we can see, there're 4 loops in this program. Let see the first one:
---------------------------------
01 for (int x =0; x <10 ; x++)
02 cout <<setw(3) << x;
---------------------------------
* Line 01 of the loop simply means create a variable call x and set it to
contain 0 initially.
* Repeat when the content of x is less than 10 (e.g. 0 - 9)
* Increment (increase by 1) the content of x after each loop.
* Line 02 prints out the content of x as the loop goes along...

So the expected output goes like this:
  0  1  2  3  4  5  6  7  8  9

Let's move on to loop 2
---------------------------------
for (int y = 1; y <= 10; y += 2)
cout << setw(3) << y;
---------------------------------
Similar to loop 1, the only difference is:
* now our variable is called y instead of x
* and y is set to the value of 1 initially
* the loop is repeated when y is less than *or* equals to 10 (e.g. 1 - 10)
* and the value of y is increased by 2 after each loop.

So the expected output goes like this:
  1  3  5  7  9

Let's take a look at loop 3
---------------------------------
for (int z =10; z > 0; z --)
cout << setw (3) << z;
---------------------------------
It resembles loop 1 but now
* the variable is now called z
* it has a starting value of 10
* the loop is repeated when value of z is more than 0 (e.g. 10 - 1)
* value of z is decremented (redueced by 1) after each loop

The output goes like this:
 10  9  8  7  6  5  4  3  2  1

Ok, we've reached the last one
---------------------------------
for (int a =10; a < 0; a++)
cout << setw(3) << a;
---------------------------------
As usual there's a change in variable name *yawn*
* it's now called a instead
* a has a inital value of 10
* the loop is repeated when a is less than 0 (negative which is impossible as a
has already been set to 10)
* the value of a is incremented after each loop

So this one is a bit more tricky than the previous 3. When a has a value of 10,
and the criteria (a.k.a. condition) for the loop fails (a less than 0), the loop
is not entered at all and is *skipped* over to the next piece of code, which is
------------------------------------
cout << endl;
getch();
return 0;
------------------------------------
Hope this helps.
If you're still having trouble understanding all these, you need to turn to a
good programming textbook to understand the language syntax etc.

Yong-Kwang Goh
Singapore

Taylor MacDonald wrote:

> I created this program will the help of some of my friends and now I need to
> analysis it. Would someone please tell me how all of these for loops work.
> How can I explain the output and  how the output is determined?
>
> #include <iostream.h>
>
> #include <conio.h>
>
> #include <iomanip.h>
>
> int main()
>
> {
>
> clrscr();
>
> // for loop 1
>
> for (int x =0; x <10 ; x++)
>
> cout <<setw(3) << x;
>
> cout << endl;
>
> // for loop 2
>
> for (int y = 1; y <= 10; y += 2)
>
> cout << setw(3) << y;
>
> cout << endl;
>
> // for loop 3
>
> for (int z =10; z > 0; z --)
>
> cout << setw (3) << z;
>
> cout << endl;
>
> // for loop 4
>
> for (int a =10; a < 0; a++)
>
> cout << setw(3) << a;
>
> cout << endl;
>
> getch();
>
> return 0;
>
> }

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019