Default code break in this example var is the first

This preview shows page 11 - 14 out of 21 pages.

default : [code] break ; } In this example, $var is the first variable to be compared. This variable is then compared against each case statement from the top down, until it finds a match. At that point, the code will
execute until a break statement is reached (that will allow you to leave the case statement entirely). Important warning about using switch case statements Don't forget to use break when you mean break! If you forget, you might run functions you don't intend to. However, there are circumstances where leaving breaks out can be useful. Consider this example: switch ( $n ) { case 0: case 1 : case 2: //only executes, if $n is 0, 1 or 2 doSomethingForNumbers2OrSmaller(); break ; case 3: //only executes, if $n is 3 doSomethingForNumber3(); default : //only executes, if $n is 3 or above doSomethingForNumbers3OrBigger(); break ; } This kind of coding is sometimes frowned upon, since it's not always as clear to see what the code is meant to do. Also, consider commenting case statements that aren't supposed to have a break; statement before the next case, so when others look at your code, they know not to add a break. In such a case, it is good programming practice to add comments to breakless cases so that it is clear that the break has been omitted deliberately: switch ( $n ) { case 0: // Falls through! case 1: doSomethingForLargeNumbers(); // Falls through! case 2: doSomethingForSmallerNumbers() break ;
Loops Just imagine you needed a program to do something 20 times. What would you do? You could copy and paste the code 20 times, and have a virtually unreadable program. Or, you could tell the computer to repeat a bit of code between point A and point B, until the time comes that you need it to stop. This is known as a Loop, control structure that repeatedly executes a statement or a series of statements while a specific condition is TRUE or until a specific condition becomes TRUE. Loops are a important basic programming technique. Loops allow programs to execute the same lines of code repeatedly, this is important for many things in programs. In PHP you often use them to layout tables in HTML, process MySQL query results, search arrays for values and other similar tasks. We will cover four types of loop statements: while, do . . . while, for, and foreach statements. while Statements The while statement construct consists of a condition expression and block of code. The condition expression is evaluated, and if the condition expression is TRUE the code within the block is executed. This repeats until the condition expression becomes FALSE. Each repetition of a looping statement is called an iteration. Because the while loop checks the condition expression before the block is executed, the control structure is often also known as a pre‐test loop. The syntax for the while statement is as follows: while (conditional expression) { statement(s); } Since a while statement will repeat until the condition expression is evaluated to false, a mechanism must be put into the while statement that ensure there is an end to the looping. A section of code in the while statement that tracks the loops progress and changes value(s) in the conditional expression,

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture