Presentation is loading. Please wait.

Presentation is loading. Please wait.

IST 210: PHP Logic IST 210: Organization of Data IST2101.

Similar presentations


Presentation on theme: "IST 210: PHP Logic IST 210: Organization of Data IST2101."— Presentation transcript:

1 IST 210: PHP Logic IST 210: Organization of Data IST2101

2 PHP Basics Logical Structures –Sequence –Selection –Loop 2IST210

3 Statements Statement: an instruction written in a high-level language –Assignment statement $filename=“abc.txt”; $x = 5+3; $y = (5 == 3); –Input/output statement echo “test”; 3IST210

4 Logic Structures Three basic logic structures –Sequence –Selection –Loop 4IST210

5 Sequence The program, when run, must perform each action in order with no possibility of skipping an action or branching off to another action. Sequence: Statement 1 Statement 2 Statement 3 …… 5IST210

6 Sequence To display headings 6IST210 <?php echo " Heading 1 "; echo " Heading 2 "; echo " Heading 3 "; echo " Heading 4 "; echo " Heading 5 "; echo " Heading 6 "; ?>

7 Selection/Decision In a selection structure, a question is asked, and depending on the answer, the program takes one of two courses of action, after which the program moves on to the next event. Selection if expression then statement 1 else statement 2 7IST210

8 IF/ELSE if – else if (Expression) Statement if (Expression) Statement 1 else Statement 2 IST2108 <?php $x = 20; if ($x >100) echo "x is a large number."; else echo "x is a small number."; ?> Try it

9 IF/ELSE More than one statement: Use braces {} IST2109 <?php $x = 200; if ($x >100) { echo "x is a large number. "; echo "x is greater than 100"; } else { echo "x is a small number. "; echo "x is smaller than 100"; } ?> Try it

10 IF/ELSE: Exercise IST21010 <?php $x = 200; $y = 100; if ($x = $y) { echo "x equals to y"; } else { echo "x does not equal to y"; } ?> Debug $x does not equal to y. Where is the bug?

11 A Very Common Mistake A==B: boolean value, true (1) or false (0) A=B: assign variable B’s value to variable A. (The value of the assignment operation is 1.) if (x = 1)  This is always true! then …

12 Loop A structure to repeat an action multiple times under a given condition. Loops constitute one of the most basic and powerful programming concepts. 12IST210

13 Loop Three approaches to do loops 1.while (Expression) Statement; 2.for (Expression1; Expression2; Expression3) Statement; IST21013 <?php $i=1; while ($i<=6) { echo " Heading $i "; $i++; } ?> <? php for ($i=1; $i<=6; $i++) echo " Heading $i "; ?> Try it

14 For Loop for (Expression1; Expression2; Expression3) Statement; Expression1: Initialization expression –executed exactly once -- before the first evaluation of the test expression Expression2: Test expression –evaluated each time before the code in the for loop executes Expression3: Increment expression –executed after each iteration of the loop, often used to increment the loop variable, which is initialized in the initialization expression and tested in the test expression. for ($i=1; $i<=6; $i++) echo " Heading $i "; IST21014

15 Loop A very common mistake in Loop: infinite loop –Case 1: –Case 2: IST21015 for ($i=1; $i<=6; $i++) { echo " Heading $i "; $i = 1; } $i=1; while ($i<=6) { echo " Heading $i "; }

16 Combinations of Logic Structure A PHP file usually combines different control structures. 16IST210


Download ppt "IST 210: PHP Logic IST 210: Organization of Data IST2101."

Similar presentations


Ads by Google