Jump to content
IGNORED

for i := 1 to n do with step 2 ?


Recommended Posts

Pascal doesn't have a STEP, what you have to do is increment the counter inside the loop yourself

something like

 

for i := 1 to 10 do

  begin

     Writeln(i); {print i value}

      i := i+2;    { step 2 }

   end

 

  • Like 1
Link to comment
Share on other sites

11 hours ago, Rybags said:

It'd be just +1 wouldn't it?

Good spot :) obviously the loop does the other increment, it was late last night 🙃

 

Probably in this case, a while-do loop would be more appropriate

 

var
    i: integer;
begin
i := 0;
while i < 10 do
begin
    Writeln(i);
    i := i+2;
end;

 

this will run the loop 5 times, output will be

0

2

4

6

8

 

 

 

Edited by TGB1718
Link to comment
Share on other sites

19 hours ago, TGB1718 said:

Pascal doesn't have a STEP, what you have to do is increment the counter inside the loop yourself

something like

 

for i := 1 to 10 do

  begin

     Writeln(i); {print i value}

      i := i+2;    { step 2 }

   end

 

 

just as a 'reminder', at least back in turbo pascal 6.0 it was not always

the case that var i will be really checked against. in x86 assembler it could

happen that pascal converts it something like 'mov ax, i; ... dec ax; loop ..'.

when this happens, you can change i all you want inside the loop without any

effect. whether this was used might depend on the optimization flags of

the compiler. or maybe also on the version of pascal. i have no clue if mad

pascal is 'affected'.

 

 

Link to comment
Share on other sites

Hello, thanks for the help
greeting

15 hours ago, drunkeneye said:

 

just as a 'reminder', at least back in turbo pascal 6.0 it was not always

the case that var i will be really checked against. in x86 assembler it could

happen that pascal converts it something like 'mov ax, i; ... dec ax; loop ..'.

when this happens, you can change i all you want inside the loop without any

effect. whether this was used might depend on the optimization flags of

the compiler. or maybe also on the version of pascal. i have no clue if mad

pascal is 'affected'.

 

 

 

Link to comment
Share on other sites

17 hours ago, drunkeneye said:

just as a 'reminder', at least back in turbo pascal 6.0 it was not always

the case that var i will be really checked against.

Thanks for the info, btw. I've never used Pascal, but am aware of some of it's syntax :)

 

Link to comment
Share on other sites

On 3/30/2024 at 9:28 PM, TGB1718 said:

Pascal doesn't have a STEP, what you have to do is increment the counter inside the loop yourself

something like

 

for i := 1 to 10 do

  begin

     Writeln(i); {print i value}

      i := i+2;    { step 2 }

   end

 

https://www.freepascal.org/docs-html/ref/refsu58.html#x168-19200013.2.4

 

  • It is not allowed to change (i. e. assign a value to) the value of a loop variable inside the loop.
  • Like 1
  • Thanks 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...