FLOW CONTROL : UNLESS STATEMENT


In the last post we learnt about the basic PERL FLOW, and we also learnt that we can alter the flow of code depending on our needs. We also learnt about conditional statements viz IF statement. When we want to make some decisions we can use IF statement, based on which a block of code is executed.

Here in this post we will learn about another CONDITIONAL STATEMENT, the UNLESS statement. This is exactly the opposite of IF statement. We know in IF statement if the condition is True, only then some BLOCK of code is executed, but with UNLESS statement, BLOCK of code will be executed if the condition is FALSE

UNLESS works just like IF, but its just the opposite, in IF the statements in blocks are executed if the LOGIC is true or condition is true, but in UNLESS the block is executed only when the condition is false.

unless(LOGIC) {BLOCK}

This means, unless ( LOGIC IS FALSE ) { Execute this part of the code }

So we can say that:

unless(LOGIC) {BLOCK} is equal to if(!LOGIC) {BLOCK}

With that said, lets try this small Perl script to testify:


[gray@ckserver Perl Programming]$ cat un.pl
#!/usr/bin/perl

use strict;
use warnings;

my $a = 10;

unless($a == 9)
{
    print "\nI am executing coz the condition is false..\n";
}

[gray@ckserver Perl Programming]$
[gray@ckserver Perl Programming]$ perl un.pl

I am executing coz the condition is false..


So its very clear from the example script that, if we use unless, the condition has to be false .

Thats all for this post, have fun.


Feel Free To Leave A Comment
If Our Article has Helped You, Support Us By Making A Small Contribution, Thank You!

0 comments: