From: Yong-Kwang Goh 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: NNTP-Posting-Host: 58echo050.singnet.com.sg Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit 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 < 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 > > #include > > #include > > int main() > > { > > clrscr(); > > // for loop 1 > > for (int x =0; x <10 ; x++) > > cout < > 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; > > }