Conditional Statements
You might want to include code that will only be executed if certain criteria are met (or if they are not). That’s what you need conditional Statements for.
If
An If-Statement is the first part of a conditional statement. It declares the condition that must be true for the following code to be executed. Be careful, you need TWO EQUALS when comparing values! (That’s because one equal is for declaring values!)
Else
An Else-Statement can be added to the end of a conditional Statement to declare what will be executed when the if-condition equals to false.
Things to check for
Supported Calculations are +
,-
,*
,/
,mod
,concatenation(~)
Supported Operands are Logical OR(||)
, Logical AND(&&)
, Bitwise OR(|)
, Bitwise AND(&)
, and Bitwise XOR(^)
The ? Operator
Surely, always typing out an if/else structure can be annoying. Especially if you just want to do an either or condition.
That’s why the ?
operator was implemented.
It follows the same logic as an if/else statement, it only is by far less code required.
Syntax: boolean ? if : else
Operators
You can use these operators. All the examples given evaluate to true.
Name | token | Explanation | Example |
---|---|---|---|
Name Not | token ! | Explanation Inverts a boolean | Example !false |
Name Not Equal | token != | Explanation Checks if the value before and after are not equal | Example 1 != 2 |
Name Equal | token == | Explanation Checks if the value before and after are equal | Example 1 == 1 |
Name Greater than | token > | Explanation Checks if the value before is greater than after | Example 1 > 2 |
Name Greater or Equal | token >= | Explanation Checks if the value before is greater than or equal with after | Example 1 >= 1 |
Name Lesser than | token < | Explanation Checks if the value before is fewer than after | Example 1 < 2 |
Name Lesser or Equal | token <= | Explanation Checks if the value before is fewer than or equal with after | Example 1 <= 1 |
Name Logical AND | token && | Explanation Checks if both before and after values are true, false if one or both are false | Example true && true |
Name Logical OR | token || | Explanation Checks if either the value before or after are true, false if neither are true | Example false |
Name Bitwise XOR | token ^ | Explanation Checks if exactly one of the before or after values is true, false if both or none are true | Example true ^ false |
Name Bitwise AND | token & | Explanation Performs a bitwise AND operation on the before and after values. See this for more | Example true && true |
Name Bitwise OR | token | | Explanation Performs a bitwise OR operation on the before and after values. See this for more | Example false |
Difference between |
and ||
(and &
and &&
)
The main difference between the single and the double, with semantics aside, is that the double performs a check after each condition and exits early - this is called short-circuiting. However, the single goes through the entire chain of conditions, even if the first one would have cancelled the entire condition. This not only saves resources, but also allows for easier scripting such as null checks and chained conditions.
The in/has Operator
The in
and the has
operator check if something is in something.
First you need the list you want to check in, then the in
/has
then the value you want to check for.
in
and has
are the same keyword for ZS, but in most cases people use has
for checking if a collection contains an item and in for loops as this represents the English grammar.
in/has loadedMods
You can check, if a mod is loaded by checking if it’s in the loadedMods list
in/has IIngredient
You can also check if an item matches a definition by comparing two IIngredients.
With this one you need to be a bit careful as not to confuse the two entries:
This is only true when the IIngredient AFTER the in
can also be found completely in the one BEFORE the in
.
In most cases you will use the has
keyword instead as it’s intention is more clear and it does exactly the same.
This is only true when ALL matching items from the IIngredient AFTER the has
can also be found in the IIngredient BEFORE has
:
Say we have an IIngredient that contains all dusts (e.g. redstone and glowstone dust):