• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!

"Continue" function in loops.

Status
Not open for further replies.
Ever wonder what to do in JASS in place of a "continue" function in a loop? This used to bother me a few years ago but the solution is simple when you think of it.

Problem:

JASS:
loop
    //do stuff
    if condition then
        //continue
    endif
    //do stuff
    exitwhen loopIsDone
endloop

Solution:

JASS:
loop
    loop
        //do stuff
        if condition then
            exitwhen true //continue
        endif
        //do stuff
        if loopIsDone then
            set exit=true
            exitwhen true
        endif
    endloop
    exitwhen exit
endloop

Hope this inspires!
 
Or structure your code properly so you don't need continue.

My example didn't show a case where it was actually needed, but I have run into structures where the complexity of what Is working with required a continue or nothing else. It is not always possible given an if/then statement.

@Adiktuz

A continue statement takes you to the top of a loop. My example had several ways out, but like I said the resources I've worked with sometimes have gotten into really complex if/then blocks and I'd wished for a continue statement. This example is very simple on purpose, but it's only for people who need it.
 

Cokemonkey11

Spell Reviewer
Level 30
Joined
May 9, 2006
Messages
3,544
You guys really meet this with a lot of negativity. If you don't need it, disregard the post. You are all completely ignoring what I've said about the example being simple but the need being more complex than what I've shown. I'll see if I can look through my resources and show you what I mean.

It's nothing personal, I just can't think of a case where the nested loop

  • looks better
  • performs the same function more efficiently
  • is easier to read
  • is easier to write

than a single looped structure that performs the same logical operation.

Maybe I'll eat my words when you post a more concrete example, though.
 
Level 17
Joined
Apr 27, 2008
Messages
2,455
Many textbooks caution readers not to use break and continue because it causes the execution flow to jump around. While this is certainly true, judicious use of break and continue can actually help make loops much more readable.
In other words, using break and continue is usually not recommended because humans are ... humans, the clearer, the better, less bug proof.
Now, here i suppose we need a better example (pseudo code) and then how it will be in jass.
 
My example didn't show a case where it was actually needed, but I have run into structures where the complexity of what Is working with required a continue or nothing else. It is not always possible given an if/then statement.

@Adiktuz

A continue statement takes you to the top of a loop. My example had several ways out, but like I said the resources I've worked with sometimes have gotten into really complex if/then blocks and I'd wished for a continue statement. This example is very simple on purpose, but it's only for people who need it.

what I mean was that this:
JASS:
loop
    loop
        //do stuff
        if condition then
            exitwhen true //continue
        endif
        //do stuff
        if loopIsDone then
            set exit=true
            exitwhen true
        endif
    endloop
    exitwhen exit
endloop
can just be
JASS:
loop
    loop
        //do stuff
         exitwhen condition //continue
        //do stuff
        if loopIsDone then
            set exit=true
            exitwhen true
        endif
    endloop
    exitwhen exit
endloop
right? or is using the if faster?
 
Level 19
Joined
Aug 8, 2007
Messages
2,765
JASS:
loop
    loop
        //do stuff
         exitwhen condition //continue
        //do stuff
        if loopIsDone then
            set exit=true
            exitwhen true
        endif
    endloop
    exitwhen exit
endloop

one more opt set

JASS:
loop
    loop
        //do stuff
        //do stuff
        exitwhen true 
    endloop
    exitwhen (standard conditions) or exit
endloop

I do agree that JASS code shouldn't need continue/break functions. After two years and 100k+ lines, never needed em once. (Use em all the time in Java but thats with string/file parsing)
 
Level 14
Joined
Jun 27, 2008
Messages
1,325
Yea well it does the same as break, but you dont really use it.

Using break only makes sense in combination with a condition, otherwise your loop always breaks, which isnt the point of a loop. So usually:

Code:
while(blabla) {
    if (something happens) {
        break;
    }
    bla bla bla..
}

You can do the same in jass:
JASS:
loop
    if (something happens) then
        exitwhen true
    endif
    bla bla bla..
endloop

which however is equal to the much more convenient

JASS:
loop
    exitwhen (something happens)
    bla bla bla..
endloop

So yea, its equal, but as the jass syntax for loops makes breaks unnecessary there is no point in using breaks .. i guess.
 
Status
Not open for further replies.
Top