tailieunhanh - Professional Information Technology-Programming Book part 64

Tham khảo tài liệu 'professional information technology-programming book part 64', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | Loops PHP offers three types of loop constructs that all do the same thingrepeat a section of code a number of timesin slightly different ways. The while Loop The while keyword takes a condition in parentheses and the code block that follows is repeated while that condition is true. If the condition is false initially the code block will not be repeated at all. Infinite Loops The repeating code must perform some action that affects the condition in such a way that the loop condition will eventually no longer be met otherwise the loop will repeat forever. The following example uses a while loop to display the square numbers from 1 to 10 count 1 while count 10 square count count echo count squared is square br count The counter variable count is initialized with a value of 1. The while loop calculates the square of that number and displays it then adds one to the value of count. The operator adds one to the value of the variable that precedes it. The loop repeats while the condition count 10 is true so the first 10 numbers and their squares are displayed in turn and then the loop ends. The do Loop The do loop is very similar to the while loop except that the condition comes after the block of repeating code. Because of this variation the loop code is always executed at least onceeven if the condition is initially false. The following do loop is equivalent to the previous example displaying the numbers from 1 to 10 with their squares count 1 do square count count echo count squared is square br count while count 10 The for Loop The for loop provides a compact way to create a loop. The following example performs the same loop as the previous two examples for count 1 count 10 count square count count echo count squared is square br As you can see using for allows you to use much less code to do the same thing as with while and do. A for statement has three parts separated by semicolons The first part is an expression that is evaluated once when the loop begins. In the .

crossorigin="anonymous">
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.