Goto https://www.stucuk.netGoto https://www.atlanticaonlinewiki.comGoto https://www.game-requirements.com


Menu


It is currently Fri Apr 19, 2019 3:40 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: "for var in list do" and "for var = 1 to value do"
PostPosted: Fri Mar 05, 2010 6:41 pm 
Offline
Moderator
Moderator
User avatar

Joined: Mon Mar 17, 2008 6:03 pm
Posts: 1130
Location: Denmark
Common for these two blocks, "for var in list do" and "for var = 1 to value do", is that they loop a piece of code a number of times. But when you use these loops, you have to be aware of certain things.

"for var in list do"
The "list" is saved in some temporary memory, meaning that even if you alter "list" inside the loop, "var" will still assume the values from the 'old' "list".
Example:
Code:
...
temp_list = [34, 45, 77, 99];
for b in temp_list do
     if b < 50 then
          temp_list = Delete(temp_list,(temp_list+0));

Despite the values "77" and "99" is removed from "temp_list", the loop is run 4 times while b assumes the values "34", "45", "77" and "99" in turn.

The loop doesn't care if the value "b" holds is changed during the loop, though.
Example:
Code:
...
temp_list = [1, 2, 3, 4];
c = 0;
for b in temp_list do
     begin
          c = c + 1;
          b = 3;
     end;

"c" will hold the value "4" when the loop is done.


"for var = 1 to value do"
Just like "for var in list do", "value" is saved, so if you change the value "value" holds within the loop, it will change nothing.

If you change "var" within the loop, it affects how many times the loop is looped.
Example:
Code:
c = 0;
for b = 1 to 5 do
     begin
          c = c + 1;
          if b > 2 then
              b = 5;
     end;

"c" will hold the value "3" when the loop is done.

These things apply for "for var = 1 downto value do" as well.

_________________
I have an if that has an else, but that if has another if with an else inside... My question is... If an if has an else, if there is more than one line in that if, should it have a begin and an end?


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group

Original War is an RTS / RPG game by Altar Games
Original War Support is not part of Altar Games but is maintaining Original War for Altar Games

Privacy Policy