From 91d62f00326126444ad65ef29a4b1359f41f4c63 Mon Sep 17 00:00:00 2001 From: Rafal Kupiec Date: Tue, 17 Jul 2018 15:37:43 +0200 Subject: [PATCH] Update page 'P# 1.0 Draft Specification' --- P%23-1.0-Draft-Specification.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/P%23-1.0-Draft-Specification.md b/P%23-1.0-Draft-Specification.md index da5fc69..95e73d2 100644 --- a/P%23-1.0-Draft-Specification.md +++ b/P%23-1.0-Draft-Specification.md @@ -357,8 +357,37 @@ The above expression in PHP will output 't' and this is an unexpected result, bu ## 7. Flow Control ### 7.1. If Statement +The if statement has the following general form: + + if (expression) + statement + +The if keyword is used to check if an expression is true. If it is true, a statement is then executed. The statement can be a single statement or a compound statement. A compound statement consists of multiple statements enclosed by curly brackets. + +Often there is a need to execute a statement if a certain condition is met, and a different statement if the condition is not met. This is what else is for. Else extends an if statement to execute a statement in case the expression in the if statement evaluates to FALSE. For example, the following code would display a is greater than b if $a is greater than $b, and a is NOT greater than b otherwise: + + <% + if ($a > $b) { + print("a is greater than b"); + } else { + print("a is NOT greater than b"); + } + %> + +Additionally, P# implements known from PHP, the elseif statement. as its name suggests, is a combination of if and else. Like else, it extends an if statement to execute a different statement in case the original if expression evaluates to FALSE. However, unlike else, it will execute that alternative expression only if the elseif conditional expression evaluates to TRUE. For example, the following code would display a is bigger than b, a equal to b or a is smaller than b: + + <% + if ($a > $b) { + print("a is bigger than b"); + } elseif ($a == $b) { + print("a is equal to b"); + } else { + print("a is smaller than b"); + } + %> ### 7.2. Switch Statement +The switch statement is a selection control flow statement. It allows the value of a variable or expression to control the flow of program execution via a multiway branch. It creates multiple branches in a simpler way than using the if, elseif statements. ### 7.3. While Loop