Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
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
Find all posts by this user
Quote this message in a reply
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"
while (i<n) do
begin
//here goes your code as in for loop
i:=i+1; //this is increment by one, but you can also use "i:=i+2" or 3,4 . . .
end;
And now syntax of repeat loop :
Code:
i:=1; // set counter on start value as "for i:=1"
repeat
begin
//here goes your code as in for loop
i:=i+1; //this is increment by one, but you can also use "i:=i+2" or 3,4 . . .
end;
until (i>n);
As you can see in repeat loop it will be at least one circulation because it is post-test, it will test after circulation, and while will test before.
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 do
if i mod 2 = 0 then
writeln(i);
But with while you don't have to use "if", like this :
Code:
i:=0;
while(i<=n) do
begin
i:=i+2;
writeln(i);
end;
As you can see i used increment by 2, also you can use decrement by two like this :
Code:
i:=i-2;
That is like "countdown" in for loop :
Code:
for i:=n downto 1 do
Okay, i think this solves your problem !

There'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
Visit this user's website Find all posts by this user
Quote this message in a reply
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 :
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"
while (i<n) do
begin
//here goes your code as in for loop
i:=i+1; //this is increment by one, but you can also use "i:=i+2" or 3,4 . . .
end;
And now syntax of repeat loop :
Code:
i:=1; // set counter on start value as "for i:=1"
repeat
begin
//here goes your code as in for loop
i:=i+1; //this is increment by one, but you can also use "i:=i+2" or 3,4 . . .
end;
until (i>n);
As you can see in repeat loop it will be at least one circulation because it is post-test, it will test after circulation, and while will test before.
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 do
if i mod 2 = 0 then
writeln(i);
But with while you don't have to use "if", like this :
Code:
i:=0;
while(i<=n) do
begin
i:=i+2;
writeln(i);
end;
As you can see i used increment by 2, also you can use decrement by two like this :
Code:
i:=i-2;
That is like "countdown" in for loop :
Code:
for i:=n downto 1 do
Okay, i think this solves your problem !
Thanks friend
that was a complete explanation.i appreciate thatHeartHeart.and down with for loop!

Gholamreza Takhti
who was he? find out
Find all posts by this user
Quote this message in a reply
11-19-2009, 09:34 PM
Post: #4
RE: Loop Counter
That was a very interesting question Alireza_M. Wink
I was looking forward to see the answer to this one.

[Image: 45669_pythonlogo.png][Image: 45668_javalogo.png]
Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: