Presentation is loading. Please wait.

Presentation is loading. Please wait.

Control Structures: for & while Loops

Similar presentations


Presentation on theme: "Control Structures: for & while Loops"— Presentation transcript:

1 Control Structures: for & while Loops
Web Programming

2 Review Conditional Expression is a key component of Control Structures
The “value” of the conditional expression determines the program flow Syntax: KEYWORD (expression) { statements } e.g. if ($count<5) { print “count is $count\n”; } Evaluation of Conditional Expression True if the value of the expression is non-zero. False if the value of the expression is zero (or null). Comparison Operators (CO)  Compares two things Numeric CO: ==, !=, >, >=, <, <= String CO: eq, ne, gt, ge, lt, le Logical Operators  Tests for multiple conditions AND (&&), OR (II), NOT (!) if Conditional Syntax if (condexpr1) { statements1 } elsif (condexpr2) { statements2 } else { statements3 } Web Programming

3 while Loop Syntax initialization Typically involves
while (exit condition) { statements } Typically involves variable initialization before the loop exit condition variable manipulation inside the loop to trigger loop exit $a=“”; while ($a ne “x”) { $a=<STDIN>; chomp $a; print “$a\n”; }  Example script Web Programming

4 for Loop Iterative Loops are another type of control structures
statements are executed fixed number of times Iterative Loops in Perl for (initialization; test condition; increment) { statements } e.g. for ($i=1; $i<=5; $i++) { print “$i\n”; }  Example script foreach localvar(list expression) { statements } list expression is an array variable or list localvar is a scalar variable defined only for the duration of foreach loop localvar holds list elements in sequential order iterates through list elements e.g. foreach $i (1, 2, 3, 4, 5) { print “$i\n”; } Web Programming

5 Loop Control Ways to control the program flow of Loop block (i.e. Loop Control) typically used with if or unless last terminates the loop  Example script next skips to the next loop iteration Web Programming


Download ppt "Control Structures: for & while Loops"

Similar presentations


Ads by Google