NouvaLang v0.1.0
GitHub

Operators & Expressions

Comprehensive guide to all operator classes in NouvaLang, complete with reference tables, execution rules, and practical examples.


1. Arithmetic Operators

Arithmetic operators compute mathematical operations between numeric types (int and float). The + operator is also overloaded to perform string concatenation.

Operator Name Description & Use Case Example Result
+ Addition / Concat Adds numbers or joins strings with other values. 10 + 5
"Nouva" + "Lang"
15
"NouvaLang"
- Subtraction / Negation Subtracts right operand from left, or inverts sign as prefix. 20 - 7
-5
13
-5
* Multiplication Multiplies two numbers. 6 * 7 42
/ Division Divides left operand by right operand. 20 / 4 5
% Modulo (Remainder) Returns remainder after integer division. Useful for parity / divisibility checks. 17 % 5 2
^ Exponentiation Raises left base to the power of the right exponent. 2 ^ 8 256
// Example Arithmetic Calculation
let radius: float = 5.0;
let area: float = 3.14159 * (radius ^ 2.0);
print("Area of circle:", area); // 78.53975

2. Comparison Operators

Comparison operators evaluate the relational condition between two values and always return a boolean (true or false). They form the backbone of conditional blocks (if) and loops (while).

Operator Name Description & Use Case Example Result
== Equal To Checks if two operands have identical value and type. 10 == 10
"a" == "b"
true
false
!= Not Equal To Checks if two operands are not equal. 5 != 8 true
> Greater Than Checks if the left value is strictly greater than the right value. 15 > 10 true
< Less Than Checks if the left value is strictly less than the right value. 4 < 9 true
>= Greater Than or Equal Checks if left value is greater than or equal to right value. 20 >= 20 true
<= Less Than or Equal Checks if left value is less than or equal to right value. 12 <= 15 true
let score: int = 85;
if score >= 60 {
    print("Passed with score:", score);
}

3. Logical Operators

Logical operators combine multiple boolean expressions. NouvaLang implements short-circuit evaluation for enhanced performance and safety.

Operator Name Description & Use Case Example Result
&& Logical AND Returns true only if both operands evaluate to truthy. If left is false, right is not evaluated. true && false
(5 > 2) && (10 > 3)
false
true
|| Logical OR Returns true if at least one operand is truthy. If left is true, right is skipped. true || false
(1 == 2) || (3 == 3)
true
true
! Logical NOT Prefix operator that inverts the truth value of an expression. !false
!(5 > 10)
true
true
let is_logged_in: bool = true;
let is_admin: bool = false;

if is_logged_in && is_admin {
    print("Admin dashboard access granted");
} else if is_logged_in && !is_admin {
    print("Standard user profile view");
}

4. Compound Assignment Operators

Compound assignment operators modify a variable in-place by performing an arithmetic operation and reassigning the computed result back to the variable.

Operator Equivalent Syntax Description & Use Case Example
+= x = x + y Adds value to variable and updates it (increments / accumulator). count += 1;
-= x = x - y Subtracts value from variable and updates it (decrements). health -= 10;
*= x = x * y Multiplies variable by factor (scaling). salary *= 1.1;
/= x = x / y Divides variable by divisor. total /= 2;
%= x = x % y Calculates modulo and assigns remainder to variable. index %= 10;
let balance: int = 1000;
balance += 250; // Deposit: 1250
balance -= 50;  // Fee: 1200
balance *= 2;   // Bonus: 2400
print("Final balance:", balance); // 2400

5. Ternary Conditional Operator (? :)

The ternary operator allows concise inline conditional evaluation without verbose if-else statement blocks:

// Syntax: condition ? consequence : alternative
let age: int = 21;
let access: str = (age >= 18) ? "Allowed" : "Blocked";
print("Access Status:", access); // "Allowed"

// Nested Ternary Expressions
let marks: int = 88;
let grade: str = (marks >= 90) ? "A+" : (marks >= 80) ? "A" : "B";
print("Assigned Grade:", grade); // "A"