Presentation is loading. Please wait.

Presentation is loading. Please wait.

ITC 240: Web Application Programming

Similar presentations


Presentation on theme: "ITC 240: Web Application Programming"— Presentation transcript:

1 ITC 240: Web Application Programming
Subhash Prajapati 04/23/15

2 Review Array - variable type that can hold many values at a time
Indexed Array - arrays with numeric index Associative Array - arrays with named keys Multidimentional Array - arrays within the array

3 Today Array Exercise Loops

4 Review Exercise: 6.1 Create students array that would include all the students name in a class [Numeric Indexed Array] Create a states array with state abbreviations as keys [Associative Array] Create students array with marks on different subjects [Multidimensional Array]

5 unset()/isset() These are two PHP built-in functions to do following: isset() - check whether a variable is set or not (od ) unset() – destroys the variable We can destroy/delete the array element like this: unset ($myArray[2])

6 Loop We can alter the flow of control using either conditionals or loops Loops: execute a statement(s) over and over. Usually contains: - a start condition - a break condition - change in each iteration

7 while loop loops through a block of code as long as the specified condition is true

8 while loop Syntax: while (condition) { // code block to be executed }

9 while loop example Following loop will iterate 4 times $counter = 1; while ($counter<5) { echo “The counter is: ” . $counter.”<br>”; $counter++; }

10 Quiz What happens to following code $counter = 0; while ($counter < 1) { echo “The counter is: ” . $counter.”<br>”; $counter--; }

11 Class Exercise 6.2 Write a program with while loop, which would print following table of squares (styling not required) Number Square Value 1 2 4 3 9 16 5 25 6 36 7 49 8 64 81 10 100

12 while loop alternative syntax
while (expression): // statements goes here endwhile; eg: $i = 0; while ($i<10): echo $i; $i++;

13 do .. while loop loops through a block of code once, and then repeats the loop for specified condition is true

14 do .. while loop Syntax: do { // code block to be executed } while (condition);

15 do .. while loop do { do_work(); } while (condition); is equivalent to while (condition) { }

16 while vs do .. while loop The difference between do-while and while is that do-while evaluates its expression at the bottom of the loop instead of the top. Therefore, the statements within the do block are always executed at least once.

17 while example scenario
Consider a program which writes some text in a file until file size becomes 5MB. We can use while loop in this scenario like this. while(IsFileSizeLessThan5MB()) { WriteMoreData (); }

18 do .. while example scenario
Let's consider another scenario where we want to take an integer input from user until user has entered a positive number. In this case we will use a do-while loop like this. do { TakeInputFromUser(); } while ($input < 0);

19 Class Exercise 5.3 Cumulative Sum: write a method (function) with a while loop that computes the sum of first n positive integer. for eg: if you pass 5, it will give 15 (sum of ) hints: Declare a function which will take one argument, the function will return the sum Inside the function, write a while loop which will iterate n number of time (n= number passed in that function) Inside the while loop, do the addition of the numbers


Download ppt "ITC 240: Web Application Programming"

Similar presentations


Ads by Google