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

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

Đ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

Flow Control in PHP Session Review      Conditional statements execute a set of statements only when a specified condition is satisfied if statement executes a block of code only when the specified condition is true switch statement executes a block of code based on the value it matches switch statement checks single variable against multiple values Ternary operator simplifies complex conditions into one line statements PHP / Session / Slide of 29 Objectives  Use while loop  Use do-while loop  Use for loop  Use jump statements PHP / Session / Slide of 29 Loops     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:  while loop  do-while loop  for loop PHP / Session / Slide of 29 While Loop - I   Executes the loop executing statements depending on the return result of the testing condition Syntax for while loop is: while(condition) { Code executes if the condition is true; } Code executes if the condition is false; PHP / Session / Slide of 29 While Loop - II    The while keyword is followed by the condition in parentheses The condition is the test expression 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 The value of the executing statements depends on the condition, therefore it checks the condition after each iteration of the loop PHP / Session / Slide of 29 Example for While Loop - I  For example, to display first 10 odd numbers PHP / Session / Slide of 29 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 PHP / Session / Slide of 29 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 Syntax for a do-while loop is: { Code executes if the condition is true; } while(condition); Code executes if the condition is false; PHP / Session / Slide of 29 Do-While Loop - II    In do-while loop, the loop body follows the 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 PHP / Session / Slide 10 of 29 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 PHP / Session / Slide 15 of 29 Example for the for loop - I  For example, to display the output multiplication of a number by of the PHP / Session / Slide 16 of 29 Example for the for loop - I    Here, the multiplication value of with is displayed The loop keeps executing until the counter value reaches to Once the counter value reaches to 6, the loop stops executing The counter keeps incrementing from to PHP / Session / Slide 17 of 29 Jump Statements   Controls the execution of the loop statements Different jump statements are:    break statement continue statement exit statement PHP / Session / Slide 18 of 29 Break Statement    Stops the execution of the loop Passes the control either to the beginning of the next loop or to the statement following the loop Uses with the if statement, switch statement, for loop, while loop, and do-while loop PHP / Session / Slide 19 of 29 Example for break statement - I  For example, to display the consecutive numbers from to 10 by using for loop PHP / Session / Slide 20 of 29 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 PHP / Session / Slide 21 of 29 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 PHP / Session / Slide 22 of 29 Example for continue statement I  For example, to display numbers from to 10 with while loop PHP / Session / Slide 23 of 29 Example for continue statement II    Here, the continue statement is used for checking the condition in the if statement The counter initializes to The loop continues till the counter value becomes As the counter value becomes 5, the loop skips executing the value and starts executing from the next counter value The loop continues till the condition becomes false PHP / Session / Slide 24 of 29 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 PHP / Session / Slide 25 of 29 Example for exit statement - I PHP / Session / Slide 26 of 29 Example for exit statement - II    Here, the HRA value is calculated on the basis of the basic salary If the basic salary is less than 6000, then the if statement exits If the basic salary is greater than or equal to 6000, then the HRA value is calculated PHP / Session / Slide 27 of 29 Summary - I     A loop executes depending on the true or false values returned by testing the testing condition A while loop executes loop statements depending on the return result of the testing condition after testing it The testing condition in do-while loop is 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 A for loop executes a set of codes repetitively for a specified amount of time PHP / Session / Slide 28 of 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 PHP / Session / Slide 29 of 29 [...]... 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 PHP / Session 9 / Slide 21 of 29 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 PHP / Session 9 / Slide. .. calculated PHP / Session 9 / Slide 27 of 29 Summary - I     A loop executes depending on the true or false values returned by testing the testing condition A while loop executes loop statements depending on the return result of the testing condition after testing it The testing condition in do-while loop is placed after the loop ends In the first iteration of the loop, the statements placed inside... the control either to the beginning of the next loop or to the statement following the loop Uses with the if statement, switch statement, for loop, while loop, and do-while loop PHP / Session 9 / Slide 19 of 29 Example for break statement - I  For example, to display the consecutive numbers from 1 to 10 by using for loop < ?php for($i=1;;$i++) { if($i>10) { break; } echo $i; } ?> PHP / Session 9 / Slide. .. loop is: for(initialization;condition;reinitialization) { Code executes if the condition is true; } Code executes if the condition is false; PHP / Session 9 / Slide 14 of 29 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... displayed The loop keeps executing until the counter value reaches to 6 Once the counter value reaches to 6, the loop stops executing The counter keeps incrementing from 0 to 6 PHP / Session 9 / Slide 17 of 29 Jump Statements   Controls the execution of the loop statements Different jump statements are:    break statement continue statement exit statement PHP / Session 9 / Slide 18 of 29 Break Statement... for continue statement I  For example, to display numbers from 1 to 10 with while loop < ?php $counter = 0; while($counter PHP / Session 9 / Slide 23 of 29 Example for continue statement II    Here, the continue statement is used for checking the condition in the if... displaying until the counter reaches to the value 10 The loop stops once the condition gets satisfied PHP / Session 9 / Slide 12 of 29 For Loop    Enables us to execute a block of code repetitively for a fixed number of times Executes the loop executing statements till the testing condition gets satisfied Stops executing once the condition gets satisfied PHP / Session 9 / Slide 13 of 29 Using For... 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 PHP / Session 9 / Slide 24 of 29 Exit Statement   Ends the loop and passes the control to the statement following the body of the loop... true value The re-initialization statement helps in the iteration of the loop PHP / Session 9 / Slide 15 of 29 Example for the for loop - I  For example, to display the output multiplication of a number by 2 of the < ?php $number=6; for($counter=1;$counter PHP / Session 9 / Slide 16 of 29 Example... statement following the body of the loop For example, to display the HRA value using if statement with the exit statement PHP / Session 9 / Slide 25 of 29 Example for exit statement - I < ?php $salary=6000; if($salary PHP / Session 9 / Slide 26 of 29 Example for exit statement - II    Here, the HRA value is

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

Mục lục

  • Flow Control in PHP

  • Review

  • Objectives

  • Loops

  • While Loop - I

  • While Loop - II

  • Example for While Loop - I

  • Example for While Loop - II

  • Do-While Loop - I

  • Do-While Loop - II

  • Example for Do-While Loop - I

  • Example for Do-While Loop - II

  • For Loop

  • Using For Loop - I

  • Using For Loop - II

  • Example for the for loop - I

  • Slide 17

  • Jump Statements

  • Break Statement

  • Example for break statement - I

Tài liệu cùng người dùng

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

Tài liệu liên quan