ProtoLang.net

Reference: Is Equal To

☰ Hide Sidebar
TOKEN IS EQUAL TO
ALIAS ==

The Is Equal To Comparison Operator checks whether two values are exactly the same. This operator evaluates to True if both values are identical, and False if they are different. It is one of the most commonly used comparison operators and is essential for checking equality in conditions.

Syntax

The short syntax structure uses the == symbol:

VALUE == VALUE

The long syntax structure uses the words IS EQUAL TO:

VALUE IS EQUAL TO VALUE

Basic Equality Checks

Example: Comparing Integer Literals

PRINTLN 5 == 5 // Displays "TRUE"
PRINTLN 5 == 10 // Displays "FALSE"
PRINTLN 0 == 0 // Displays "TRUE"

Example: Comparing Float Literals

PRINTLN 3.14 == 3.14 // Displays "TRUE"
PRINTLN 2.5 == 2.0 // Displays "FALSE"
PRINTLN 1.0 == 1.0 // Displays "TRUE"

Example: Comparing String Literals

PRINTLN "Hello" == "Hello" // Displays "TRUE"
PRINTLN "Hello" == "World" // Displays "FALSE"
PRINTLN "abc" == "abc" // Displays "TRUE"

Example: Comparing Boolean Literals

PRINTLN TRUE == TRUE // Displays "TRUE"
PRINTLN TRUE == FALSE // Displays "FALSE"
PRINTLN FALSE == FALSE // Displays "TRUE"

Comparing Variables

Example: Integer Variable Comparison

NEW INT x = 10
NEW INT y = 10
NEW INT z = 20
PRINTLN x == y // Displays "TRUE"
PRINTLN x == z // Displays "FALSE"

Example: String Variable Comparison

NEW STR name1 = "Alice"
NEW STR name2 = "Alice"
NEW STR name3 = "Bob"
NEW BOOL areSame = name1 == name2
NEW BOOL areDifferent = name1 == name3
PRINTLN areSame // Displays "TRUE"
PRINTLN areDifferent // Displays "FALSE"

Example: Comparing Variable to Literal

NEW STR password = "secret123"
NEW BOOL isCorrect = password == "secret123"
PRINTLN isCorrect // Displays "TRUE"

Using Is Equal To in Conditionals

The Is Equal To operator is commonly used in If statements to check for specific values.

Example: Checking User Input

NEW STR answer = "yes"
IF answer == "yes"
    PRINTLN "You agreed!"
ELSE
    PRINTLN "You declined."
END IF
// Displays "You agreed!"

Example: Multiple Equality Checks

NEW STR grade = "A"
IF grade == "A"
    PRINTLN "Excellent!"
ELIF grade == "B"
    PRINTLN "Good job!"
ELIF grade == "C"
    PRINTLN "Satisfactory"
ELSE
    PRINTLN "Needs improvement"
END IF
// Displays "Excellent!"

Example: Validating Numeric Input

NEW INT secretNumber = 42
NEW INT guess = 42
IF guess == secretNumber
    PRINTLN "Correct!"
ELSE
    PRINTLN "Try again"
END IF
// Displays "Correct!"

Using Is Equal To in Loops

The Is Equal To operator can control loop behavior by checking for specific conditions.

Example: Stopping at Target Value

NEW INT counter = 1
WHILE counter <= 10
    IF counter == 5
        PRINTLN "Found 5!"
    END IF
    SET counter = counter + 1
END WHILE

Example: Breaking on Match

NEW INT i = 1
NEW INT target = 7
WHILE i <= 10
    PRINTLN "Checking: " .. i
    IF i == target
        PRINTLN "Target found!"
        BREAK
    END IF
    SET i = i + 1
END WHILE

Comparing Expression Results

Example: Checking Arithmetic Results

NEW INT x = 10
NEW INT y = 5
NEW BOOL result = ( x + y ) == 15
PRINTLN result // Displays "TRUE"

Example: Checking Modulo Results

NEW INT number = 8
NEW BOOL isEven = ( number % 2 ) == 0
PRINTLN isEven // Displays "TRUE"

Example: Comparing Multiple Expressions

NEW INT a = 5
NEW INT b = 3
NEW INT c = 2
NEW BOOL balanced = ( a + b ) == ( c * 4 )
PRINTLN balanced // Displays "TRUE" ( 8 == 8 )

Combining with Logical Operators

Example: Multiple Equality Checks

NEW STR username = "admin"
NEW STR password = "pass123"
NEW BOOL validUser = ( username == "admin" ) && ( password == "pass123" )
PRINTLN validUser // Displays "TRUE"

Example: Either/Or Equality

NEW STR color = "red"
NEW BOOL isPrimary = ( color == "red" ) || ( color == "blue" ) || ( color == "yellow" )
PRINTLN isPrimary // Displays "TRUE"

Is Equal To vs Equal To

It's critical to distinguish between the comparison operator ( ==, IS EQUAL TO ) and the assignment operator ( =, EQUAL TO ).

Example: Assignment vs Comparison

NEW INT x = 5 // EQUAL TO ( = ) assigns 5 to x
NEW BOOL result = x == 5 // IS EQUAL TO ( == ) checks if x equals 5
PRINTLN x // Displays "5"
PRINTLN result // Displays "TRUE"

Example: Common Mistake

NEW INT age = 18
// Wrong: Using assignment in condition
// IF age = 18 // Syntax error!
// Correct: Using comparison in condition
IF age == 18
    PRINTLN "Exactly 18!"
END IF

Practical Applications

Example: Menu Selection

NEW INT choice = 2
IF choice == 1
    PRINTLN "Starting new game"
ELIF choice == 2
    PRINTLN "Loading saved game"
ELIF choice == 3
    PRINTLN "Exiting"
END IF
// Displays "Loading saved game"

Example: Status Checking

NEW STR status = "active"
IF status == "active"
    PRINTLN "System is running"
ELIF status == "paused"
    PRINTLN "System is paused"
ELIF status == "stopped"
    PRINTLN "System is stopped"
END IF
// Displays "System is running"

Example: Finding in Sequence

NEW INT i = 1
NEW BOOL found = FALSE
WHILE i <= 10
    IF i == 7
        SET found = TRUE
        BREAK
    END IF
    SET i = i + 1
END WHILE
IF found == TRUE
    PRINTLN "Number 7 was found!"
END IF

Case Sensitivity in String Comparisons

Example: Case Matters

NEW STR word1 = "Hello"
NEW STR word2 = "hello"
PRINTLN word1 == word2 // Displays "FALSE" ( case matters! )
PRINTLN word1 == "Hello" // Displays "TRUE"

Important Notes

  • IS EQUAL TO ( == ) checks equality; EQUAL TO ( = ) assigns values.
  • Both values must be the same Data Type for comparison.
  • String comparisons are case-sensitive.
  • The result is always a Boolean ( TRUE or FALSE ).
  • Use parentheses for clarity when comparing expressions.

Common Errors

Error: Using Single Equals in Comparison

NEW INT x = 5
IF x = 5 // Syntax error! Use == for comparison
    PRINTLN "x is 5"
END IF
// Correct:
IF x == 5
    PRINTLN "x is 5"
END IF

Error: Comparing Different Data Types

NEW INT x = 5
NEW STR y = "5"
PRINTLN x == y // Type error! Cannot compare Integer and String

Error: Comparing Integer and Float

NEW INT x = 5
NEW FLOAT y = 5.0
PRINTLN x == y // Type error! Cannot compare Integer and Float

Error: Case Sensitivity Mistake

NEW STR answer = "YES"
IF answer == "yes" // Will be FALSE due to case difference
    PRINTLN "Confirmed"
END IF
// Correct: Match the case
IF answer == "YES"
    PRINTLN "Confirmed"
END IF