4. Control Structures Like most languages, GSL knowns the if statement. Syntax: if(condition) thenStatement
else elseStatment
The semantics is the common one: If <condition> evaluates to true, then thenStatement is executed, else elseStatement is executed. The else statement may be missing, then nothing is done in the case that <condition> is false.
If more than a statement should be executed either in the then part or the else part, then the statements have to be grouped to a statement block by surrounding them with curly brackets (See also: Statement Blocks). Example: int i = ...;
int j = ...;
if(i>j)
echo("i is greater than j"); //only one statement in then part: no brackets needed
else { //two statements in else part: curly brackets needed
echo("damn :(");
echo("i is less or equal to j");
}
Note that GSL also supports The Conditional Expression Operators ?: The while loop executes a statement repeatedly as long as a condition is try. The syntax is the following: while(condition)
loopStatement
<condition> is checked. If it is true, then <loopStatment> is executed. This is done repeatedly as long as <condition> stays true. If <condition> is false from the beginning on, then <loopStatement> is not executed at all.
Example: int i = 0;
while(i<5){
echo(" " + i);
i++;
}
This prints " 0 1 2 3 4" to the console.The syntax is exactly the one from C/JAVA: for(init;condition;afterLoop)
loopStatement
The semantics are as following:
1.) <init> is executed. It commonly contains definitions and initialisations for loop variables.
2.) <condition> is evaluated. If it is false, then then nothing is done, otherwise go to to 3.)
3.) <loopStatement> is executed, followed by <afterLoop>.
4.) Go back to 2.)
Example: for(int i=0;i<5;i++) echo(" " + i);
This prints " 0 1 2 3 4" to the console.The foreach loop is a very convenient way to iterate over:
The syntax is always the same: for(iterator: iterateOver)
loopStatement
<iterateOver> is an array/struct/comma separated list over which should be iterated.
<iterator> is a variable (or a variable declaration) of type string (or int in case of an array). In each loop cycle the next key/entry is assigned to <iterator>. Then, <loopStatement> is evaluated.
Note that the keys over which will be iterated are saved at the first loop pass. So if you alter <iterateOver> inside of the loop by example adding new keys, these new keys won't be iterated over. If <iterateOver> is null, then the loop will be skipped. Iterating over the keys of an array: Arrays can contain int and string keys. If <iterator> is of type string, then the loop will only iterate over the string keys. If <iterator> is of type int, then the loop will only iterate over the int keys. For integer iterations, the loop will iterate sorted from the lowest to the highest key. For string iterations, the loop will iterate in the order in which the keys were inserted into the array. Example: array a = array();
a[0] = "a";
a[5] = "b";
a[-35] = "c";
for(int i: a)
echo(a[i]);
This prints "cab" to the console, since the loop starts with the lowest key (-35).
Example: array a = array();
a["x"] = "a";
a["xx"] = "b";
a["xxx"] = "c";
for(string s: a)
echo(a[s]);
This prints "abc" to the console, since the values were inserted into the array in that order.
Iterating over members of a struct: For structs, the foreach loop only works with a string iterator (since structs contain no members with int keys). In each loop cycle, the iterator will be assigned with the name of the next member. You can then access the members using the array syntax (a[ b ]), which also works for structs (See also: Interchangeability of Array and Struct Syntax). The members will be iterated in the order in which they are declared in the struct's definition. Example: typedef Point3D struct{
int x;
int y;
int z;
}
Point3D p = Point3D();
p.x = 5;
p.y = 6;
p.z = 2;
for(string s: p)
echo(s + " = " + p[s] + "; ");
This prints "x = 5; y = 6; z = 2; " to the console.
Iterating over the entries of a comma separated list: A comma separated list is a string, in which values are seperated by commas (","). Each string can be inserted as <iterateOver> and treated as a comma separated list. If the input string is empty, then no loop cycle will be executed. If the input contains no comma, then one loop cycle with the whole string as iterator will be executed. If the input contains commas, then in each cycle the iterator is one part between commas. The commas themself never occur in the iterator. If two commas appear consecutively, then a loop cycle with empty string ("") as iterator will be executed. Examples: string list1 = "";
for(string s: list1)
echo("woot");
Won't output anything since list1 is empty
string list1 = "foobar";
for(string s: list1)
echo("woot" + s + " ");
Will output "wootfoobar ", loop body executed once.
string list1 = ",foo,,bar";
for(string s: list1)
echo("woot" + s + " ");
Will output "woot wootfoo woot wootbar ", loop body executed four times.The statement "break;" can be used, to leave a loop (for, foreach, while) immediately. Example: string list1 = ",foo,,bar";
for(string s: list1){
echo("woot" + s + " ");
break;
}
will only ouput "woot " since the loop is left after the first execution.The statement "continue;" can be used in any loop, to skip the remaining actions in the loop body and return back to the loop head, starting the next loop cycle. In for loops: <afterLoop> is executed, then the condition is rechecked. If it is true the next cycle starts. In while loops: the condition is rechecked, if it is still true the next cycle starts. In foreach loops: The next key/entry is loaded to the iterator and the next cycle starts. Example: string list1 = ",foo,,bar";
for(string s: list1){
if(s == "") continue;
echo("woot" + s + " ");
}
will only ouput "wootfoo wootbar " since all cycles where s equals "" are skipped.
|