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
}
if (CONDITION) {
#code block to be executed
}
else {
#code block to be executed if condition fails
}
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
}
1)
$number=10;
if ( $number = 10 )
{
print "The number is 10";
}
Output:
The number is 10
$number=20;
if ( $number = 10 )
{
print "The number is 10";
}
else
{
print "The number is not 10";
}
Output:
The number is not 10
$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