Sunday, July 17, 2011

if statement and syntax perl

if statement:
If statement is mainly used by for decision making.
There are actually 3 ways it can be used.
Different Syntax which can be used:

1)

if (CONDITION) {
#code block to be executed
}
2)

if (CONDITION) {
#code block to be executed
}
else {
#code block to be executed if condition fails
}
3)

if (CONDITION_ONE) {
# code block to be executed if CONDITION_ONE passes
}
elsif (CONDITION_TWO)
{
#code block to be executed if CONDITION_TWO passes and CONDITION_ONE FAILS
}
else
{
# code block to be executed if both conditions fails
}
Example:

1)

$number=10;
if ( $number = 10 )
{
print "The number is 10";
}

Output:
The number is 10

2)

$number=20;

if ( $number = 10 )
{
print "The number is 10";
}
else
{
print "The number is not 10";
}
Output:
The number is not 10

3)

$number=20;
if ( $number = 10 )
{
print "The number is 10";
}
elsif ($number =20 )
{
print "The number is 20";
}

else
{
print "the number is not 10 and 20";
}

Output:
The number is 20;


No comments:

Post a Comment