Conditional statements and loops are similar to other languages. However, in Ginjo scopes always need to be marked explicitly using braces { }. Also note that the ++ -- == ?: operators are purposefully missing in Ginjo. The = operator is context dependant. It is a comparison operator inside conditional statements and an assignment operator otherwise. See Operators for further details.
You must always use braces { } after any if, elseif or else statement.
if(testme=3){ //do something here } elseif(testme>0 && testme<3){ //do something here } else{ //do something here }
There is no "fall through" behaviour in switch statements. (Note that the break keyword has no effect in a switch like in C/C++/C#.) Only one case or default clause is executed. case clauses accept either a single value or a comma separated list of values. Moreover, switch statements can also be used with strings.
//test the integer variable testme switch(testme){ case(1,2,3){ //do something here } case(3){ //this will never execute; 3 is already handled in the case above } default{ //do something here } } //test the string variable testme switch(testme){ case('Ginjo', 'Builder'){ //do something here } case('website'){ //do something here } default{ //do something here } }
The counter variable (i in the below example) can be declared either inside the for statement or beforehand. The continue keyword can be used to skip onto the next iteration of the loop, while the break keyword can be used to terminate the loop prematurely. In the following example the variable total will have a value of 6 after the loop executes.
var:int32 total=1 for(var:int32 i=0; i<12; i+=1){ if(i=0){continue} elseif(i<4){total*=i} else{break} }
The foreach loop iterates over each element of a group. The data type of the group must be a reference type that refers to a collection or an array that's enumerable. This means that group is an object that implements the IEnumerable interface of the System.Collections namespace or the IEnumerable(of T) interface of the System.Collections.Generic namespace. In Ginjo both the semi-colon ( ; ) and the in keyword are recognized as separators delimiting the element of a group. The element variable (str in the below example) can be declared either inside the foreach statement or beforehand. The continue keyword can be used to skip onto the next iteration of the loop, while the break keyword can be used to terminate the loop prematurely.
var:Collections.Generic.List(of string) lst=new Collections.Generic.List(of string) lst.Add('Example text 1') lst.Add('Example Ginjo text') lst.Add('Example text 2') foreach(var:string str in lst){ if(str.Contains('Ginjo')){ //do something here } }
A while loop tests for condition each time prior to executing a block of code. In contrast, the do ... while loop executes a block of code, and then tests for the condition. The continue keyword can be used to skip onto the next iteration of the loop, while the break keyword can be used to terminate the loop prematurely. The following example is a rewrite of the for loop example using the while loop.
var:int32 total=1 var:int32 i=0 while(i<12){ if(i=0){continue} elseif(i<4){total*=i} else{break} i+=1 }