In the last tutorial, we learnt about if statements in perl. In this tutorial we will learn about if-else conditional statement. This is how an if-else statement looks:
if(condition) { Statement(s); } else { Statement(s); }
The statements inside “if” would execute if the condition is true, and the statements inside “else” would execute if the condition is false.
Example:
#!/usr/local/bin/perl printf "Enter any number:"; $num = <STDIN>; if( $num >100 ) { # This print statement would execute, # if the above condition is true printf "number is greater than 100\n"; } else { #This print statement would execute, #if the given condition is false printf "number is less than 100\n"; }
Output:
Enter any number:120 number is greater than 100
Leave a Reply