|
Loop Counter
|
|
11-19-2009, 08:31 PM
Post: #1
|
|||
|
|||
|
Loop Counter
Hi guys,
I have a little problem with 'for loop,counter' how can i increse the counter in loop?the default increasment is 1.for example when you code: For i:=1 To 100 it will automatically increase i with 1.what am i suppose to do if i ever wanted to increase i 2 times rather 1.thanks Gholamreza Takhti who was he? find out |
|||
|
11-19-2009, 08:46 PM
(This post was last modified: 11-19-2009 08:49 PM by drdebcol.)
Post: #2
|
|||
|
|||
|
RE: Loop Counter
For that you need to use "while" or "repeat" loop. First of all for loop in pascal does increment by one, and that is the only bad thing, for example in C++ you can do "for (i=1;i<=n;any_increment)" but here you are limited only to increment by one. Because of that you need to use other types of loops :
While loop (pre-test loop) Repeat loop (post-test loop) Now let's explain the syntax of while loop : Code: i:=1; // set counter on start value as "for i:=1"Code: i:=1; // set counter on start value as "for i:=1"And in while you have condition "i<n" and in repeat you have condition "i>n", so the sign changes, you need to know that. More about loops you have here : FOR : http://www.pro9ramming.com/learnpascal/for.html WHILE : http://www.pro9ramming.com/learnpascal/while.html REPEAT : http://www.pro9ramming.com/learnpascal/repeat.html I think i have said enough, now let's do writing of even numbers with "while" loop, without condition. With for you had to use condition : Code: for i:=1 to n doCode: i:=0;Code: i:=i-2;Code: for i:=n downto 1 doThere's a fine line between genius and insanity. I have erased this line. Oscar Levant There's a fine line between an administrator and black hat hacker. I have erased this line. Dr DEBCOL |
|||
|
11-19-2009, 09:29 PM
Post: #3
|
|||
|
|||
RE: Loop Counter
(11-19-2009 08:46 PM)drdebcol Wrote: For that you need to use "while" or "repeat" loop. First of all for loop in pascal does increment by one, and that is the only bad thing, for example in C++ you can do "for (i=1;i<=n;any_increment)" but here you are limited only to increment by one. Because of that you need to use other types of loops :Thanks friend that was a complete explanation.i appreciate that ![]() .and down with for loop!
Gholamreza Takhti who was he? find out |
|||
|
11-19-2009, 09:34 PM
Post: #4
|
|||
|
|||
|
RE: Loop Counter
That was a very interesting question Alireza_M.
![]() I was looking forward to see the answer to this one. ![]()
|
|||
|
« Next Oldest | Next Newest »
|









![[Image: 45669_pythonlogo.png]](http://myph.us/pics/45669_pythonlogo.png)