SunQuest
 
           C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old June 8th, 2004, 04:50 PM
Bespin Bespin is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 4 Bespin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
total beginners help please!

Hi there,

Im learning c++ for the first time with no background whatsoever.
Ive only just started about two weeks ago (so I apologise if this looks
a stupid questoin!) Whilst I did a lot of Pascal
about 6 years ago at uni, this is all totally new to me!

I would be really grateful if someone could help me to understand the simple
ins and outs of the for loop. for example:

//System has already read in the value of loopcount from user
for (int i = 1; i <= loopcount; i++)
{
cout << "Number of loops completed: " << i << "loops\n";
}

Why doesnt the loop automatically set i to 1 everytime it starts? if this is a
repeating loop, shouldnt it read the whole line from scratch everytime,
thus creating a never ending loop?
I cant understand why it ignores the statement int i = 1; everytime it goes round.
I get the fact that while i is less than or equal to loopcount, it increments i
untill otherwise and displays the message. I tried to declare the variable
outside the brackets though obviously it didnt work. Can someone explain this?

Thanks very much!!!!!

Mike.

Reply With Quote
  #2  
Old June 9th, 2004, 04:53 AM
kode_monkey kode_monkey is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 367 kode_monkey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 21 sec
Reputation Power: 5
Hi Mike,

Don't worry this isn't a stupid question is a common misconception people have about how the for loop works. Taking the following for loop -

for (int i = 1; i <= loopcount; i++)

it can be broken down into three parts.

1) int i = 1 : This is the initialising statement that is run once at the start of the loop, not once each time it is run.

2) i <= loopcount : This is the end condition of the loop, telling it to run until this condition is violated.

3) i++ : This is the increment operator (thats probably a bad name for it since it doesn't have to increment something you could have i-- or i=i^2 or whatever) This is run at the end of each iteration generally to move between the start condition and end condition.

So that for loop is equivalent to something of the form -

int i = 1; // initialise i
while (i <= loopcounter)
{
// do something with i here
i++;
}

So i starts at 1 and runs until it is no longer <= loopcounter (ie its greater than loopcounter). This is achieved by incrementing it at the end of each iteration.

Hope this helps,

-KM-

Reply With Quote
  #3  
Old June 9th, 2004, 10:34 AM
Bespin Bespin is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 4 Bespin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks very much! That helps a lot. I wish my books had
explained that way, rather than simply listing the code and
expecting you to understand it!

Reply With Quote
  #4  
Old June 9th, 2004, 03:38 PM
kode_monkey kode_monkey is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 367 kode_monkey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 21 sec
Reputation Power: 5
No problems, always glad to help.

What book are you working from? If you have some spare cash I highly recommend C++ Primer Plus. It is both an excellent starting guide and great reference once you know what you're doing.

-KM-

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > total beginners help please!


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway