update: added additional note fields (#29551)

* fix: corrected placement of notes

Some "note" fields were preemptive in their placement; I corrected their placement to be in more appropriate locations.

* update: added additional note fields

Added some additional "note" fields to assist those who are reading through the PHP documentation.

* fix: formatting of escape sequence examples

The formatting was a bit jumbled, so I cleaned up the formatting of the escape sequence.

* update: improved examples

I added a new example to demonstrate the use of a "break" statement in a while loop, as well as numbered each example.

* update: add clarifying statement

I added a note at the end of the description, with the goal of being a bit more explicit in terms of what the "continue" statement does.
This commit is contained in:
Corey Abma
2018-12-13 03:51:39 -05:00
committed by Randell Dawson
parent e545aceab8
commit 723fa03609
4 changed files with 53 additions and 13 deletions

View File

@ -8,7 +8,7 @@ The `break` statement terminates the enclosing loop or switch statement in which
In the first example, when the value of i is 3, the break statement is executed, which causes the execution of the loop to be terminated. In the first example, when the value of i is 3, the break statement is executed, which causes the execution of the loop to be terminated.
## Example ## Example 1
``` ```
int[] array = { 1, 2, 3, 4, 5 }; int[] array = { 1, 2, 3, 4, 5 };
for (int i = 0; i < array.Length; i++) for (int i = 0; i < array.Length; i++)
@ -26,7 +26,7 @@ for (int i = 0; i < array.Length; i++)
``` ```
In the second example, a break statement is included at the end of each case. This executes the statements in the case without continuing to the next case. Without the break statement, the program would continue to execute the next case and would not function as intended. In the second example, a break statement is included at the end of each case. This executes the statements in the case without continuing to the next case. Without the break statement, the program would continue to execute the next case and would not function as intended.
## Example ## Example 2
``` ```
switch (exampleVariable) switch (exampleVariable)
{ {
@ -68,3 +68,37 @@ switch (exampleVariable)
> default > default
> This only shows in the Default Example > This only shows in the Default Example
``` ```
In the third example, we make use of an infinite `while` loop, which prompts the user for the best song of all time. When the user finally enters "Ocean Man", a `break` statement is used, and the infinite `while` loop is exited.
## Example 3
```
while (true)
{
Console.WriteLine("What is the greatest song of all time? Be honest--I can wait all day!");
string response = Console.ReadLine();
if (response == "Ocean Man")
{
Console.WriteLine("Good, I agree!");
break;
}
else Console.WriteLine("That's not the greatest song! Try again!");
}
```
## Output (if the user enters "Ocean Man" right away):
```
> What is the greatest song of all time? Be honest--I can wait all day!
Ocean Man
> Good, I agree!
```
## Output (if the user enters "Darude Sandstorm" the first time, then "Ocean Man"):
```
> What is the greatest song of all time? Be honest--I can wait all day!
Darude Sandstorm
> That's not the greatest song! Try again!
> What is the greatest song of all time? Be honest--I can wait all day!
Ocean Man
> Good, I agree!
```

View File

@ -8,6 +8,7 @@ The `continue` statement passes control to the next iteration inside a loop.
In this example, when the value of i is 2, the next statement within the loop is skipped. In this example, when the value of i is 2, the next statement within the loop is skipped.
Note: When you execute a `continue` statement, you're effectively bypassing whatever code comes after it (in terms of execution) within the loop. This, along with the `break` statement, are very useful when working with loops.
## Example ## Example
``` ```
int[] array = { 1, 2, 3, 4, 5 }; int[] array = { 1, 2, 3, 4, 5 };

View File

@ -16,7 +16,7 @@ If/Else is a conditional statement where depending on the truthiness of a condit
statement2; statement2;
} }
``` ```
> **Note:** The `else` statement is optional. > **Note:** You can nest as many statements in an "if" block as you'd like; you are not limited to the amount in the examples.
## If/Else Statement ## If/Else Statement
``` ```
@ -30,7 +30,7 @@ If/Else is a conditional statement where depending on the truthiness of a condit
statement4; statement4;
} }
``` ```
> **Note:** `elseif` should always be written as one word. > **Note:** The `else` statement is optional.
## If/Elseif/Else Statement ## If/Elseif/Else Statement
``` ```
@ -46,6 +46,7 @@ If/Else is a conditional statement where depending on the truthiness of a condit
statement5; statement5;
} }
``` ```
> **Note:** `elseif` should always be written as one word.
## Nested If/Else Statement ## Nested If/Else Statement
``` ```
@ -87,6 +88,7 @@ For instance:
echo 'One condition is true, and one condition is false!'; echo 'One condition is true, and one condition is false!';
} }
``` ```
> **Note:** It's a good practice to wrap individual conditions in parens when you have more than one (it can improve readability).
## Ternary Operators ## Ternary Operators

View File

@ -25,11 +25,14 @@ A _raw_ string can be used by prefixing the string with `r` or `R` which allows
print(r"An odd number of backslashes at the end of a raw string will cause an error\") print(r"An odd number of backslashes at the end of a raw string will cause an error\")
^ ^
SyntaxError: EOL while scanning string literal. SyntaxError: EOL while scanning string literal.
#Some more examples of escape sequences.
Escape Sequence ## Some more examples of escape sequences.
\\ Prints Backslash
\` Prints single-quote Escape Sequence <- Intended Character
\" Prints double quote - \\\ <- backslash
\a ASCII bell makes ringing the bell alert sounds ( eg. xterm ) - \\' <- single quote / apostrophe
\b ASCII backspace ( BS ) removes previous character - \\" <- double quote / quotation mark
\n Adds newline. - \\a <- ASCII bell makes ringing the bell alert sounds ( eg. xterm )
- \\b <- ASCII backspace ( BS ) removes previous character
- \\n <- newline
- \\r <- carriage return