Loops and blocks

An important part of a programming language is to be able to control the program flow. For simplicity, P* has been inspired by other languages and supports the common loop- and conditional blocks.

If block

The if, else if and else blocks is probably the most widely used flow control blocks of all time. The code in an if block is only run if the condition you specify is true. If it is false, the condtition of the next else if is checked, and that block's code is run if the condition was true.

You can specify as many else if blocks as you want. When the first true condition is found, that block is run, and none of the remaining conditions below are checked. Never is more than one block in an if-else if-else construct run.

In the end, you can specify an else block. This block doesn't have any condition, and is run if all the other conditions was false.

Try to run this program and replace Banana on the third line with different values:

SCENE main {
	/* Set the string my_fruit to contain Banana */
	string my_fruit = "Banana";

	/* This string will contain the color of the fruit, but it's empty for now */
	string color;

	if (my_fruit == "Banana") {
		/* The fruit was a Banana, set color to "yellow" */
		color = "yellow";
	}
	else if (my_fruit == "Orange") {
		/* The fruit was an Orange, set color to "orange" */
		color = "orange";
	}
	else {
		/* The fruit is neither a Banana or an Orange, we don't know it's color */
		color = "unknown";
	}

	echo "The color of the fruit $my_fruit is $color\n";
}

While loop

A while loop is run many times while the specified condition is true. You can use anything as a condition, but in this example, we use only the number 1 as condition, which makes it run indefinitly. Inside the loop below, we have put a break statement which makes the program immediately jump out of the loop and continue below.

This kind of construct is useful to make the code more readable if we hav many different conditions to check for. In this loop we increment the variable counter by 1 at the end of every run. If the counter is less than 5, we print one message, and if it's more than five but below ten we print another message. When the counter reaches 10, we break out of the loop. Try to run it!

SCENE main {
	/* Set our counter to zero */
	int counter = 0;

	/* This loop runs indefinitly untill we end it with a break */
	while (1) {
		if (counter < 5) {
			/* Check if counter is less than five */
			echo "The counter is currently less than five\n";
		}
		else if (counter < 10) {
			/* Check if counter is less than ten */
			echo "The counter is currently less than ten\n";
		}
		else {
			/* Counter is not less than ten if we end up here */
			echo "The counter is now 10, end the loop now\n";
			break;
		}

		/* Increment the counter */
		counter++;
	}
}

For loop

The for loop is mostly used to keep a counter which increments each run and then end when it reaches a certain value. In addition to the condition the while loop has, it provides two extra fields: One which is run before the loop starts the first time and one which is run at the end of each iteration.

The example below gives the same output as the program with the while loop. Note that we have put the counter = 0 and counter++ expressions in a different location, but they are still run at the same time as in the while loop.

SCENE main {
	/* Create the counter variable */
	int counter;

	/* Set counter to zero and loop while the counter is less than five */
	for (counter = 0; counter < 5; counter++) {
		echo "The counter is currently less than five\n";
	}

	/* Do nothing the first run (counter is still the same as when we
	ended the first loop), and run while the counter is less than 10. */
	for (0; counter < 10; counter++) {
		echo "The counter is currently less than ten\n";
	}

	echo "The counter is now 10, end the loop now\n";
}

A while can always be replaced by a for loop and vice versa, choose the one which makes the code easier to read.

Foreach loop

The foreach loop is meant to further simplify how we control a loop. The condition of a foreach loop may contain a list or range operation. A variable is specified used to store the value we get from the condition, and the loop runs untill all values in the list have been used once.

Go ahead and check if the program below produces the same output as the two previous programs!

SCENE main {
	/* Create the counter variable */
	int counter;

	/* Loop values 0 through 4 and put the value into the counter for each run */
	foreach (counter; 0..4) {
		echo "The counter is currently less than five\n";
	}

	/* The counter is now four, but since we've already done that number,
	we use the exclusive range operator ... (three dots) which keeps out
	the lowest and highest number of the range. This loop will run from 5
	to 9. */
	foreach (counter; counter...10) {
		echo "The counter is currently less than ten\n";
	}

	/* The counter never actually becomes 10, but we just keep this message anyway */
	echo "The counter is now 10, end the loop now\n";
}