neuling Posted March 30 Share Posted March 30 Hi good afternoon. how can you do the for with step 2 please? Thanks. greeting Quote Link to comment Share on other sites More sharing options...
Kylev Posted March 30 Share Posted March 30 For i=1 to n step 2 in BASIC. Were you looking for a PASCAL version? Quote Link to comment Share on other sites More sharing options...
TGB1718 Posted March 30 Share Posted March 30 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 1 Quote Link to comment Share on other sites More sharing options...
Rybags Posted March 30 Share Posted March 30 It'd be just +1 wouldn't it? The end of the loop should increment by 1 regardless of what you do to the counter inside the loop. Quote Link to comment Share on other sites More sharing options...
TGB1718 Posted March 31 Share Posted March 31 (edited) 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 March 31 by TGB1718 Quote Link to comment Share on other sites More sharing options...
drunkeneye Posted March 31 Share Posted March 31 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'. Quote Link to comment Share on other sites More sharing options...
neuling Posted April 1 Author Share Posted April 1 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'. Quote Link to comment Share on other sites More sharing options...
neuling Posted April 1 Author Share Posted April 1 Hello, thanks for the help greeting Quote Link to comment Share on other sites More sharing options...
TGB1718 Posted April 1 Share Posted April 1 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 Quote Link to comment Share on other sites More sharing options...
tebe Posted April 1 Share Posted April 1 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. 1 1 Quote Link to comment Share on other sites More sharing options...
neuling Posted April 3 Author Share Posted April 3 hello thanks for help. greeting. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.