SRL Archive - OSR Help

for i := 0 to blah question


JPHamlett

2010-05-23 07:57:29

Hey ok i know how for i := 0 to balh works and all but is it possible to only ahve it do the even numbers and not the odds


such as

only do 0, 2, 4, 6, 8, 10, etc

or in java terms

for (int i = 0; i < blah; i += 2)

mastaraymond

2010-05-23 07:59:35

Make it a while loop.


program new;
const
blah = 15;
var
i : integer;
begin
i := 0;
while i < blah do
begin
Writeln(inttostr(i));
i := i + 2;
end;
end.

Gu3st...

2010-05-23 08:02:45

or use mod
for I := 0 to 100 do
begin
if I mod 2 <> 0 then Continue;
WriteLn(I);
end;

Baked0420

2010-05-23 10:18:25

or you can do this:


var
i: integer;
begin
for i:= 0 to 10 do
begin
writeln(i);
inc(i);
end;
end.

or to be more safe:


var
i: integer;
begin
for i:= 0 to 10 do
begin
if(i mod 2 = 1) then
inc(i);
writeln(i);
inc(i);
end;
end.

just to make sure it's doing evens ;)

java makes it easier to do for loops with evens, but it's still very possible and easy to do in pascal.

Sir R. M8gic1an

2010-05-23 10:49:35

Ray's while loop would be the most efficient solution.

~RM

Wanted

2010-05-23 14:43:27

From: J_Pizzle
Hey ok i know how for i := 0 to balh works and all but is it possible to only ahve it do the even numbers and not the odds


such as

only do 0, 2, 4, 6, 8, 10, etc

or in java terms

for (int i = 0; i < blah; i += 2)


for I := 0 to 5 do
WriteLn(I * 2);

Awkwardsaw

2010-05-23 15:02:22

From: IceFire908
for I := 0 to 5 do
WriteLn(I * 2);


that results in
0
2
4
6
8
10

he wants
0
2
4

afaik

Frement

2010-05-23 15:06:36

From: Awkwardsaw
that results in
0
2
4
6
8
10

he wants
0
2
4

afaik


What??? Those are the same :O You could just change from 5 to 2?

Wanted

2010-05-23 15:10:16

From: Awkwardsaw
that results in
0
2
4
6
8
10

he wants
0
2
4

afaik


What?

Awkwardsaw

2010-05-23 15:40:02

From: IceFire908
What?


What?

JPHamlett

2010-05-23 16:11:23

i figured it out guys stop spamming

YoHoJo

2010-05-23 16:20:04

From: J_Pizzle
i figured it out guys stop spamming


what?

Awk:
Icefire posted:
for I := 0 to 5 do
WriteLn(I * 2);
Which gave 0 2 4 6 8 10

If you want 0 2 4
Just do
for I := 0 to 2 do
WriteLn(I * 2);

Pretty straight forward! :D

Awkwardsaw

2010-05-23 16:26:50

From: YoHoJo
what?

Awk:
Icefire posted:
for I := 0 to 5 do
WriteLn(I * 2);
Which gave 0 2 4 6 8 10

If you want 0 2 4
Just do
for I := 0 to 2 do
WriteLn(I * 2);

Pretty straight forward! :D


unless for some reason the end result(the 2 in this case) is dynamic, although i didnt show it, that was sort of my point :)

Sir R. M8gic1an

2010-05-23 16:30:28

If you guys are really going to go about it that way…


for i := 0 to Total/2 do
begin
x := i * 2;
//use x from here on
end;

~RM