Spla - IF Statement


The IF statement is similar to that of other languages. The if statement contains a logical expression using which the data is compared and a decision is made based on the result of the comparison.

Syntax

?? expression:
   statement(s)

If the boolean expression evaluates to TRUE, then the block of statement(s) inside the if statement is executed. In Spla, statements in a block are uniformly indented after the : symbol. If boolean expression evaluates to FALSE, then the first set of code after the end of block is executed.

Flow Diagram

If statement

Example

int var1 = 100;
?? var1:
   << '1 - Got a true expression value';
   << var1;

int var2 = 0;
?? var2:
   << '2 - Got a true expression value';
   << var2;
<< 'Good bye!';

Output

When the above code is executed, it produces the following result −

1 - Got a true expression value
100
Good bye!

Spla - IF...ELSE IF...ELSE Statements


An else statement can be combined with an if statement. An else statement contains a block of code that executes if the conditional expression in the if statement resolves to 0 or a FALSE value.

The else statement is an optional statement and there could be at the most only one else statement following if.

Syntax

The syntax of the if...else statement is −

?? expression:
   statement(s);

//
   statement(s);

Flow Diagram

Spla if...else statement

Example


int amount = 0;
<<'Enter amount: ';
>>amount;

?? amount<1000:
   discount = amount*0.05;
   <<'Discount'+discount;
//
   discount = amount*0.10;
   <<'Discount'+discount;
    
<<'Net payable:'+amount-discount;

Output

In the above example, discount is calculated on the input amount. Rate of discount is 5%, if the amount is less than 1000, and 10% if it is above 10000. When the above code is executed, it produces the following result −

Enter amount: 600
Discount 30.0
Net payable: 570.0

Enter amount: 1200
Discount 120.0
Net payable: 1080.0

The else if Statement

The else if statement allows you to check multiple expressions for TRUE and execute a block of code as soon as one of the conditions evaluates to TRUE.

Similar to the else, the elif statement is optional. However, unlike else, for which there can be at the most one statement, there can be an arbitrary number of elif statements following an if.

syntax

?? expression1:
   statement(s);
/? expression2:
   statement(s);
/? expression3:
   statement(s);
//
   statement(s);

Core Spla does not provide switch or case statements as in other languages, but we can use if..elif...statements to simulate switch case as follows −

Example


int amount = 0;
<<'Enter amount: ';
>>amount;

?? amount<1000:
   discount = amount*0.05;
   <<'Discount'+discount;
/? amount<5000:
   discount = amount*0.10;
   <<'Discount'+discount;
//
   discount = amount*0.15;
   <<'Discount'+discount;
    
<<'Net payable:'+amount-discount;

When the above code is executed, it produces the following result −

Enter amount: 600
Discount 30.0
Net payable: 570.0

Enter amount: 3000
Discount 300.0
Net payable: 2700.0

Enter amount: 6000
Discount 900.0
Net payable: 5100.0

Spla - Nested IF Statements


There may be a situation when you want to check for another condition after a condition resolves to true. In such a situation, you can use the nested if construct.

In a nested if construct, you can have an if...else if...else construct inside another if...else if...else construct.

Syntax

The syntax of the nested if...else if...else construct may be −

?? expression1:
   statement(s);
   ?? expression2:
      statement(s);
   /? expression3:
      statement(s);
   //
      statement(s);
/? expression4:
   statement(s);
//:
   statement(s);

Example

int num = 0;
<<'enter number: ';
>>num;

?? num%2 == 0:
   ?? num%3 == 0:
      << 'Divisible by 3 and 2';
   //
      << 'divisible by 2 not divisible by 3';
//
   ?? num%3 == 0:
      << 'divisible by 3 not divisible by 2';
   //
      <<  'not Divisible by 2 not divisible by 3';

Output

When the above code is executed, it produces the following result

enter number8
divisible by 2 not divisible by 3

enter number15
divisible by 3 not divisible by 2

enter number12
Divisible by 3 and 2

enter number5
not Divisible by 2 not divisible by 3