Perl Tutorial : Perl data types
Perl Tutorial : Perl Operators
Perl Tutorial : Perl loops
Perl Tutorial : Perl Conditionals
Perl Tutorial : Perl Subroutines
Perl Tutorial : Perl regex
Perl Tutorial : Perl Files
Perl Tutorial : Perl & Databases
Perl Tutorial : Perl OOPs
Perl Tutorial : Perl signals
Perl Tutorial : Perl threads
Perl Tutorial : Perl Debugging

Introcution to Perl CGI

Introduction to modperl
Home >> perlconditionals >>

The if...else Statement

This statement uses a relational expression to check the validity of a condition and execute a set of statements enclosed in braces. It returns a Boolean value, true or false, according to the validity of the condition. The syntax of the if...else statement is:
if(condition)
{
 block of statement(s);
}
else
{
 block of statement(s);
}
In this syntax, condition is a relational expression. If the result of this expression is true, then the block of statements following the if statement is executed. Otherwise, the block of statements following the else statement is executed.

In Perl, unlike other languages, all loops and conditional constructs require statements to be enclosed in braces, even for single statements.
#! /usr/bin/perl
print "Enter a value for a: ";
$a = <>;
print "Enter a value for b: ";
$b  = <>;
if ($a>$b)
{
   print "a is greater than b\n";
}
else
{
   print "b is greater than a\n";
}

In this example, the if clause checks whether $a is greater than $b. If the value of $a is greater than $b, then the result is: a is greater than b. Otherwise, the control transfers to the code following the else clause and the statement associated with the else clause is printed as a result.

The if...elsif...else Statement
This statement is used when there is more than one condition to be checked. The syntax of the if...elsif...else statement is:
if (condition)
{
 block of statement(s);
}
elsif (condition)
{
 block of statement(s);
}
else
{
 block of statement(s);
}

In this syntax, if the condition associated with the if clause is false, the control transfers to elsif clause that checks for the next condition. The code associated with elsif clause is executed only if the condition is true or the code associated with the else clause is executed.
#! /usr/bin/perl
print "Enter the score of a student: ";
$score = <>;
if($score>=90)
{
  print "Excellent Performance\n";
}
elsif($score>=70 && $score<90)
{
  print "Good Performance\n";
}
else
{
  print "Try hard\n";
}

Perl functions

scalars and strings

lc
ord

regex (regular expressions)

arrays

shift
split

More..

eval
Share |


Perl examples

  • How to use Data::Dumper?
  • How to parse config file?
  • Include external module in perl..
  • Download perl

    download stable version of perl
    Perl 5.10.1
    Perl interview Questions
    sed tutorial
    awk tutorial

    Join us on Google Groups

    Subscribe to perl
    Email:
    Visit this group
    Perl Jobs


    privacy | sitemap | disclaim | contact us
    © 2009 perlhome.com