"Sputnik" help  
Sputnik Help
If(<expression>)
{
statements
...
}
Else If(expression-n)
{
else if statements
...
}
Else
{
else statements
...
}

Description

Conditionally run statements.

Parameters

expression If the expression is true, the first statement block is executed. If not, the first true Else If block is executed. Otherwise, the "Else" block is executed.

Both the Else If and Else blocks are optional.

Remarks

If statements may be nested.

You may mix in Unless stuff with this statement.

The If...Else If...Else statement in Sputnik provides a way to conditionally execute statements based on the evaluation of one or more expressions. It allows you to handle different scenarios and make decisions based on the truth or falsity of certain conditions.

The If block is the primary block where the initial expression is evaluated. If the expression is true, the statements within the If block are executed. If the expression is false, the program moves on to the next block, which is the Else If block. This block allows you to evaluate additional expressions and execute statements based on their truth or falsity. The Else If block can be repeated multiple times to handle various conditions.

If none of the If or Else If expressions evaluate to true, the program proceeds to the Else block. The Else block is optional, but when included, its statements are executed if none of the previous conditions were met.

Nested If...Else If...Else statements are supported, allowing for more complex decision-making logic. You can nest If blocks within other If blocks to handle specific conditions within a broader context.

The expressions used within the If...Else If...Else statement can include boolean operators and logical operators to create more complex conditions. Parentheses can be used to group expressions and control the evaluation order.

You can also use the Unless statement in conjunction with the If...Else If...Else statement to further expand the conditional logic. The Unless statement allows you to check if an expression is false and execute statements based on that condition.

Additionally, you can use the ternary operator (? :) as a shorthand for simple If...Else statements, especially when assigning values to variables or printing output. It provides a concise way to make a decision based on a condition.

The If...Else If...Else statement in Sputnik offers a flexible and structured approach to handle conditional execution of statements. It allows you to handle different scenarios, make decisions based on conditions, and control the flow of your program. By using this statement, you can create logic that responds dynamically to changing conditions and efficiently directs the execution path of your code.

Related

Unless, Select

Example

Basic example:

my $a = 100;
if($a == 100)
{
    say "Value is 100";
}
// PRINTS
// Value is 100

All features:

my $a = 200;
if($a == 100)
{
    say "Value is 100";
}
else if($a == 200)
{
    say "Value is 200";
}
else
{
    say "No Match";
}
// PRINTS
// Value is 200

Example of using the logical ! operator:

my $a = 200;
if(!($a == 100))
{
    say "Value is not 100";
}
else if($a == 200)
{
    say "Value is 200";
}
else
{
    say "No Match";
}
// PRINTS
// Value is not 100

Same as above but using != operator:

my $a = 200;
if($a != 100)
{
    say "Value is not 100";
}
else if($a == 200)
{
    say "Value is 200";
}
else
{
    say "No Match";
}
// PRINTS
// Value is not 100

It is also possible to not bother with the {} example:

my $a = 100;
if($a != 100)
    say "Value is 100";
else if($a == 200)
    say "Value is 200";
else
    say "No Match";
// PRINTS
// No Match
 
// Or even
my $a = 200;
if($a != 100)
    say "Value is 100";
else
    say "Value is NOT 100";
// PRINTS
// Value is 100
 
// Note this will only work for the FIRST statement and not the other so the following is invalid:
my $a = 200;
if($a == 100)
    say "Value is 100";
    //say "Value is 100"; // INVALID this will not be considered part of the IF
else
    say "Value is NOT 100";
    //say "Value is NOT 100"; // INVALID this will not be considered part of the else
// PRINTS
// Value is NOT 100

Example of using the logical ? : operator:

my $a = 100;
say $a == 100 ? "Value is 100" : "The value is NOT 100" ;
// PRINTS
// Value is 100

my $a = 101;
say $a == 100 ? "Value is 100" : "The value is NOT 100" ;
// PRINTS
// The value is NOT 100
The ? : operator is much faster than a full IF statement so if you want more speed in your programs this is a rather good choice for simple Ifs.

Can be used to set variables:

my $a = 101;
my $var = $a == 100 ? "Value is 100" : "The value is NOT 100";
say $var;
// PRINTS
// The value is NOT 100

On one line:

my $a = 10;
if($a == 10) say "It is 10";
// PRINTS
// It is 10
 
// Or even
my $a = 10;
if($a == 10) say "It is 10"; else say "It is NOT 10";
// PRINTS
// It is 10
 
// Or even
my $a = 10;
if($a == 10) say "It is 10"; else if($a == 11) say "It is 11"; else say "ELSE";
// PRINTS
// It is 10

 


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