Control Structures - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

Control Structures

Description:

foreach VAR (LIST) {} each member of LIST is assigned to VAR, and the loop executed ... within loop to VAR affect LIST. if VAR omitted, $_ used _at_array = (1, ... – PowerPoint PPT presentation

Number of Views:27
Avg rating:3.0/5.0
Slides: 15
Provided by: PaulL155
Category:

less

Transcript and Presenter's Notes

Title: Control Structures


1
Control Structures
2
if-elsif-else
  • semantically the same as C/C
  • syntactically, slightly different.
  • if (a gt 0)
  • print \a is positive\n
  • elsif (a 0)
  • print \a equals 0\n
  • else
  • print \a is negative\n
  • brackets are required!

3
unless
  • another way of writing if (!)
  • analogous to English meaning of unless
  • unless (EXPR) BLOCK
  • do BLOCK unless EXPR is true
  • do BLOCK if EXPR is false
  • can use elsif and else with unless as well

4
while/until loops
  • while is similar to C/C
  • while (EXPR) BLOCK
  • While EXPR is true, do BLOCK
  • until (EXPR) BLOCK
  • Until EXPR is true, do BLOCK
  • While EXPR is false, do BLOCK
  • another way of saying while (!)
  • again, brackets are required

5
do
  • Execute all statements in following block, and
    return value of last statement executed
  • When modified by while or until, run through
    block once before checking condition
  • do
  • i
  • while (i lt 10)
  • Note that Perl does not consider do to be an
    actual loop structure. This is important later
    on

6
for loops
  • Perl has 2 styles of for.
  • First kind is virtually identical to C/C
  • for (INIT TEST INCRMENT)
  • for (i 0 i lt 10 i) print \i i\n
  • yes, the brackets are required.

7
foreach loops
  • Second kind of for loop in Perl
  • no equivalent in core C/C language
  • foreach VAR (LIST)
  • each member of LIST is assigned to VAR, and the
    loop executed
  • sum 0
  • foreach value (_at_nums)
  • sum value

8
More About for/foreach
  • for and foreach are actually synonyms
  • Anywhere you see for you can replace it with
    foreach and viceversa
  • Without changing ANYTHING ELSE
  • they can be used interchangeably.
  • usually easier to read if conventions followed
  • for (i 0 ilt10 i)
  • foreach i (_at_array)
  • but this is just as syntactically valid
  • foreach (i 0 ilt10 i)
  • for i (_at_array)

9
Two More Things (about for)
  • foreach VAR (LIST)
  • while iterating through list, VAR becomes an
    alias to each member of LIST
  • Changes within loop to VAR affect LIST
  • if VAR omitted, _ used
  • _at_array (1, 2, 3, 4, 5)
  • foreach (_at_array)
  • _ 2
  • _at_array now ? (2, 4, 6, 8, 10)

10
Reading it in English
  • Perl has a cute little feature that makes simple
    loop constructs more readable
  • If your if, unless, while, until, or foreach
    block contains only a single statement, you can
    put the condition at the end of the statement
  • if (a gt 10) print \a is a\n
  • print \a is a\n if a gt 10
  • Using this modifier method, brackets and
    parentheses are unneeded
  • This is syntactic sugar whichever looks and
    feels right to you is the way to go.

11
Loop Control next, last, redo
  • last ? equivalent of C break
  • exit innermost loop
  • next ? (mostly) equivalent of C continue
  • begin next iteration of innermost loop
  • redo ? no real equivalent in C
  • restart the current loop, without evaluating
    conditional
  • Recall that do is not a looping block. Hence,
    you cannot use these keywords in a do block (even
    if its modified by while)

12
continue block
  • while, until, and foreach loops can have a
    continue block.
  • placed after end of loop, executed at end of each
    iteration
  • executed even if the loop is broken out of via
    next (but not if broken via last or redo)
  • foreach i (_at_array)
  • next if (i 2 ! 0)
  • sum i
  • continue
  • nums

13
Breaking Out of More Loops
  • next, last, redo operate on innermost loop
  • Labels are needed to break out of nesting loops
  • TOP while (i lt 10)
  • MIDDLE while (j gt 20)
  • BOTTOM foreach (_at_array)
  • if (j 2 ! 0)
  • next MIDDLE
  • if (i 3 lt 10)
  • last TOP

14
goto
  • yes, it exists.
  • No, dont use it.
Write a Comment
User Comments (0)
About PowerShow.com