More Perl Control Flow Software Tools. Slide 2 Control Flow l We have already seen several Perl control flow statements: n if n while n for n last l Other.

Slides:



Advertisements
Similar presentations
Statement-Level Control Structures
Advertisements

1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
PHP Functions and Control Structures. 2 Defining Functions Functions are groups of statements that you can execute as a single unit Function definitions.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
Perl I/O Learning Objectives: 1. To understand how to perform input from standard Input & how to process the input 2. To understand how to perform input.
Computer Science 1620 Loops.
Shell Programming Software Tools. Slide 2 Shells l A shell can be used in one of two ways: n A command interpreter, used interactively n A programming.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
Perl Functions Software Tools. Slide 2 Defining a Function l A user-defined function or subroutine is defined in Perl as follows: sub subname{ statement1;
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
Perl I/O Software Tools. Lecture 15 / Slide 2 Input from STDIN Reading from STDIN is easy, and we have done it many times. $a = ; In a scalar context,
Perl Basics Software Tools. Slide 2 Control Flow l Perl has several control flow statements: n if n while n for n unless n until n do while n do until.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Perl Basics Learning Objectives: 1. To understand the commands available for control flow in Perl 2. To learn some useful operators in Perl.
Objectives You should be able to describe:
More Perl Control Flow Learning Objectives: 1. To learn more commands for control flow 2. To apply the new commands learnt for writing tidier program 3.
Shell Programming Learning Objectives: 1. To understand the some basic utilities of UNIX File 2. To compare UNIX shell and popular shell 3. To learn the.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
More Perl Control Flow Learning Objectives: 1. To learn more commands for control flow 2. To apply the new commands learnt for writing tidier program 3.
5.05 Apply Looping Structures
Loops and Iteration Chapter 5 Python for Informatics: Exploring Information
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
DiagrammaticRepresentation Iteration Construct False True Condition Exit from Statement (s) loop Sequence construct Selection construct Statement 1 Statement.
CPS120 Introduction to Computer Science Iteration (Looping)
PL/SQL Loops. Building Logical Conditions All logical conditions must yield a boolean condition. You can build a simple Boolean condition by combining.
Control Structures. Important Semantic Difference In all of these loops we are going to discuss, the braces are ALWAYS REQUIRED. Even if your loop/block.
Meet Perl, Part 2 Flow of Control and I/O. Perl Statements Lots of different ways to write similar statements –Can make your code look more like natural.
CIS 234: LOOPS Adapted from materials by Dr. Donald Bell, 2000 (updated April 2007)
Control Structures. if-elsif-else semantically the same as C/C++ syntactically, slightly different. if ($a > 0){ print “\$a is positive\n”; } elsif ($a.
PHP Conditional Statements Conditional statements in PHP are used to perform different actions based on different conditions. Conditional Statements Very.
PHP Programming with MySQL Slide 4-1 CHAPTER 4 Functions and Control Structures.
CPS120: Introduction to Computer Science Decision Making in Programs.
1 Loops. 2 Topics The while Loop Program Versatility Sentinel Values and Priming Reads Checking User Input Using a while Loop Counter-Controlled (Definite)
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Chapter 2 Functions and Control Structures PHP Programming with MySQL 2 nd Edition.
Chapter 8 Chapter 8 Control Structures. Control Structures  A control structure is a control statement and the statements whose execution it controls.
Control Structures By Shyam Gurram. Control Structure In this chapter we have two different types of structures. Conditional Structure Iterative Control.
Controlling Execution Dong Shao, Nanjing Unviersity.
Chapter 8 Iteration Dept of Computer Engineering Khon Kaen University.
Week 3 - Wednesday.  What did we talk about last time?  Other C features  sizeof, const  ASCII table  printf() format strings  Bitwise operations.
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormick 3rd floor 607 Office Hours – Tuesday and.
Shell Programming Learning Objectives: 1. To understand the some basic utilities of UNIX File 2. To compare UNIX shell and popular shell 3. To learn the.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 10 - JavaScript/JScript: Control Structures II Outline 10.1Introduction 10.2Essentials of.
CPTG286K Programming - Perl Chapter 4: Control Structures.
CPS120 Introduction to Computer Science Iteration (Looping)
Topic 2: Working with scalars CSE2395/CSE3395 Perl Programming Learning Perl 3rd edition chapter 2, pages 19-38, Programming Perl 3rd edition chapter.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
W E E K F I V E Control Flow. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Chapter 8 Topics Introduction Selection Statements Iterative Statements.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
JavaScript and Ajax (Control Structures) Week 4 Web site:
Perl Chapter 3 Conditional statements. Control Expressions Control expressions – interpreted as T/F (evaluated as strings or numbers) – simple, relational,
Week 3 - Friday.  What did we talk about last time?  Preprocessor directives  Other C features  sizeof, const  ASCII table  printf() format strings.
Loops Tonga Institute of Higher Education. Introduction Programs need to be able to execute tasks repeatedly. Use loops to repeat actions  For Loop 
CSC 4630 Perl 3 adapted from R. E. Beck. Problem But we worked on it first: Input: Read from a text file named in a command line argument Output: List.
CPTG286K Programming - Perl Chapter 9: Misc. Control Structures Chapter 10: Filehandles & File tests.
BLOCK  is enclosed by curly brackets {}.  Keep in mind that blocks are always bounded by curly brackets, even if your block has only one code line.
REPETITION CONTROL STRUCTURE
Miscellaneous Items Loop control, block labels, unless/until, backwards syntax for “if” statements, split, join, substring, length, logical operators,
Review If you want to display a floating-point number in a particular format use The DecimalFormat Class printf A loop is… a control structure that causes.
JavaScript: Control Statements.
Control Structures: for & while Loops
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Statement-Level Control Structures
More Loops Topics Counter-Controlled (Definite) Repetition
Perl Control Flow Learning Objectives:
More Loops Topics Counter-Controlled (Definite) Repetition
CSC215 Lecture Control Flow.
More Loops Topics Counter-Controlled (Definite) Repetition
Presentation transcript:

More Perl Control Flow Software Tools

Slide 2 Control Flow l We have already seen several Perl control flow statements: n if n while n for n last l Other control flow statements: n unless n until n do while n do until n foreach n next n redo

Slide 3 unless The Perl unless statement is like an if statement with the condition negated: if($temperature <= 20){ print "too cold!\n"; } unless($temperature > 20){# same thing print "too cold!\n"; } if(!$hot){ print "too cold!\n"; } unless($hot){# same thing print "too cold!\n"; }

Slide 4 unless unless can have else, just like if : #!/usr/local/bin/perl5 -w print "Enter temperature: "; chomp($temperature = ); unless($temperature > 20){ print "too cold!\n"; }else{ print "too hot!\n"; }

Slide 5 unless unless can also have elsif, just like if : #!/usr/local/bin/perl5 -w print "Enter temperature: "; chomp($temperature = ); unless($temperature >= 20){ print "too cold!\n"; }elsif($temperature == 20){ print "ok!\n"; }else{ print "too hot!\n"; }

Slide 6 until The Perl until statement is like a while statement with the condition negated. l Sometimes is is easier to say “until something is true” rather than “while not this is true”: while(!$endoffile){... } until($endoffile){# same thing... }

Slide 7 until The until statement loops indefinitely, until the condition is true, such as a user-controlled condition: #!/usr/local/bin/perl5 -w $resp = "no"; until($resp eq "yes"){ print "Wakeup [yes/no]? "; chomp($resp = ); } $ test11 Wakeup [yes/no]? no Wakeup [yes/no]? y Wakeup [yes/no]? yes $

Slide 8 do while The do while statement is like the C++ do while statement. l It loops indefinitely, while the condition is true, such as a user-controlled condition: do while always executes the body of the loop at least once. #!/usr/local/bin/perl5 -w do{ print "Wakeup [yes/no]? "; chomp($resp = ); }while($resp ne "yes"); $ test11 Wakeup [yes/no]? no Wakeup [yes/no]? yes $

Slide 9 do until The do until statement loops indefinitely, until the condition is true, such as a user- controlled condition. do until always executes the body of the loop at least once. #!/usr/local/bin/perl5 -w do{ print "Wakeup [yes/no]? "; chomp($resp = ); }until($resp eq "yes"); $ test11 Wakeup [yes/no]? no Wakeup [yes/no]? y Wakeup [yes/no]? yes $

Slide 10 foreach foreach takes a list of values and assigns them one by one to a scalar variable. l The body of the loop is executed once for each successive assignment. foreach is similar to the shell programming’s for statement. foreach $i }

Slide 11 foreach l The following example sums the contents of an array: $ cat sum #!/usr/local/bin/perl5 = (21,32,3,44,75,16,19); $sum = 0; foreach $b $sum += $b; } print "The array sum is: $sum\n"; $ sum The array sum is: 210 $

Slide 12 foreach foreach allows us to easily print an array in our own customized way. l The following example prints an array with each element separated by 2 spaces: $ cat print1 #!/usr/local/bin/perl5 = (1,2,3,4,5); foreach $i print "$i "; } print "\n"; $ print $

Slide 13 foreach l The following example prints the numbers in reverse order without changing the array: $ cat print2 #!/usr/local/bin/perl5 = (1,2,3,4,5); foreach $i print "$i "; } print "\n"; $ print $ is the same as writing Parenthesis are always optional on Perl functions.

Slide 14 foreach $_ If you omit the scalar variable in foreach, Perl will use $_ automatically: $ cat print3 #!/usr/local/bin/perl5 = (1,2,3,4,5); foreach print; } print "\n"; $ print $ print (and other Perl functions) use $_ as the default if nothing is specified.

Slide 15 foreach $_ Of course, if you want double spaces, you will have to use $_ explicitly: $ cat print3a #!/usr/local/bin/perl5 = (1,2,3,4,5); foreach print "$_ "; } print "\n"; $ print3a $

Slide 16 foreach The scalar variable in foreach is an alias for each variable in the list, not just a copy. (Tricky!) If you modify the scalar variable in foreach, the aliased element in the list is also changed: $ cat double #!/usr/local/bin/perl5 = (1,2,3,4); foreach $b $b *= 2; } print $ double $

Slide 17 last Again The last command only breaks out of while, until, for, and foreach loops. For mysterious reasons, do while and do until do not count as loops in Perl. $ cat last1 #!/usr/local/bin/perl5 -w do{ print "Wakeup [yes/no]? "; chomp($resp = ); if($resp eq "yes"){ last; } }while(1); $ last1 Wakeup [yes/no]? yes Can't "last" outside a block at last1 line 6, chunk 1. $

Slide 18 last Again In the example below, the last statement causes the outer while loop to be broken: $ cat last2 #!/usr/local/bin/perl5 -w while(1){ do{ print "Wakeup [yes/no]? "; chomp($resp = ); if($resp eq "yes"){ last; } }while(1); } $ last2 Wakeup [yes/no]? yes $

Slide 19 next The next command causes a jump to the end of the loop, but without terminating the loop. $ cat next1 #!/usr/local/bin/perl5 -w $resp = "no"; until($resp eq "quit" || $resp eq "yes"){ print "Wakeup [yes/no/quit]? "; chomp($resp = ); if($resp eq "yes"){ next; } print "sleeping...\n"; } $ next1 Wakeup [yes/no/quit]? no sleeping... Wakeup [yes/no/quit]? yes $ next1 Wakeup [yes/no/quit]? quit sleeping... $

Slide 20 redo The redo command causes a jump back to the beginning of the loop, but without re-testing the condition ( next re-tests the condition). $ cat redo #!/usr/local/bin/perl5 -w $resp = "no"; until($resp eq "quit" || $resp eq "yes"){ print "Wakeup [yes/no/quit]? "; chomp($resp = ); if($resp eq "yes"){ redo; } print "sleeping...\n"; } $ redo Wakeup [yes/no/quit]? no sleeping... Wakeup [yes/no/quit]? yes Wakeup [yes/no/quit]? quit sleeping... $

Slide 21 next and redo Like last, next and redo only break out of while, until, for, and foreach loops (not do while and do until loops). $ cat redo1 #!/usr/local/bin/perl5 -w $resp = "no"; do{ print "Wakeup [yes/no/quit]? "; chomp($resp = ); if($resp eq "yes"){ redo; } print "sleeping...\n"; }until($resp eq "quit" || $resp eq "yes"); $ redo1 Wakeup [yes/no/quit]? no sleeping... Wakeup [yes/no/quit]? yes Can't "redo" outside a block at redo1 line 7, chunk 2. $

Slide 22 redo and last/next You can make a while loop using redo and last/next in a “naked” block (a “naked” block is a block { } that is not part of a loop or if statement): $ cat redo2 #!/usr/local/bin/perl5 -w { print "Wakeup [yes/no]? "; chomp($resp = ); if($resp eq "yes"){ last;# could also use "next;" } redo; } $ redo2 Wakeup [yes/no]? no Wakeup [yes/no]? y Wakeup [yes/no]? yes $

Slide 23 Labeled Blocks l What if you want to jump out of two nested loops? l In C++, you’d be in trouble. In Perl, you can use labeled loops to break out of an outer loop using last, next, and redo. l For clarity (and other reasons), it is best to choose label names that are all upper case letters and digits. l Labels always end in a colon “:”. OUTERLOOP: for($i=1; $i<3; $i++){ INNERLOOP: for($j=1; $j<5; $j++){... Add the label (without the colon) as a parameter to last, for example, if you want to exit OUTERLOOP. last OUTERLOOP;

Slide 24 Labeled Blocks Example $ cat label #!/usr/local/bin/perl5 -w $sum = 0; LOOP1: for($i1=1; $i1<=2; $i1++){ LOOP2: for($i2=1; $i2<=2; $i2++){ LOOP3: for($i3=1; $i3<=2; $i3++){ $sum += 1; print "sum so far: $sum\n"; if($sum > 2){ last LOOP2; } $sum += 2; } $sum += 3; } print "sum at end: $sum\n"; $ label sum so far: 1 sum so far: 2 sum so far: 5 sum so far: 9 sum at end: 12 $

Slide 25 Backward if l A simple way to write “if this, then that” is: chomp($user = `whoami`); print("Hi Bill!\n") if($user eq "gates"); is the same as: chomp($user = `whoami`); if($user eq "gates"){ print "Hi Bill!\n"; } Backward if avoids having to write the curly braces { }. l There can only be one statement inside the block.

Slide 26 Backward if Backward if is a natural and tidy way to exit from a loop: $ cat backif #!/usr/local/bin/perl5 -w while(1){ print "Wakeup [yes/no]? "; chomp($resp = ); last if $resp eq "yes"; }; $ backif Wakeup [yes/no]? no Wakeup [yes/no]? y Wakeup [yes/no]? yes $

Slide 27 Backward unless, while, until You can also use backward unless, while, and until (if there is only one statement in the block): $ cat sum #!/usr/local/bin/perl5 -w print "Enter numbers to sum (0 to quit): \n"; $sum = 0; $n = 1; $sum += $n = while($n != 0); print "The sum is: $sum: \n"; $ sum Enter numbers to sum (0 to quit): 1 0 The sum is: 2 $

Slide 28 && if Another simple way to write “if this, then that” is: chomp($user = `whoami`); $user eq "gates" && print("Hi Bill!\n"); is the same as: chomp($user = `whoami`); if($user eq "gates"){ print("Hi Bill!\n"); }

Slide 29 && if this && that; l Why does this work? Isn’t && the logical-and operator? Consider what happens when this and that take on values of true and false: If this is true, then the value of the entire expression is still not known, because it depends on the value of that. So that has to be evaluated. If this is false, there is no need to look at that, because the value of the whole expression must be false. Since there is no need to evaluate that, Perl skips it.

Slide 30 && if && if is also a tidy way to exit from a loop: $ cat backif1 #!/usr/local/bin/perl5 -w while(1){ print "Wakeup [yes/no]? "; chomp($resp = ); $resp eq "yes" && last; }; $ backif1 Wakeup [yes/no]? no Wakeup [yes/no]? y Wakeup [yes/no]? yes $

Slide 31 || unless Similarly, another simple way to write an unless statement is: $temperature > 20 || print "too cold!\n"; is the same as: unless($temperature > 20){# same thing print "too cold!\n"; }