Programming Basics

  Home arrow Programming Basics arrow Logical Operators and Control Flow Con...
PROGRAMMING BASICS

Logical Operators and Control Flow Constructs
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating:  stars stars stars stars stars / 0
    2012-02-29

    Table of Contents:
  • Logical Operators and Control Flow Constructs
  • Even More Choices: if . . . elsif . . . else

  •  
     

    SEARCH CODEWALKERS

    Logical Operators and Control Flow Constructs


    (Page 1 of 2 )

    In this fourth part of a nine-part article series on the variety of control structures Perl programmers can use, we'll take a close look at logical operators. It is excerpted from chapter three of the book Beginning Perl, Second Edition, written by James Lee (Apress; ISBN: 159059391X).

    Logical Operators

    We also saw in Chapter 2 that we can join together several tests into one by the use of the logical operators. Table 3-3 provides a summary of those.

    Table 3-3. Logical Operators

    Operator

    Description

    $x and $y

    True if both $x and $y are true

    $x && $y

     

    $x or $y

    True if either of $x or $y, or both are true

    $x || $y

     

    not $x

    True if $x is not true

    ! $x

     

    The operatorsand,or, andnotare usually used instead of&&,||, and!mainly due to their readability. The operatornot means not, after all. Don’t forget there is a difference in precedence between the two—and,or, andnotall have lower precedence than their symbolic representations.

    Multiple Choice: if . . . else

    Consider these two if statements:

    if ($password eq $guess) {
    print "Pass, friend.\n";
    }
    if ($password ne $guess) {
    die "Go away, imposter!\n";
    }

    We know that if the first test condition is true, then the second one will not be—we’re asking exactly opposite questions: Are these the same? Are they not the same?

    In which case, it seems wasteful to do two tests. It’d be much nicer to be able to say, “If the strings are the same, do this. Otherwise, do that.” And in fact we can do exactly that, although the keyword is not otherwise but else:

    if ($password eq $guess) {
    print "Pass, friend.\n";
    } else {
    die "Go away, imposter!\n";
    }

    That’s

    if ( condition ) { action } else { alternative action }

    Like theifstatement, those curly braces are required in theelsepart.

    More Programming Basics Articles
    More By Apress Publishing

    blog comments powered by Disqus

    PROGRAMMING BASICS ARTICLES

    - The Transliteration Operator in Perl
    - Perl String Processing Functions
    - Perl String Processing
    - Control Flow Constructs: Loops Conclusion
    - Loop Control Constructs
    - Control Flow Constructs: the For and Foreach...
    - Loops and Control Flow Constructs
    - Expression Modifiers for Perl Control Flow C...
    - Logical Operators and Control Flow Constructs
    - Comparing Strings with Control Flow Construc...
    - Perl Operators and Control Flow Constructs
    - Control Flow Constructs
    - More Time Manipulation with PHP
    - Validating and Manipulating Dates with PHP
    - Using the Date Constructor in PHP

    Developer Shed Affiliates

     



    © 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap