PHP 5/MySQL Programming- P26

PHP 5/MySQL Programming- P26:computer programming has often been seen as a difficult and arcane skill. Programming languages are difficult and complicated, out of the typical person’s reach. However, the advent of the World Wide Web has changed that to some extent. It’s reasonably easy to build and post a Web page for the entire world to see. The language of the Web is reasonably simple, and numerous applications are available to assist in the preparation of static pages | h1 Counting Backwards h1 for i 10 i 0 i- print i br n end for loop body html If you understand how for loops work the changes all make sense. I m counting backwards this time so i begins with a large value in this case 10 . The condition for continuing the loop is now i 0 which means the loop continues as long as i is greater than 0. The loop ends as soon as i is 0 or less. Note that rather than adding a value to i this time I decrement by 1 each time through the loop. If you re counting backwards be very careful that the sentry variable has a mechanism for getting smaller. Otherwise the loop never ends. Recall that i adds 1 to i i- subtracts 1 from i. Chapter 4 Loops and Arrays Using a hile Loop PHP like most languages provides another kind of looping structure even more flexible than the for loop. You can use the while loop when you know how many times something will happen. Figure shows how a while loop can work much like a for loop. Repeating Code with a while Loop The code for the program is much like the for loop example but you can see that the while loop is a little bit simpler html head title A simple While Loop title 104 PHP 5 MySQL Programming for the Absolute Beginner head body h1 A simple while loop h1 i 1 while i 10 print i br n i end while body html The while loop requires only one parameter which is a condition. The loop continues as long as the condition is evaluated as TRUE. As soon as the condition is evaluated as FALSE the loop exits. FIGURE Although this program s output looks a lot like the basic for loop it uses a different construct to achieve the same result. This particular program starts by initializing the variable i then checking to see if it s greater than or equal to 10 in the while statement. Inside the loop body the program prints the current value of i and increments i. Recognizing Endless Loops The flexibility of the while construct gives it power but with that power comes potential for problems. while loops are

Không thể tạo bản xem trước, hãy bấm tải xuống
469    13    1    31-03-2023
Đã 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.