Conditional statements are very useful in making the decisions if programmer specifies the more conditions to evaluate the program. The statement will be evaluated when the condition is to be true and in some cases which will be executed if the condition to be false which is explained in the below image.
Following are some types of conditional statement are briefly explained.
IF
If statement consists of one or more statements if boolean expression is true then only inside a block statement will execute if it is false programmer need to set the code after the end statement. The image below demonstrates If statement as shown below.
The code below demonstrates the If statement syntax as shown below.
[code]
if(boolean_expression){
# statement(s) will execute if the given condition is true
}
[/code]
The image below demonstrates to explain the If statement as shown below.
The code below demonstrates the If statement as shown below.
[perl]
$a = 10;
# check the boolean condition using if statement
if( $a < 30 ){
# if condition is true then print the following
printf "a is less than 30\n";
}
print "value of a is : $a\n";
$a = "";
# check the boolean condition using if statement
if( $a ){
# if condition is true then print the following
printf "a has a true value\n";
}
print "value of a is : $a\n";
[/perl]
Result
By running the above code in a Perl command line user can get the following output as shown in below image.
IF ELSE
IF statement followed by Else statement which is an optional and which is executed when the boolean expression is false the syntax of the If else statement as shown below.
[code]
if(boolean_expression){
# statement(s) will execute if the given condition is true
}else{
# statement(s) will execute if the given condition is false
}
[/code]
The Diagram below demonstrates the Flow Diagram of the IF ELSE statement as shown below.
The code below demonstrates the IF ELSE statement as shown below.
[perl]
$a = 100;
# check the boolean condition using if statement
if( $a < 20 ){
# if condition is true then print the following
printf "a is less than 20\n";
}else{
# if condition is false then print the following
printf "a is greater than 20\n";
}
print "value of a is : $a\n";
$a = "";
# check the boolean condition using if statement
if( $a ){
# if condition is true then print the following
printf "a has a true value\n";
}else{
# if condition is false then print the following
printf "a has a false value\n";
}
print "value of a is : $a\n";
[/perl]
Result
By running the above code in a Perl command line user can get the following output as shown in below image.
IF ELSEIF
Which is an optional statement followed by an else statement which is the very useful test for various conditions with a single if..elseif statement. Which must have the at least one else and it must come after any else if. If can also have the zero to many else ifs and which is must present before the else. If an else if succeeds then none of the remaining else if will be tested. The syntax below demonstrates the If else if statement as shown below.
[code]
if(boolean_expression 1){
# Executes when the boolean expression 1 is true
}
elsif( boolean_expression 2){
# Executes when the boolean expression 2 is true
}
elsif( boolean_expression 3){
# Executes when the boolean expression 3 is true
}
else{
# Executes when the none of the above condition is true
}
[/code]
The code below demonstrates the If else if as shown below.
[perl]
$a = 100;
# check the boolean condition using if statement
if( $a == 20 ){
# if condition is true then print the following
printf "a has a value which is 20\n";
}elsif( $a == 30 ){
# if condition is true then print the following
printf "a has a value which is 30\n";
}else{
# if none of the above conditions is true
printf "a has a value which is $a\n";
}
[/perl]
Result
By running the above code in a Perl command line user can get the following output as shown in below image.