- Joined
- Dec 14, 2005
- Messages
- 10,532
Conditional statements allow the programmer to specify when an action or set of actions should occur--they can control the execution of code if a condition (in the form of a boolean) is true.
The If statement
JASS provides one fairly powerful conditional statement: the if statement. It allows you to run code if a condition is true, and optionally run code if it is not or if other conditions are instead.
Its syntax runs like this:
For example, suppose we want to write a function to compute |x| for an integer x--that is, the absolute value of x (|x>=0| = x, |x<0| = -x).
If x is greater than or equal to 0, x is returned as-is. Otherwise (if x is less than 0), -x is returned.
Practice: Based on your knowledge of how returns work, rewrite this function without using else.
elseif
Sometimes you want to execute the "else" only if another condition is true. Suppose we want to modify our AbsoluteValue function to return |x|+1 if x<-5.
With the above model, we might approach it like this:
However, that clearly gets messy fast as you add more conditions to the function. As such, a form of else which can itself take a condition exists, called "elseif". Rewriting the above example using elseif would yield:
However, as the name implies, the elseif will not be executed unless previous elseifs (you can have as many as you want in an if statement above the "else" (if applicable)) and the if itself returned false. Using this to our advantage, we could rewrite the function above to range from largest number cases to smallest without making it any more complicated:
The If statement
JASS provides one fairly powerful conditional statement: the if statement. It allows you to run code if a condition is true, and optionally run code if it is not or if other conditions are instead.
Its syntax runs like this:
JASS:
if <condition> then
//code goes here
//optional from here on
else
//more code goes here
endif
For example, suppose we want to write a function to compute |x| for an integer x--that is, the absolute value of x (|x>=0| = x, |x<0| = -x).
JASS:
function AbsoluteValue takes integer x returns integer
if x >= 0 then
return x
else
return -x
endif
endfunction
If x is greater than or equal to 0, x is returned as-is. Otherwise (if x is less than 0), -x is returned.
Practice: Based on your knowledge of how returns work, rewrite this function without using else.
elseif
Sometimes you want to execute the "else" only if another condition is true. Suppose we want to modify our AbsoluteValue function to return |x|+1 if x<-5.
With the above model, we might approach it like this:
JASS:
function AbsoluteValue takes integer x returns integer
if x >= 0 then
return x
else
if x < -5 then
return -x + 1
else
return -x
endif
endif
endfunction
However, that clearly gets messy fast as you add more conditions to the function. As such, a form of else which can itself take a condition exists, called "elseif". Rewriting the above example using elseif would yield:
JASS:
function AbsoluteValue takes integer x returns integer
if x >= 0 then
return x
elseif x < -5 then
return -x + 1
else
return -x
endif
endfunction
However, as the name implies, the elseif will not be executed unless previous elseifs (you can have as many as you want in an if statement above the "else" (if applicable)) and the if itself returned false. Using this to our advantage, we could rewrite the function above to range from largest number cases to smallest without making it any more complicated:
JASS:
function AbsoluteValue takes integer x returns integer
if x >= 0 then
return x
elseif x >= -5 then //x < 0 because if it is not the above case already executed and the if ends.
return -x
else
return -x + 1
endif
endfunction
Last edited: