[suggestion] loop codes 'break' & 'continue'

Started by Lt. Smash, Fri 07/11/2008 15:09:18

Previous topic - Next topic

Lt. Smash

what would be the best approach to convert similar things like this into ags code:

Code: ags

String input (String s) {
int i;
char c;

while (i < s.Length -1) {
  if(s.Chars[i] != 'R')
    continue;   //return to top of loop, i++;
  if(s.Chars[i] == 'B')
    break; //continue after while loop
  if(i == 0 || c == '^')
  {
     s = "blabal";
     continue;
  }
  if(c == 'x')
     s = "blabalba";
}
   //break would continue here
...
}


Gilbert

To break a while loop, just set the running index to something that will stop the loop from running.

For example, in your case, just set the value to s.Length-1 or greater.

Of course those "if(i == 0 || c == '^') blah blah: codes will still be executed in your example, you may mess with those if's so that it won't do that.

Pumaman

To convert a "continue", just reverse the "if" logic and put the rest of the code inside the "if" for the rest of the loop.

For example, your "continue"s can be be converted like this:
Code: ags

while (i < s.Length -1) {
  if(s.Chars[i] == 'R')
  {
    if(s.Chars[i] == 'B')
      break; //continue after while loop
    if(i == 0 || c == '^')
    {
       s = "blabal";
    }
    else
    {
     if(c == 'x')
       s = "blabalba";
     }
  }
}

Lt. Smash

#3
I changed this thread into a suggestion and everyone can post in here, who would find continue and break support for loops useful.
Maybe we can get CJ to think about an implementation. maybe


I know its not on his priority list but CJ said some time ago that he would like to implement it. Maybe the time has come.  :)

Snarky

Throw in for-loops and switch/case, and I'd be a very happy camper indeed.

Lt. Smash

yes, I think 'for', 'switch', 'break' and 'continue' are the most often suggested ags language keywords. This should be taken into account.

Shane 'ProgZmax' Stevens

I'd definitely like to see switch/case.  That would make doing the custom dialogs much easier.  I wouldn't mind for loops, either.

Trent R

I personally have always like foreach loops, but implementing them depends on AGS works internally. I'm thinking of something like foreach Character* in Room1, blah blah do stuff. Or something like that.

~Trent
To give back to the AGS community, I can get you free, full versions of commercial software. Recently, Paint Shop Pro X, and eXPert PDF Pro 6. Please PM me for details.


Current Project: The Wanderer
On Hold: Hero of the Rune

Radiant

The easiest way to use "break" effects is putting the loop in a function and using "return".

SMF spam blocked by CleanTalk