Loops and branches in Gox language-GX14

The loops and branches implemented in Gox language are relatively simple and easy to understand. The only thing to note is that else if needs to be written as "elif", the other basics are similar to the Go language.

The following is quoted from Qlang's Github page ,

for 语句
for { // 无限循环,需要在中间 break 或 return 结束
	...
}

for booleanExpr { // 类似很多语言的 while 循环
	...
}

for initExpr; conditionExpr; stepExpr {
	...
}

typical example:

for i = 0; i < 10; i++ {
	...
}

In addition, Gox also supports for...range syntax:

for range collectionExpr { // 其中 collectionExpr 可以是 slice, map 或 chan
	...
}

for index = range collectionExpr {
	...
}

for index, value = range collectionExpr {
	...
}

Conditional branches are mainly if...else... and switch branches:

if statement

if booleanExpr1 {
	// ...
} elif booleanExpr2 {
	// ...
} elif booleanExpr3 {
	// ...
} else {
	// ...
}

switch statement

switch expr {
case expr1:
	// ...
case expr2:
	// ...
default:
	// ...
}

or:

switch {
case booleanExpr1:
	// ...
case booleanExpr2:
	// ...
default:
	// ...
}

 


The following is how to write loops and branches when using Anko engine.

In the Gox language, the loop implemented is mainly a for loop, and the conditional branch supports if...else... and switch statements. It is relatively simple. Take a look at the following example to get a rough idea.

i = 0
for {
	println(i)
	i++
	if i > 1 {
		break
	}
}

println("")

for i in [0, 1] {
	println(i)
}

println("")

for key, value in {"a": "b"} {
	println(key, value)
}

println("")

m = {"a": "b", "c": 3}

for k in keys(m) {
	println(k, m[k])
}

println("")

i = 0
for i < 2 {
	println(i)
	i++
}

println("")

for i = 0; i < 2; i++ {
	println(i)
}

println("")


for i = 0; i < 10; i++ {
	println(i)
	if i < 1 {
		continue
	}

	break
}

println("")

for i in range(7) {

	if i > 2 && i < 5 {
		println(i)
	} else {
		printfln("not in range: %v", i)
	}
}

println("")

aryT = [3, "abc", true, 2.8]

for v in aryT {
	printf("v: %v ", v)
	switch v {
	case typeOf(v) == "bool":
		println("is bool:", v)
	case typeOf(v) == "string":
		println("is string")
	case 2.8, 3:
		println("is number")
	default:
		println("unknown:", v)
	}
}

Although only for loops are supported, the use of several for loops can basically support do...while..., until and other loops in other languages, and it also supports traversal of arrays or maps.

The switch statement is also very simple to implement. It does not even support break or fallthrough.

The execution result of this code is:

λ gox scripts\loopAndIf.gox         
0                                   
1                                   
                                    
0                                   
1                                   
                                    
a b                                 
                                    
a b                                 
c 3                                 
                                    
0                                   
1                                   
                                    
0                                   
1                                   
                                    
0                                   
1                                   
                                    
not in range: 0                     
not in range: 1                     
not in range: 2                     
3                                   
4                                   
not in range: 5                     
not in range: 6                     
                                    
v: 3 is number                      
v: abc is string                    
v: true is bool: true               
v: 2.8 is number                    

Since Gox language supports dynamic types, the switch statement may cause some problems, so you need to pay attention when using it.

Guess you like

Origin blog.csdn.net/weixin_41462458/article/details/107899934