• 🏆 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!

Loop Questions

Status
Not open for further replies.
Level 15
Joined
Oct 16, 2010
Messages
941
1. how do I setup a double condition for an exitwhen statement? Whenever I try and do [exitwhen condition1 == true or condition2 == true] it glitches up and won't run the loop.

2. Alternatively, is it possible in Vjass to force exit a loop? i.e. an "exitloop" function that would immediately stop the loop. I can't seem to find a keyword for it but i'm sure there has to be one.

Thanks ahead of time
 
1. exitwhen condA and/or condB should work fine. Perhaps one of the conditions are being met before the loop starts?

2. Well, I don't think there is an exitloop, but just satisfy one of the conditions, or add your own condition. For example:
JASS:
function lala takes nothing returns nothing
    //pointless function
    local integer i = 0
    loop
        exitwhen i == 5064
        set i = i + 1 
        if GetTriggerUnit() != null then
            set i = 5064 //effectively exit the loop
        endif
    endloop
endfunction

EDIT: DSG's method for #2 is better. I forgot about that.
 
Last edited:

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,202
1. As the and and or statmenets generate a boolean it is perfectly fine to use them with it. If it is not working, then you must have messed up the statements (logic bug). You can however mimic them via multiple exitwhen statements and negation (not which inverts the boolean) but this is logically slower as I doubt the jass compiler optimizes at all.

2. You are allowed many exitwhen statements. "exitwhen true" will instantly break out of the loop (as true is a boolean statement and it accepts any boolean statement).

The exitwhen statement uses any boolean statement (even a constant one like true). Remember that it exits when true is passed to the statement (exitwhen false is a usless statement). You can use as many exitwhen statements as you want at any time you want as long as they are inside a loop.

The and statement is false all the time except when both sides are true. The or statement is true all the time except when both sides are false.
 
Level 10
Joined
Mar 31, 2009
Messages
732
Sorry to dredge up this thread, but I recently found myself pondering the use of this exitwhen statement.
It it safe for me to think of the statement as meaing something more like "exit if", or does this "when" part of its name suggest it actually continually watches for its condition to be met when at another part of the loop.

Eg say I run this code

JASS:
loop
 exitwhen a = true
 // do something
 // do something else
 set a = true
 // do something again
 // do more stuff
endloop

Will the runtime ever get to the // do something again part?
 
Level 20
Joined
Jul 6, 2009
Messages
1,885
@Teelo,
It will execute both parts once in your example because the exitwhen is on loop start. Loop executes functions in order,when it comes to exitwhen it will stop the loop if the boolean statement is true.
So if you had
JASS:
loop
// do something
// do something else
exitwhen true
// do something again
// do more stuff
endloop

It would execute only the functions above the exitwhen part.
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,202
Teelo, if you run that code, WC3 will quit to main menu as there is a syntax error...

exitwhen a = true
This is invalid. "=" is not a comparator. All comparators are ==, !=, <, >, <=, >=.

Furthor more that is a stupid thing to do.

exitwhen a
The above is perfectly valid (a is a boolean statement) and does exactly what you wanted (exists when the variable a contains true).
 
Level 22
Joined
Sep 24, 2005
Messages
4,821
1. how do I setup a double condition for an exitwhen statement? Whenever I try and do [exitwhen condition1 == true or condition2 == true] it glitches up and won't run the loop.
Use the keywords "and" or "or". Note that these are logical conjunctions.
read more at wikipedia
2. Alternatively, is it possible in Vjass to force exit a loop? i.e. an "exitloop" function that would immediately stop the loop. I can't seem to find a keyword for it but i'm sure there has to be one.
Yes its possible, use the "return" keyword.

JASS:
function Nonesense takes nothing returns nothing
  local integer i=0
  local integer j=10
  local boolean abort=false
  
  loop
    set i=i+1
    set j=j-1
    // Question number 1
    exitwhen i==j and j==i
     if abort then
      // Question number 2
      return
     endif
    endloop
    
endfunction
 
Level 31
Joined
Jul 10, 2007
Messages
6,306
Also, you don't need the ==true there. For true, just use the variable. For false, use not variable.

Also, if you don't need a comparison, don't use it -.-. A comparison would be slightly slower (not much difference really) than just a straight out boolean.

JASS:
function Test takes nothing returns nothing
    local boolean a = true

    loop //exit immediately as a is true
        exitwhen a
    endloop

    loop //infinite as a is true atm
        exitwhen not a
    endloop
endfunction
 
Status
Not open for further replies.
Top