| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
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. |
|
#2
|
|||
|
|||
|
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- |
|
#3
|
|||
|
|||
|
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! |
|
#4
|
|||
|
|||
|
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- |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > total beginners help please! |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|