Page 1 of 1

Perl Conditional Statements Rate Topic: -----

#1 jumptrooper  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 68
  • View blog
  • Posts: 234
  • Joined: 19-August 10

Posted 19 August 2010 - 08:04 PM

Perl uses the conditional statements "if", "else", "elsif", and "unless". It also uses the "?" operator (known as the "turing operator")

IF, ELSE, ELSIF


In true Perl fasion, there are five different ways to use an "if" statement.

if ( expression ) block
if ( expression ) block else block
if ( expression ) block elsif ( expression ) block ...
if ( expression ) block elsif ( expression ) block ... else block
statement if ( expression )


In the first case if the expression evaluates to a non-zero, non-null value, the following block of code executes.
EX:
if($a > $B)/>{   
  print "$a is greater than $b";
}



The second case is an extension of the first. If the expression is true, the block of code following the "if" executes. If the expression is false (0 or null), the block of code following the "else" executes.
EX:
if($a > $B)/>{   
  print "$a is greater than $b";
}
else{
  print "$a is NOT greater than $b";
}


The third statement shows us Perl's lazy side. Rather than strain your hands typing out "else if(...){", you can just type "elsif(...){".
EX:
if($a > $B)/>{
  print "$a is greater than $b";
}
elsif($a > $c){
  print "$a may not be bigger than $b, but it is bigger than $c";
}



The fourth is just the combination of the previous two.
EX:
if($a > $B)/>{   
  print "$a is greater than $b";
}
elsif($a < $B)/>{
  print "$b is greater than $a";
}
else{
  print "$a and $b are equal";
}


The fifth usage of "if" is reserved for cool people that want to sound good as they read their code out loud to their friends. It allows you to put your conditional expression after your statement, kind of like yelling "NOT!" after you just said how lame Perl is.
EX:
print "Let's date!" if(($girl != $ugly) and ($personality >= $good));

or
print "Yay!" if($dayIveBeenWaitingFor == $today);



UNLESS


The "unless" statement works exactly the same way as "if", but with the opposite effect.
EX:
print "BOOOOO!" unless ($myPoints > $yourPoints);

or
unless ($a > $B)/>{
  print "$b is the biggest!";
}
else{
  print "$a is bigger than $b";
}



?


The turing operator ( "?" ) is functionally identical to an if...else statement, but provides a shorter (possibly single line) means of expressing it.
The format is:
(expression) ? (statement if true) : (statement if false);

EX:
($a > $B)/> ? print "$a wins" : print "$a doesn't win";

This post has been edited by jumptrooper: 23 December 2010 - 02:49 PM
Reason for edit:: Changed $B to $b


Is This A Good Question/Topic? 2
  • +

Replies To: Perl Conditional Statements

#2 jumptrooper  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 68
  • View blog
  • Posts: 234
  • Joined: 19-August 10

Posted 06 September 2010 - 10:53 AM

Can an admin change all the "$B"s to "$b", please? Thanks!
Was This Post Helpful? 0
  • +
  • -

#3 newclearner  Icon User is offline

  • D.I.C Regular

Reputation: 103
  • View blog
  • Posts: 302
  • Joined: 29-September 10

Posted 19 December 2010 - 06:21 AM

I have just started learning Perl. You tutorial introduces the basic concepts in quick and crisp manner.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1