Saturday, December 27, 2014

Tutorial 6 : Condition Example

In this tutorial, we will learn few common condition statement which is helps in decision making. Normally these kind of conditions is used to evaluated in program. If the condition is true, it will execute the following statement; if false, it will execute another statement. Let's start to learn how to use it.

If Statement

In this case, if the boolean expression is true, then the block code in the if statement will be executed. While don't care it is true or not, it will execute the block code after the if statement too. Below is the example of this case.

IF condition statement flow.

#!/usr/bin/perl

$money = 100;

if ($money==100)
{
  print "I have 100 dollar\n";
}

print "I don't have 100 dollar\n";



The output of this example is shown in the video below and there will show us what is the output if the $money is not equal to 100. There will be a different for it, please pay attention on it to have a clear image how it works.


If ... Else Statement

In this case, it is different with if statement. If the boolean expression is return true, then the if block statement will be executed, otherwise else block statement will be executed. Let's see how it perform.

If ... Else condition statement flow

#!/usr/bin/perl

$money = 200;

if ($money==100)
{
   print "Money value is equal to 100\n";
}
else
{
   print "Money value is not equal to 100\n";
}

The output of above coding is shown in the video below. Let's us notice what is the output different between the if statement case.


If ... Elsif ... Else Statement

In this part, we will learn another powerful tool which is if...elsif...else statement. We notice that, there is an if statement that can followed by an optional elsif and else statement. It is very useful when we have several conditions need to be check instead of using single if...else statement.

#!/usr/bin/perl

$money = 50;

if ($money==100)
{
  print "Money equal to 100 dollar\n";
}
elsif ($money==200)
{
  print "Money equal to 200 dollar\n";
}
else
{
  print "Money is not equal to 100 or 200 dollar\n";
}

The output of this example is shown as video below.


Switch Statement

A switch statement allows the variable to be tested for equality against a number of values, which is more easier compared to if...else statement in term of coding. Each value to be compared is called a case, while the variable to be switched for checking in each case, thus is called switch case statement as well.

Switch case flow

#!/usr/bin/perl

use Switch;

$money = 50;

switch($money)
{
  case 100 {print "money equal 100\n"}
  case 200 {print "money equal 200\n"}
  case 40  {print "money equal 40\n"}
  case 50  {print "money equal 50\n"}
  else     {print "money equal others\n"}
}

The output of this example code is shown as the video below.


Congratulation to all of us is completed for this tutorial. In next tutorial, we will learn how loop condition perform in Perl language. See you guys.

No comments:

Post a Comment