Control flow
- In their natural order, programs execute sequentially. But sometimes you want to control the flow of execution. This can be achieved via Control flow statements
- There are two types of control flow statements
Branching- When you want to execute a piece of code conditionally.Loop- When you want to execute a piece of code multiple times.
Branching
if statement
if statementis used for achieving if something is true do this or else do this. This is written as
def string product;
def real price;
// ... do something with product and price
if (product == "shirt"){
price = 0.8 * price; // Because shirts have 20% discount
} else {
price = 0.9 * price; // Everything else has 10% discount
}
- You can also chain
if statementsoptionally.
def real myScore;
def real friendScore;
def string emotion;
if (myScore < friendScore){
emotion = "sad";
} else if (myScore > friendScore){
emotion = "happy";
} else {
emotion = "neutral";
}
- Note that the
else statementis completely optional.
switch statement
switch statementare used to match a variable against a series of values and executes the first matching statement.
def string product;
def real price;
// ... do something with product and price
switch (product) {
case "shirt": {
price = 0.8 * price; // shirts have 20% discount
}
case "trouser": {
price = 0.85 * price; // trousers have 15% discount
}
case "purse": {
price = 0.7 * price; // purses have 30% discount
}
default: {
price = 0.9 * price; // Everything else has 10% discount
}
}
- Note that the
defaultclause is completely optional.
Looping
for-i loop
for-iloop repeats till a specified condition is met. After every iteration, a specified update operation is performed.
for (def int i = 0; i < 10; i += 1){
if (i % 2 == 0){
log(i);
}
}
- The above example prints all the even numbers less than 10;
- From the above example, we can see that the
for-i loophas the following syntax:
for ([initialization]; [condition]; [updation]){
[body]
}
- The order of execution is [initialization], [condition], [body], [updation], [condition], [body], [updation] ... till the time [condition] evaluates to false;
Tip
Theoretically speaking all 4; [initialization], [condition], [body] and [updation], are optional. Although, if any of them are not present, it's probably better to use a different kind of loop.
for-in loop
For-in loopis used to loop over each element of aListor aMap.
def List<string> fruits = ["apple", "banana", "cherry"];
for (def fruit, def position) in fruits {
log(fruit);
}
- In the above example, the
for-in looploops over the listfruits.- First iteration:
fruit = "apple",position = 1 - Second iteration:
fruit = "banana",position = 2 - Third iteration:
fruit = "cherry",position = 3
- First iteration:
- To achieve the same thing with
for-iloop we would have to write:
def List<string> fruits = ["apple", "banana", "cherry"];
for (def position = 0; position <= fruits.length(); position += 1>) {
def string fruit = fruits[position];
log(fruit);
}
- As we can see from the above two examples,
for-inloops are much simpler to use when we need to only loop over aListor aMap
while loop
whileloops repeat until the condition evaluates to false;
def int i = 0;
while (i < 10){
if (i % 2 == 0){
log(i);
}
i += 1;
}
- In the above example, the
while looprepeats till i is less than 10;
Tip
Notice the similarities between the for-i loop and while loop. You can see that a while loop is like a for-i loop except that the [initialization] and [updation] steps are written at a different place.
Tip
While loop first checks the condition and then executes the body. This makes it entry controlled loop. Sometimes you might want to first execute the body and then check the condition. This is called exit controlled loop and can be achieved via do-while loop (described below).
do-while loop
- A
do-while loopis extremely similar to awhile loopexcept that the condition is checked after the execution of the body.
def int i = 0;
do {
if (i % 2 == 0){
log(i);
}
i += 1;
} while (i < 10);
- If you notice carefully, a
do-whileloop always executes at least once.
break and continue statement
breakandcontinueare not loops, but special statements that can only be used inside a loop.break- This means that we are done with the loop; now execute the first statement outside the loop. Think ofbreakas I want to break out of the loop.continue- This means that we are done with the current iteration of the loop; now execute the next iteration of the loop. Think ofcontinueas I want to to continue to the next iteration of the loop.
def int i = 0;
while (i < 10){
if ( i == 6){
break;
}
if (i % 2 == 0){
log(i);
}
i += 1;
}
log("out");
- In the above example, the output will be:
0
2
4
out
def int i = 0;
while (i < 10){
if ( i == 6){
continue;
}
if (i % 2 == 0){
log(i);
}
i += 1;
}
log("out");
- In the above example, the output will be:
0
2
4
8
out
- Sometime you might want to break out of or continue with the next iteration of the outer loop. This can be achieved by
labelled breakandlabelled continue.
def int i = 1;
outer: while (i < 5){
def int j = 1;
while (j < 5){
if (i == 3 && j == 3){
break outer;
}
log("i is: ", i, " and j is: ", j);
j += 1;
}
i += 1;
}
log("out");
- (Notice the use of
outer:next to thewhile loop. This means that we have labelled the loop as outer.) In the above example, the output will be:
i is: 1 and j is: 1
i is: 1 and j is: 2
i is: 1 and j is: 3
i is: 1 and j is: 4
i is: 2 and j is: 1
i is: 2 and j is: 2
i is: 2 and j is: 3
i is: 2 and j is: 4
i is: 3 and j is: 1
i is: 3 and j is: 2
out