Update page 'P# 1.0 Draft Specification'
@@ -387,17 +387,125 @@ Additionally, P# implements known from PHP, the elseif statement. as its name su
|
|||||||
%>
|
%>
|
||||||
|
|
||||||
### 7.2. Switch Statement
|
### 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.
|
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. The switch statement works with two other keywords: case and break. The case keyword is used to test a label against a value from the round brackets. If the label equals to the value, the statement following the case is executed. The break keyword is used to jump out of the switch statement. There is an optional default statement. If none of the labels equals the value, the default statement is executed.
|
||||||
|
|
||||||
|
<%
|
||||||
|
$domain = 'de';
|
||||||
|
switch ($domain) {
|
||||||
|
case 'us':
|
||||||
|
print("United States\n");
|
||||||
|
break;
|
||||||
|
case 'de':
|
||||||
|
print("Germany\n");
|
||||||
|
break;
|
||||||
|
case 'sk':
|
||||||
|
print("Slovakia\n");
|
||||||
|
break;
|
||||||
|
case 'pl':
|
||||||
|
print("Poland\n");
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
print("Other\n");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
%>
|
||||||
|
|
||||||
### 7.3. While Loop
|
### 7.3. While Loop
|
||||||
|
The while is a control flow statement that allows code to be executed repeatedly based on a given boolean condition. This is the general form of the while loop:
|
||||||
|
|
||||||
|
while (expression):
|
||||||
|
statement
|
||||||
|
|
||||||
|
The while loop executes the statement when the expression is evaluated to true. The statement is a simple statement terminated by a semicolon or a compound statement enclosed in curly brackets.
|
||||||
|
|
||||||
|
<%
|
||||||
|
int $i = 0;
|
||||||
|
while ($i < 5) {
|
||||||
|
print("P# Language\n");
|
||||||
|
$i++;
|
||||||
|
}
|
||||||
|
%>
|
||||||
|
|
||||||
### 7.4. Do-While Loop
|
### 7.4. Do-While Loop
|
||||||
|
The do while loop is a version of the while loop. The difference is that this version is guaranteed to run at least once.
|
||||||
|
|
||||||
|
<%
|
||||||
|
int $count = 0;
|
||||||
|
do {
|
||||||
|
print("$count\n");
|
||||||
|
} while ($count != 0)
|
||||||
|
%>
|
||||||
|
|
||||||
|
In above example, first the iteration is executed and then the truth expression is evaluated.
|
||||||
|
|
||||||
### 7.5. For Loop
|
### 7.5. For Loop
|
||||||
|
The for loop does the same thing as the while loop. Only it puts all three phases, initialization, testing and updating into one place, between the round brackets. It is mainly used when the number of iteration is know before entering the loop.
|
||||||
|
|
||||||
|
<%
|
||||||
|
string $days[] = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
|
||||||
|
"Saturday", "Sunday" };
|
||||||
|
int $len = sizeof($days);
|
||||||
|
|
||||||
|
for (int $i = 0; $i < $len; $i++) {
|
||||||
|
print("$days[$i]\n");
|
||||||
|
}
|
||||||
|
%>
|
||||||
|
|
||||||
|
The three phases are divided by semicolons. First, the $i counter is initiated. The initiation part takes place only once. Next, the test is conducted. If the result of the test is true, the statement is executed. Finally, the counter is incremented. This is one cycle. The for loop iterates until the test expression is false.
|
||||||
|
|
||||||
### 7.6. Foreach Loop
|
### 7.6. Foreach Loop
|
||||||
|
The foreach construct simplifies traversing over collections of data. It has no explicit counter. The foreach statement goes through the array one by one and the current value is copied to a variable defined in the construct. In P#, it can be used to traverse over an array.
|
||||||
|
|
||||||
|
<%
|
||||||
|
string $days[] = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
|
||||||
|
"Saturday", "Sunday" };
|
||||||
|
string $day;
|
||||||
|
foreach ($days as $day) {
|
||||||
|
print("$day\n");
|
||||||
|
}
|
||||||
|
%>
|
||||||
|
|
||||||
|
The usage of the foreach statement is straightforward. The $days is the array that gets iterated through. The $day is the temporary variable that has the current value from the array. The foreach statement goes through all the days and prints them to the console.
|
||||||
|
|
||||||
|
There is another syntax of the foreach statement. Below form will additionally assign the current element's key to the $key variable on each iteration.
|
||||||
|
|
||||||
|
<%
|
||||||
|
string $benelux[] = { 'be' => 'Belgium', 'lu' => 'Luxembourgh', 'nl' => 'Netherlands' };
|
||||||
|
string $key, $value;
|
||||||
|
foreach ($benelux as (string) $key => $value) {
|
||||||
|
print("$key is $value\n");
|
||||||
|
}
|
||||||
|
%>
|
||||||
|
|
||||||
|
Casting a $key to string in above example can be useful, because P# supports arrays with both numeric and associative indexes. This means that if above array contained additional row with integer-based key, it would be casted to string automatically.
|
||||||
|
|
||||||
### 7.7. Break & Continue Statements
|
### 7.7. Break & Continue Statements
|
||||||
|
The break statement is used to terminate the loop. The continue statement is used to skip a part of the loop and continue with the next iteration of the loop.
|
||||||
|
|
||||||
|
<%
|
||||||
|
int $val;
|
||||||
|
while (true) {
|
||||||
|
$val = rand(1, 30);
|
||||||
|
print("$val ");
|
||||||
|
if ($val == 22) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
%>
|
||||||
|
|
||||||
|
In above example, endless loop has been defined. There is only one way to jump out of a such loop—using the break statement. Above code assigns a random value from 1 to 30 and prints it. If the value equals to 22, script finishes the endless while loop.
|
||||||
|
|
||||||
|
<%
|
||||||
|
int $num = 0;
|
||||||
|
while ($num < 1000) {
|
||||||
|
$num++;
|
||||||
|
if (($num % 2) == 0)
|
||||||
|
continue;
|
||||||
|
print("$num ");
|
||||||
|
}
|
||||||
|
%>
|
||||||
|
|
||||||
|
Above example prints a list of numbers that cannot be divided by 2 without a remainder, during a while loop, which iterates through numbers from 1 to 999.
|
||||||
|
|
||||||
## 8. Functions
|
## 8. Functions
|
||||||
A function is a piece of code in a larger program. The function performs a specific task. The advantages of using functions are:
|
A function is a piece of code in a larger program. The function performs a specific task. The advantages of using functions are:
|
||||||
|
|||||||
Reference in New Issue
Block a user