these operators are discussed thoroughly in our perl operators lesson

Upload: dereasepiokio

Post on 30-May-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/9/2019 These Operators Are Discussed Thoroughly in Our PERL Operators Lesson.

    1/21

    Conditional statements may involve logical operators and usually test equality orcompare one value to another. Here's a look at some common conditional statements.Remember that the code to be executed will only execute if the condition in parenthesesis met.ifs.pl:

    #!/usr/bin/perl

    # HTTP HEADERprint "content-type: text/html \n\n";

    # SOME VARIABLES$x = 7;$y = 7;

    # TESTING...ONE, TWO...TESTING

    if ($x == 7) {print '$x is equal to 7!';print "
    ";

    }if (($x == 7) || ($y == 7)) {

    print '$x or $y is equal to 7!';print "
    ";

    }if (($x == 7) && ($y == 7)) {

    print '$x and $y are equal to 7!';print "
    ";

  • 8/9/2019 These Operators Are Discussed Thoroughly in Our PERL Operators Lesson.

    2/21

    }

    if.pl:$x is equal to 7!$x or $y is equal to 7!

  • 8/9/2019 These Operators Are Discussed Thoroughly in Our PERL Operators Lesson.

    3/21

    $x and $y are equal to 7!

    These operators are discussed thoroughly in our PERL Operators lesson.PERL - If Else

    The else statement is a perfect compliment to an if statement. The idea behind them isthat if the conditional statement is not met then do this. In hypothetical, plain english, "Ifthis is true do this, if not do this."ifelse.pl:

  • 8/9/2019 These Operators Are Discussed Thoroughly in Our PERL Operators Lesson.

    4/21

    #!/usr/bin/perl

    # HTTP HEADERprint "content-type: text/html \n\n";

  • 8/9/2019 These Operators Are Discussed Thoroughly in Our PERL Operators Lesson.

    5/21

    # SOME VARIABLES$name = "Sarah";

    $x = 5;

    all television

    stationsbroadcasting

    workersarriving in

  • 8/9/2019 These Operators Are Discussed Thoroughly in Our PERL Operators Lesson.

    6/21

    Indonesiafollowing themovementsinvolved Teruadi want to seehear thetelevision tohear music

  • 8/9/2019 These Operators Are Discussed Thoroughly in Our PERL Operators Lesson.

    7/21

    everywherewant out of theroom to the

    bedroom tosleep wants totake a showergo whereverour leaders

  • 8/9/2019 These Operators Are Discussed Thoroughly in Our PERL Operators Lesson.

    8/21

    commandertelevision

    broadcasters areeager to

    participatepemimpinnnyarobert adhiKusuma's son

  • 8/9/2019 These Operators Are Discussed Thoroughly in Our PERL Operators Lesson.

    9/21

    # IF/ELSE STATEMENTSif ($x > 10) {

    print "$x is greater than 10!";} else {

    print "$x is not greater than 10!";}print "
    ";

    # STRINGS ARE A LITTLE DIFFERENTif ($name eq "Sarah") {

    print "Hello, $name!";

    } else {print "You are not $name!";

    }

    PERL - Elsif Statements

    Elsif statements test another conditional statement before executing code. This waymultiple conditions can be tested before the script continues.

  • 8/9/2019 These Operators Are Discussed Thoroughly in Our PERL Operators Lesson.

    10/21

    elsif.pl:

    #!/usr/bin/perl

    # HTTP HEADER

    print "content-type: text/html \n\n";

    # SOME VARIABLES

    $x = 5;

    # PLAY THE GUESSING GAME

    if ($x == 6) {print "X must be 6.";

    }elsif ($x == 4) {

    print "X must be 4.";}elsif ($x == 5) {

    print "X must be 5!";

  • 8/9/2019 These Operators Are Discussed Thoroughly in Our PERL Operators Lesson.

    11/21

    }

    We could take this example a step further, adding an else statement at the bottom. Thiswould serve as a catch all if none of the conditions were met.

    Bookmark and Share

    * Go Back* Continue

    Found Something Wrong in this Lesson?

    Report a Bug or Comment on This Lesson - Your input is what keeps Tizag improvingwith time!

    Web ReferenceHTML Reference CSS Reference CSS Examples PHP Examples Help Tizag GrowLink to TizagNew - Tizag.com Forums!

  • 8/9/2019 These Operators Are Discussed Thoroughly in Our PERL Operators Lesson.

    12/21

    Recent Forum Topics:

    - table border colors- Wanna Make Money, Its Very Simple... Here's A Way!!

    - Getting this code to work- What do you think/and little problem.

    Advertise Here

    More Tutorials!Microsoft Office Tutorials Artist Tutorials While loops continually iterate as long as theconditional statement remains true. It is very easy to write a conditional statement thatwill run forever especially at the beginner level of coding. On a more positive note, whileloops are probably the easiest to understand. The syntax is while (coditional statement){ execute code; }.Advertise on Tizag.comwhilecounter.pl:

    #!/usr/bin/perl

    print "content-type: text/html \n\n";

  • 8/9/2019 These Operators Are Discussed Thoroughly in Our PERL Operators Lesson.

    13/21

    # SET A VARIABLE$count = 0;

    # RUN A WHILE LOOPwhile ($count

  • 8/9/2019 These Operators Are Discussed Thoroughly in Our PERL Operators Lesson.

    14/21

    1 all televisionstations

    broadcastingworkersarriving inIndonesiafollowing themovements

  • 8/9/2019 These Operators Are Discussed Thoroughly in Our PERL Operators Lesson.

    15/21

    involved Teruadi want to seehear thetelevision tohear musiceverywherewant out of theroom to the

  • 8/9/2019 These Operators Are Discussed Thoroughly in Our PERL Operators Lesson.

    16/21

    bedroom tosleep wants totake a showergo whereverour leaderscommandertelevision

    broadcasters are

  • 8/9/2019 These Operators Are Discussed Thoroughly in Our PERL Operators Lesson.

    17/21

    eager toparticipatepemimpinnnyarobert adhiKusuma's son234567Finished Counting!PERL - Next, Last, and Redo

    Outlined below are several interrupts that can be used to redo or even skip iterations of

    code. These functions allow you to control the flow of your while loops.

    NextPlace it inside your loop and it will stop the current iteration and go on to the next one.ContinueExecuted after each loop iteration and before the conditional statement is evaluated. Agood place to increment counters.Last

  • 8/9/2019 These Operators Are Discussed Thoroughly in Our PERL Operators Lesson.

    18/21

    Last stops the looping immediately (like break)RedoRedo will execute the same iteration over again.

    flowcontrol.pl:

    #!/usr/bin/perl

    print "content-type: text/html \n\n";

    # SET A VARIABLE

    $count = 0;while ($count

  • 8/9/2019 These Operators Are Discussed Thoroughly in Our PERL Operators Lesson.

    19/21

    # PRINT THE COUNTER

    print $count."
    ";

    }continue {

    $count++;};

    print "Loop Finished!";

    flowcontrol.pl:0

    123Skip Four!567Finished Counting!

  • 8/9/2019 These Operators Are Discussed Thoroughly in Our PERL Operators Lesson.

    20/21

    Above, we skip the fourth iteration by incrementing the variable again. In the example wealso print a line, "Skip Four!" just to make things easier to follow.PERL - While Array Loop

    Here we are just showing a method of looping through an array using a while loop. Weuse three variables to do this including: the array, a counter, and an index number so thateach time the while loop iterates we also loop through each index of the array.whilearrayloop.pl:

    #!/usr/bin/perl

    print "content-type: text/html \n\n";

    # SET UP AN HTML TABLEprint "";

    # DEFINE AN ARRAY@names = qw(Steve Bill Connor Bradley);

    # COUNTER - COUNTS EACH ROW$count = 1;

    # COUNTS EACH ELEMENT OF THE ARRAY$n = 0;

    # USE THE SCALAR FORM OF ARRAYwhile ($names[$n]) {

    print "$count$names[$n]";$n++;$count++;

    }print "";

    while.pl:1 Steve2 Bill3 Connor 4 Bradley

    Bookmark and Share

    * Go Back* Continue

  • 8/9/2019 These Operators Are Discussed Thoroughly in Our PERL Operators Lesson.

    21/21

    Found Something Wrong in this Lesson?

    Report a Bug or Comment on This Lesson - Your input is what keeps Tizag improvingwith time!

    Web ReferenceHTML Reference CSS Reference CSS Examples PHP Examples Help Tizag GrowLink to TizagNew - Tizag.com Forums!Recent Forum Topics:- table border colors- Wanna Make Money, Its Very Simple... Here's A Way!!- Getting this code to work

    - What do you think/and little problem.

    Advertise Here

    More Tutorials!Microsoft Office Tutorials Artist Tutorials

    2003-2008 Erack Network | Copyright | Privacy Policy | Advertising InformationSite design by Seattle Web Design