"Sputnik" help  
Sputnik Help
Language Reference - Datatypes - Conditional Statements

Conditional Statements

You will often want to change the flow of your script based on a condition or series of conditions. Is one number bigger than another? Or, does a string contain a certain value?

Conditions are evaluated as true (non-zero) or false (zero). Conditions generally use comparison operators like ==, <>, >=.

The following conditional statements are available in Sputnik:

Both statements are similar and decide which code to execute depending on the condition given. Here is an example of an If statement that prints a message if a variable is greater than 10.

my $var = 20;
if ($var > 10)
{
    say "\$var was greater than 10!";
}
else
{
    say "\$var was less than 10";
}
// PRINTS
// $var was greater than 10!

In the example above the expression $var > 10 evaluated to true because the variable was indeed greater than 10. This caused the If statement to execute the code and display "$var was greater than 10!".

A Select statement is very similar, but is generally used for situations where you want to test a large number of conditions as it is generally easier to read than a large If type block. e.g.

my $var = 30;
select
{
     case $var > 1 && $var <= 10:
         say "\$var was greater than 1";
         break;
     case $var > 10 && $var <= 20:
         say "\$var was greater than 10";
         break;
     case $var > 20 && $var <= 30:
         say "\$var was greater than 20";
         break;
     case $var > 30 && $var <= 40:
         say "\$var was greater than 30";
         break;
     case $var > 40:
         say "\$var was greater than 40";
         break;
}
// PRINTS
// $var was greater than 20

A Switch statement is very similar to Select statement, but is generally used for situations where the same expression is tested against some different possible values.

my $var = 30;
switch($var)
{
     case 1 rr 10:
         say "\$var was greater than 1";
         break;
     case 11 rr 20:
         say "\$var was greater than 10";
         break;
     case 21 rr 30:
         say "\$var was greater than 20";
         break;
     case 31 rr 40:
         say "\$var was greater than 30";
         break;
     default:
         say "\$var was greater than 40 or less or equal to 0";
         break;
}
// PRINTS
// $var was greater than 20

 


Contact
Cheryl (uberfox@hotmail.com)
Homepage
http://ubersoft.org