Operators


Operators are symbols with specified meaning, we can divide them into 5 basic groups as follows:

  1. Arithmetics Operators, used to execute arithmetic operations.
  2. Logical Operators, used to link two expression or more so that the overall value will be True or False.
  3. Comparison Operators, used to compare data values.
  4. Bitwise Operators, used for manipulating bits.
  5. Assignment Operators, used to assign a value to a variable.

You can view these from here

Conditionals


Conditionals are used to control the program workflow based on the changes that occur during the execution. You can put any number of conditionals in a single program. We can put conditionals inside each other, which is called "Nested Conditional". We can also put more than one condition inside `if` and `if else` using Logical Operators.

Let's start with `if` conditional which is used to execute specific commands if the condition is true. It is written as follows:

if (condition_expression) {
  // statements;
}

Note: condition parentheses in conditionals are not mandatory, which means we can write the following:


if condition_expression {
  // statements;
}

Where:
condition_expression: represents the conditional we want to check.
statements: If the `condition_expression` is true, these statements executed, and we should remember to put those statements inside curly brackets.
In case those statements are only one statement, we can remove the curly brackets, but if there are more than one they are mandatory.

Example:

import "Srl/Console.alusus";
use Srl;
def a: int = 3;
def b: int = 24;
def sum: int = a + b;
// print 25 if the condition is true.
if (sum == 25){
    Console.print("True");
}

if..else

if condition_expression {
  // statements1
} else {
  // statements2
}

We use it in case we want to execute some statements if the condition is false.

Example:

import "Srl/Console.alusus";
use Srl;
def a: int = 3;
def b: int = 24;
def sum: int = a + b;
// if the sum is not equal to 25 and `a` is less than or equal to `b`
if sum != 25 && a <= b  {
  Console.print("True");
}
// if the sum is equal to 25 or `a` is greater than `b`
else {
  Console.print("False");
}

else if

It has the following form:

if condition_expression1 {
  // statements1
} else if condition_expression2 {
  // statements2
}

We use it if we want to check another condition in case the first is false. The difference between this and the previous form is that executing `statements2` are done only if `condition_expression2` is true.

import "Srl/Console.alusus";
use Srl;
def a: int = 3;
def b: int = 24;
def sum: int = a + b;
// if the sum is greater than 20 or equal to 15, print True.
if sum > 20 || sum==15 {
  Console.print("True");
}
// else (if the sum is less than or equal to 20 and not equal to 15) and `a` equal to `b', print Equal.
else if a==b {
  Console.print("Equal");
}

if..else if..else

if condition_expression1 {
  // statements1
} else if condition_expression2 {
  // statements2
} else {
  // statements3
}

This form is used if we have multiple conditions.

Example:

import "Srl/Console.alusus";
use Srl;
def a: int = 3;
def b: int = 24;
def sum: int = a + b;
def mul: int = a * b;
Console.print("%d\n",sum); // 27
Console.print("%d\n",mul); // 72
// if the sum is equal to 25
if sum == 25  {
  Console.print("True");
}
// if the multiplication is greater than 25
else if mul>25 {
  Console.print("Good");
}
// else (if the sum is not equal to 25 and the multiplication is less than or equal to 25)
else {
  Console.print("False");
}

Note: `switch` statement is not supported yet, but it is possible to use `if..else..if..else` instead.

Example:
Now we will write a program that represents a simple calculator that do some operations like: multiplication, addition, subtraction, and division.

Step 1: define the local variables `n1`, `n2`, `res`, and `opt` for example. where `n1` and `n2` takes numerical values, `res` will store the result, and `opt` determines the operation to perform.
Step 2: print a sentence that tells the user to enter the required operation.
Step 3: enter the required operation.
Step 4: print a sentence that tells the user to enter the first number, and enter it. Do the same for the second number.
Step 5: perform the operation specified by `opt`.
Step 6: store the result in `res`.
Step 7: Show the result.
Step 8: end the program.

import "Srl/Console.alusus";
use Srl.Console;
def opt:char;
def n1: int[64]; // define the first number
def n2: int[64]; // define the second number
def res:float[64]; // define a variable that store the result
print ("Select an operator (+, -, *, /) to perform an operation in Alusus calculator: ");
opt=getChar();
print("Enter the first number: ");
n1 = getInt(); // enter the first number
print("Enter the second number: ");
n2 = getInt(); // enter the second number
if opt == '+' {
  res = n1 + n2; // add the two numbers.
  print ("Addition of %d and %d is: %0.1f\n", n1, n2, res);
} else if opt == '-' {
  res = n1 - n2;
  print ("Subtraction of %d and %d is: %0.1f\n", n1, n2, res);
} else if opt == '*' {
  res = n1 * n2;
  print ("Multiplication of %d and %d is: %0.1f\n", n1, n2, res);
} else if opt == '/' {
  if (n2 == 0) // if the second number is 0, enter a new value for since we cannot divide by zero.
  {
    print ("Divisor cannot be zero. Please enter another value: ");
    n2 = getInt();
    // Note: We need to loop endlessly until the user enters a non-zero value for n2, but this
    // requires the use of loops which will be explained in a following section.
  }
  res = n1 / n2; // divide two numbers
  print ("Division of %d and %d is: %0.1f\n", n1, n2, res);
} else {
  print("You have entered wrong inputs\n");
}

/*
Select an operator (+, -, *, /) to perform an operation in Alusus calculator: *
Enter the first number: 5
Enter the second number: 3
Multiplication of 5 and 3 is: 15.0
*/