Perl Chapter 3 Conditional statements. Control Expressions Control expressions – interpreted as T/F (evaluated as strings or numbers) – simple, relational,

Slides:



Advertisements
Similar presentations
ICE1341 Programming Languages Spring 2005 Lecture #13 Lecture #13 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
Advertisements

Statement-Level Control Structures
Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Statement-Level Control Structures Sections 1-4
CSC 200 Lecture 4 Matt Kayala 1/30/06. Learning Objectives Boolean Expressions –Building, Evaluating & Precedence Rules Branching Mechanisms –if-else.
ISBN Chapter 8 Statement-Level Control Structures.
Chapter 4 Making Decisions
C++ for Engineers and Scientists Third Edition
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Decision Structures and Boolean Logic
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
PL/SQL Loops. Building Logical Conditions All logical conditions must yield a boolean condition. You can build a simple Boolean condition by combining.
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.
Flow of Control Part 1: Selection
Chapter 8 Chapter 8 Control Structures. Control Structures  A control structure is a control statement and the statements whose execution it controls.
Chapter 5 Loops. Overview u Loop Statement Syntax  Loop Statement Structure: while, for, do-while u Count-Controlled Loops u Nested Loops u Loop Testing.
ISBN Chapter 8 Statement-Level Control Structures.
sequence of execution of high-level statements
Control Structures sequence of execution of high-level statements.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Flow of Control Chapter 3. Outline Branching Statements Java Loop Statements Programming with Loops The Type boolean.
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
Topic 2: Working with scalars CSE2395/CSE3395 Perl Programming Learning Perl 3rd edition chapter 2, pages 19-38, Programming Perl 3rd edition chapter.
Digital Image Processing Lecture 6: Introduction to M- function Programming.
CSCI 161 Lecture 7 Martin van Bommel. Control Statements Statements that affect the sequence of execution of other statements Normal is sequential May.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
Chapter 8 Statement-Level Control Structures. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 8 Topics Introduction Selection Statements.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Chapter 7: Expressions and Assignment Statements
Lecture 3- Decision Structures
Chapter 7: Expressions and Assignment Statements
JavaScript: Control Statements I
Operator Precedence Operators Precedence Parentheses () unary
Topics The if Statement The if-else Statement Comparing Strings
JavaScript: Control Statements.
Chapter 8: Control Structures
Flow of Control.
Flow of Control.
Topics The if Statement The if-else Statement Comparing Strings
Outline Altering flow of control Boolean expressions
Chapter 8 JavaScript: Control Statements, Part 2
Control Structures: for & while Loops
Iteration: Beyond the Basic PERFORM
Flow of Control.
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Chapter 6 Control Statements: Part 2
Statement-Level Control Structures
Chapter8: Statement-Level Control Structures April 9, 2019
The Selection Structure
Chap 7. Advanced Control Statements in Java
Flow of Control.
CSC215 Lecture Control Flow.
Controlling Program Flow
REPETITION Why Repetition?
Presentation transcript:

Perl Chapter 3 Conditional statements

Control Expressions Control expressions – interpreted as T/F (evaluated as strings or numbers) – simple, relational, compound Simple control expressions – arithmetic or string expressions – if value is a string empty string and “0” interpreted as false, otherwise true – if value is a number it’s true unless its value is zero – any undefined value is false (because undef number is 0 and undef string is empty

Relational expressions – includes a single relation operator (e.g. >) – Perl includes 2 sets of six relational ops for numbeic and string operands (Table 3.1, p36) – relational operators coerce operands to proper type – strings known to be numbers may be compared with numeric ops “27” > “3” is true string op “27” gt “3” is false – More examples “fruit” eq 7  “fruit” eq “7” “boy” == “girl”  0 == 0

Precedence equal operators have lower precedence – e.g. == != eq, ne all relational ops have precedence lower than arithmetic ops – ex * $a < $limit * first

Compound Expressions Consist of scalar variables, scalar literals, relational expressions and BOOLEAN operators Two sets of Boolean operators – &&, !!, ! lower precedence than relational ops short circuit – and, or, not perform same, except have LOWER precedence than that of any other Perl operators more readable – See Table 3.2 precedence and associativity

Control Statements: Selection if if ($a > $b) { print “\$a is greater than \$b \n”; $max = $a; } if ($total) { 0 interpreted as false $average = $sum / $total; print “The average is $average \n”; } else { print “average cannot be computed. \n”;

unless – doesn’t require reverse logic with if unless (($count < $limit) or $no_limit) { print “I cannot go on!\n” } if (($count >= $limit) and ($nolimit == 0))

Nested if statement if possible, make nested if in the else clause (linear nesting) elsif - example pg. 40 No switch– use nested ifs (later section 3.4)

Conditional expression boolean_expr ? then_expr : else_expr can be used in the assignment statement $average = ($number != 0) ? $sum/$number:0; (pg. 41 example)

Iteration: while, until, and for All pretest loops while (control expr) { #loop body } until ($sum > 1000) { $sum += ; … } (stops when condition becomes true) for (init expr; control expr; increment expr) { #loop body } Examples: gpa1.pl and gpa2.plgpa1.plgpa2.pl

$sum = 0; for ($count = 0; $count <10; $count ++) { $value = ; $sum += $value; } all three expressions are optional (;’s must occur) if control expr omitted  interpreted as true

all three expressions may be multiple expressions (or statements) separated by,’s (comma is an operator) value returned by a list of comma separated expressions is the value of the last expression for ($x = 0, $y =n; $x <n; $x++, $y--) { } assignment statements as control expressions for ($sum=0; $value = ; $sum += $value) { } – when gets to eof, it returns empty string, which represents false variables that appear in loop control expressions may occur in loop body (may even have their value changed)

Ways out of a block: last, next, redo may be used in any block most commonly used within loops abnormal statement flow control - UGLY!!!! next operator (w/out an operand)  transfers control to the end of the block – if block is a while  control goes back to control expression of loop

last operator in a loop body causes loop to be immediately terminated  control is transferred to the first statement after loop body redo operator (appearing in loop body)  causes control to transfer to top of loop body (not the test ) … careful, infinite loop – iteration is redone last, next, redo often used in conjunction with a labeled block

LOOP: indent followed by a colon while (…) { … } In nested loop these operators only apply to loop in which they appear Sometimes we would like to perform these ops on an enclosing loop or block. OUTER: for ( …) { … last OUTER; } switch example: gpa3.plgpa3.pl

last and the fake switch last operator when in loop body causes loop to terminate (control transferred to 1 st statement after loop body) DON’T DO UGLY STUFF! Do for a labeled block, specifically to fake a switch

SWITCH: { if ($rank == 1) { $freshman++; last SWITCH; } if ($rank == 2) { $sophomore++; last SWITCH; } if ($rank == 3) { $junior++; last SWITCH; } if ($rank == 4) { $senior++; last SWITCH; } print “ERROR –not a legal class rank. \n”; }

die takes a list of parameters, catenates them, prints to STDERR (output usually same as STDOUT), terminates program execution die “ERROR –not a legal class rank.”; – example in gpa3.pl line 27 catenates program name, line number of die and \n If you include \n in the string, info not provided System error may be stored in $!, can include die “ERROR –in SWITCH $!”; no error message exit 0;

3.9 Debugger READ IT!