Similar to unless statement, the unless-else statement in Perl behaves opposite to the if-else statement. In unless-else, the statements inside unless gets executed if the condition is false and statements inside else gets executed if the condition is true.
unless(condition) { #These statements would execute #if the condition is false. statement(s); } else { #These statements would execute #if the condition is true. statement(s); }
Example
#!/usr/local/bin/perl printf "Enter any number:"; $num = <STDIN>; unless($num>=100) { #This print statement would execute, #if the given condition is false printf "num is less than 100\n"; } else { #This print statement would execute, #if the given condition is true printf "number is greater than or equal to 100\n"; }
Output:
Enter any number:100 number is greater than or equal to 100
Leave a Reply