CUNY CSCI 132 - Controlling the Execution Flow in a Program

Unformatted text preview:

Control Structures in PerlControl Flow in ProgramsSequencesAlteration of FlowThe if statementConditionsRelational OperatorsNumeric Relational OperatorsComparing WordsComparing StringsComparing StringsString Relational OperatorsLogical ValuesThe If-else StatementThe if-else StatementExampleExample 2Optional elsif ClausesThe if-elsif-else FlowchartExampleRepetition StatementsRepetition StatementsThe while Loop FlowchartThe while LoopThe while Loop: Second ExampleUse of while LoopsLooking Ahead: The split() FunctionProcessing Tab-Separated Data FilesMore General SplittingParsing FilesParsing files (2)The until Loopfor LoopsExampleSafety of for LoopsDangerous Uses of for LoopslastThe next StatementAnother Example of nextThe foreach StatementThe foreach StatementExampleAnother ExampleLogical OperatorsPerl's Logical OperatorsLogical SemanticsTruth Table for Logical ANDTruth Table for Logical ORTruth Table for Logical NOTExampleDeMorgan's LawLazy EvaluationUse of Lazy EvaluationDigging DeeperMore Lazy EvaluationThe Symbolic Logical OperatorsStatement ModifiersStatement Modifier Example 1Statement Modifier Example 2SummaryCopyright 2006 Stewart WeissCopyright 2009 Stewart WeissControl Structures in PerlControl Structures in PerlControlling the Execution Flow in a Program2 CSci 132 Practical UNIX with PerlControl flow in programsA program is a collection of statements. After the program executes one statement, it "moves" to the next statement and executes that one. If you imagine that a statement is a stepping stone, then you can also think of the execution flow of the program as a sequence of "stones" connected by arrows:statementstatement3 CSci 132 Practical UNIX with PerlSequencesWhen one statement physically follows another in a program, as in $number1 = <STDIN>;$number2 = <STDIN>;$sum = $number1 + $number2; the execution flow is a simple sequence from one statement to the next, without choices along the way. Usually the diagrams use rectangles to represent the statements:stmt 1 stmt 2 stmt 34 CSci 132 Practical UNIX with PerlAlteration of flowSome statements alter the sequential flow of the program. You have already seen a few of these. The if statement is a type of selection, or branching, statement. Its syntax is if ( condition ) { block }in which condition is an expression that is evaluated to determine if it is true or false. If the condition is true when the statement is reached, then the block is executed. If it is false, the block is ignored. In either case, whatever statement follows the if statement in the program is executed afterwards.5 CSci 132 Practical UNIX with PerlThe if statementThe flow of control through the if statement is depicted by the following flow-chart (also called a flow diagram):if-blockif ( condition)truenext statementfalse6 CSci 132 Practical UNIX with PerlConditionsThe condition in an if statement can be any expression. Although any expression can be used as a condition, in programs that follow good principles of software design, the condition is usually one that is built from relational operators and/or logical operators.For example, $x > $yis a condition that is true if the value of $x is greater than the value of $y when the condition is reached.7 CSci 132 Practical UNIX with PerlRelational operatorsRelational operators are operators that compare two expressions. In math we use operators like >, <, and ≠ to compare numeric expressions. There is no symbol "≤" on the keyboard, so we use a pair of symbols "<=" instead. There are six numeric relational operators in Perl, which are listed in the next slide.8 CSci 132 Practical UNIX with PerlNumeric relational operatorsThe numeric relational operators in Perl are Operator Example Meaning> $x > $ytrue if $x is greater than $y< $x < $ytrue if $x is less than $y== $x == $ytrue if $x equals $y!= $x != $ytrue if $x does not equal $y>= $x >= $ytrue if $x > $y or $x == $y<= $x <= $ytrue if $x < $y or $x == $y9 CSci 132 Practical UNIX with PerlComparing wordsWhen you look up words in a dictionary, or sort names, you use an implicit rule for ordering strings, usually called dictionary order.a < b < c < … < z orders the letters, and two words w and v are ordered using the rules:1. If first letter of w < first letter of v, then w is less than v2. If the first n letters of w and v are the same, but the (n+1)st of w < (n+1)st of v, then w is less than v3. If w is a prefix of v then w is less than v. 4. If the words are identical, then w equals v10 CSci 132 Practical UNIX with PerlComparing stringsIn Perl, a different rule is used to order the characters, but the rule for words remains the same. The characters are ordered by their ASCII values. In the ASCII ordering, all punctuation precedes digits, which precede uppercase letters, which precede lowercase letters. In UNIX, you can type "man ascii" to see the ASCII table. Thus,blank < … < 0 < 1 < 2 < ... < 9 < ... < A < … < Z < a < … < z11 CSci 132 Practical UNIX with PerlComparing strings Examples:'A' is less than 'a''Zoo' is less than 'apple''apple' is less than 'zoo''100' is less than '20' '111' is less than 'a'The string relational operators are listed on the next page. Note that they are different from the numerical operators. You MUST use these when comparing strings.12 CSci 132 Practical UNIX with PerlString relational operatorsThe string relational operators in Perl are Operator Example Meaninggt $x gt $ytrue if $x is greater than $ylt $x lt $ytrue if $x is less than $yeq $x eq $ytrue if $x equals $yne $x ne $ytrue if $x does not equal $yge $x ge $ytrue if $x gt $y or $x eq $yle $x le $ytrue if $x lt $y or $x eq $y13 CSci 132 Practical UNIX with PerlLogical values Perl will convert all expressions to true and false, even if they have no relational operators in them. The rules are:Any number other than 0 is true; 0 is false.The empty string ('' or "" ) is false.A string containing only a zero, i.e., "0" or '0', is false.Anything that is undefined is false.Thus, the following expressions are true:"hello" 62 "00" "\t" "0.0"and these are false: "0" '0' 014 CSci 132 Practical UNIX with PerlThe if-else StatementThe flow of control through the if-else statement is depicted by the following flow-chart. Notice how it differs from the if statement.true-blockif ( condition)truefalse-blockfalsenext statement15 CSci 132 Practical UNIX with PerlThe if-else statementThe if


View Full Document

CUNY CSCI 132 - Controlling the Execution Flow in a Program

Download Controlling the Execution Flow in a Program
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Controlling the Execution Flow in a Program and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Controlling the Execution Flow in a Program 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?