Plange


The if keyword is used create branches in the flow of the program.

Example

if (input("What is 1 + 1 ? ") = "2") {
	print("You are correct!");
} else {
	print("Sorry, you are incorrect.");
}
			

The user is prompted to enter a number. If they answer correctly, the code between the first set of braces { } is executed. Otherwise, the statements in the second set of braces is. Clauses can be chained together.

Example

choice := | coerce(input("Chose a number: ")) |;
if (choice < 100) {
	print("That's a nice number.");
} elif (choice < 10000) {
	print("Great.");
} elif (choice < 1000000000) {
	print("Huge!");
} else {
	print("Let's not get carried away. o_O")
}
			

The simplest if statement; one without any elif or else clause

Example

buffer ← input();
if (buffer != "") {
	print(buffer); //echo the input back
}
			

Looking carefully at the syntax of if, see that the use of expressions does not require that { } are used. Interestingly, things like this are possible:

Pseudocode

resultString := if (success) "All is well." else "Something went wrong.";
			

Further, the void-function evaluation of the earlier examples is not implied by the if. It's only implied by being in a statement_scope. Another example is in order:

Pseudocode

myFunction := if (!override) { print(5 + 5); } else { print("ABC"); };
print(type_of(myFunction)); //prints "Void → Void"
			

Note that the above program does not print "10" or "ABC", because evaluation of the statements inside curly braces does not occur until:

Pseudocode (continued)

myFunction := if (!override) { print(5 + 5); } else { print("ABC"); };
print(type_of(myFunction)); //prints "Void → Void"
myFunction;
			

This provides a nice segue into a discussion on statement_scope (syntax), specifically that if a program statement is an expression that evaluates to a Void → Void function then that function is invoked as a step in evaluating the statement.


copyright © Brent Lewis 2017