1. Trang chủ
  2. » Mẫu Slide

Slide môn học PHP session 2d flow control in PHP

29 170 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 29
Dung lượng 96,5 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

 Executes a block of statements repetitively  Tests and executes loop statements repetitively if the condition returns true value  Stops executing loop statements if the condition

Trang 1

Flow Control in PHP

Session 9

Trang 2

Conditional statements execute a set of

statements only when a specified condition

switch statement checks single variable

against multiple values

Ternary operator simplifies complex

Trang 3

Use do-while loop

Use for loop

Trang 4

Executes a block of statements repetitively

Tests and executes loop statements repetitively

if the condition returns true value

Stops executing loop statements if the condition returns false value

Types of loop statements are:

Trang 5

While Loop - I

Executes the loop executing statements

depending on the return result of the testing

Trang 6

While Loop - II

condition in parentheses

consisting of variables and operators If the condition is satisfied, the code in the loop body is executed If the condition is false, the loop stops and the control move

to the statement following the loop

depends on the condition, therefore it checks the condition after each iteration of the loop

Trang 7

Example for While Loop - I

Trang 8

Example for While Loop - II

Here, the first 10 odd numbers are displayed using the while loop

The value keeps displaying until the counter reaches to the value 10

The loop stops once the condition gets satisfied

Trang 9

Do-While Loop - I

Works similarly like the while loop

Difference between the while and do-while

loop is that in the do-while loop, the

condition is placed at the end of the loop

Trang 10

Do-While Loop - II

In do-while loop, the loop body follows the

do keyword

The loop body is executed at least once The

loop body is followed by the while keyword

and the condition is in the parenthesis

If the condition returns true value, the

loop keeps executing If the condition

returns false value, the loop ends and the

statements following the while keyword are

executed

Trang 11

Example for Do-While Loop - I

For example, to print the first 10 even

Trang 12

Example for Do-While Loop - II

using the do-while loop

the value is displayed once Then the

execution starts

counter reaches to the value 10 The

loop stops once the condition gets

satisfied

Trang 13

For Loop

code repetitively for a fixed

number of times

statements till the testing

condition gets satisfied

Trang 14

Using For Loop - I

Trang 15

Using For Loop - II

The condition specifies the testing

condition

The initialization initializes the value

for the counter

The re-initialization specifies the

increment and decrement of the counter

The codes inside the loop gets executed

only when the condition returns true value

The re-initialization statement helps in

the iteration of the loop

Trang 16

Example for the for loop - I

For example, to display the output of the multiplication of a number by 2

Trang 17

Example for the for loop - I

with 2 is displayed

counter value reaches to 6

the loop stops executing The counter keeps incrementing from 0 to 6

Trang 19

Break Statement

beginning of the next loop or to

the statement following the loop

switch statement, for loop, while

loop, and do-while loop

Trang 20

Example for break statement - I

For example, to display the consecutive numbers

from 1 to 10 by using for loop

Trang 21

Example for break statement - II

Here, the break statement is used

with the for loop

The condition is specified in the

if statement

The break statement controls the

loop If the break statement is not

used, then the loop will go in

continuous state

Trang 22

Continue Statement

Uses with the loop statements

Stops executing the current loop

and continues with the next

execution of the program

Uses with the if statement, for

loop, while loop, and do-while loop

Trang 23

Example for continue statement - I

For example, to display numbers from 1 to 10

with while loop

Trang 24

Example for continue statement - II

Here, the continue statement is used for

checking the condition in the if statement

The counter initializes to 0 The loop

continues till the counter value becomes

5 As the counter value becomes 5, the

loop skips executing the value 5 and

starts executing from the next counter

value

The loop continues till the condition

becomes false

Trang 25

Exit Statement

Ends the loop and passes the

control to the statement

following the body of the loop

For example, to display the HRA

value using if statement with

the exit statement

Trang 26

Example for exit statement - I

Trang 27

Example for exit statement - II

on the basis of the basic salary

6000, then the if statement exits

than or equal to 6000, then the

HRA value is calculated

Trang 28

Summary - I

false values returned by testing the testing condition

depending on the return result of the

testing condition after testing it

placed after the loop ends In the first

iteration of the loop, the statements placed inside the loop get executed without any

consideration of the testing result

Trang 29

Summary - II

The break statement stops the iteration of

the loop from the current loop execution

This statement is used with for loop, while

loop, and do…while loop statements

The continue statement is used for breaking

the iteration of the loop from the current

loop execution

The exit statement is used to break the loop

while the loop is in the execution process

After using the exit statement, no further

execution of the loop is possible

Ngày đăng: 30/11/2016, 22:11

TÀI LIỆU CÙNG NGƯỜI DÙNG

  • Đang cập nhật ...

TÀI LIỆU LIÊN QUAN