Control Statement
MCScript control statements almost inherit Java control statements, but have the following limitations.
- If statement braces cannot be omitted
- Since there is no label, multiple loops cannot be escaped
The reason why the above functions were not implemented is that there is no problem even if it is not, and it may reduce the readability of the source.
However, in MCScript, if and switch can be handled as expressions and the extension for is further enhanced, convenience has improved. In this section, we will explain in detail such control statements.
Conditional Branch
MCScript conditional branching is not significantly different from Java.
if
MCScript if can omit parentheses.
x = 1
if x == 1 {
do()
}
Use elif instead of else if.
x = 1
if x < 0 {
do1()
} elif x > 0 {
do2()
} else {
do3()
}
It can also be handled as an expression.1
x = 1
abs = if x < 0 { -x } else { x }
switch
MCScript switch is similar to SwitchExpression implemented in Java12.
str = "one"
num = switch str {
"one" -> 1
"two" -> {
2
}
default -> 0
}
When multiple cases are combined, they must be enclosed in parentheses.
day = "Sunday"
switch day {
("Saturday", "Sunday") -> Println("It's a holiday");
default -> Println("It's a weekday");
}
And the biggest difference from Java is that there is no type restriction. In MCScript, all cases are compared and executed using the equals method.
class = int
switch class {
String -> Println("It's a String");
int -> Println("It's a int");
boolean -> Println("It's a boolean");
default -> Println("Don't know type: " + class);
}
Loop
MCScript for and while have the same role as Java, but there are some changes to the syntax, such as omitting parentheses.
for
You can write for statements in the conventional way. MCScript basically recommends the use of RangeObject, which is introduced below, in terms of performance and readability.
for i = 0; i < 10; i += 1 {
Println(i)
}
for-each
In for-each, use in instead of colon.
list = [1, 2, 3]
for i in list {
Println(i)
}
You can get an index by describing two variables.
list = ["a", "b", "c"]
for i, str in list {
Println(i + ": " + str)
}
Maps can also be processed for-each.
map = { 1:"a", 2:"b" }
for k, v in map {
Println(k + ":" + v)
}
If there is only one variable, the key is assigned.
RangeObject
In MCScript, RangeObject is generated by connecting numerical values with two dots. RangeObject works as an integer Iterable.
for i in 0..3 {
Println(i)
}
/*
0
1
2
3
*/
for i in -3..0 {
Println(i)
}
/*
-3
-2
-1
0
*/
Using until instead of a dot creates a RangeObject that does not contain the last value.
for i in 0 until 3 {
Println(i)
}
/*
0
1
2
*/
for i in -3 until 0 {
Println(i)
}
/*
-3
-2
-1
*/
You can specify the amount of change with step.
for i in 0..10 step 3 {
Println(i)
}
/*
0
3
6
9
*/
for i in 10..0 step 3 {
Println(i)
}
/*
10
7
4
1
*/
while
There is no change in the behavior of while, but there is an abbreviated notation of infinite loop
while {
# infinite loop
}
1. For simple processing, conditional operators can be used to write concisely. ↩