ProtoLang.net

Reference: Is Not Equal To

☰ Hide Sidebar
TOKEN IS NOT EQUAL TO
ALIAS !=

The Is Not Equal To Comparison Operator checks whether two values are different. This operator evaluates to True if the values are not the same, and False if they are identical. It is the logical opposite of the Is Equal To operator.

Syntax

The short syntax structure uses the != symbol:

VALUE != VALUE

The long syntax structure uses the words IS NOT EQUAL TO:

VALUE IS NOT EQUAL TO VALUE

Basic Inequality Checks

Example: Comparing Integer Literals

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

Example: Comparing Float Literals

PRINTLN 3.14 != 2.71 // Displays "TRUE"
PRINTLN 2.5 != 2.5 // Displays "FALSE"
PRINTLN 1.0 != 1.5 // Displays "TRUE"

Example: Comparing String Literals

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

Example: Comparing Boolean Literals

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

Comparing Variables

Example: Integer Variable Comparison

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

Example: String Variable Comparison

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

Example: Comparing Variable to Literal

NEW STR password = "secret123"
NEW BOOL isWrong = password != "wrong_password"
PRINTLN isWrong // Displays "TRUE"

Using Is Not Equal To in Conditionals

The Is Not Equal To operator is commonly used in If statements to check when values differ.

Example: Validating Input

NEW STR username = "admin"
IF username != ""
    PRINTLN "Username provided"
ELSE
    PRINTLN "Username required"
END IF
// Displays "Username provided"

Example: Filtering Results

NEW STR status = "pending"
IF status != "complete"
    PRINTLN "Task is not finished"
ELSE
    PRINTLN "Task is complete"
END IF
// Displays "Task is not finished"

Example: Error Detection

NEW INT errorCode = 404
IF errorCode != 0
    PRINTLN "An error occurred: " .. errorCode
ELSE
    PRINTLN "Operation successful"
END IF
// Displays "An error occurred: 404"

Using Is Not Equal To in Loops

The Is Not Equal To operator can control loop continuation by checking when values remain different.

Example: Loop Until Match

NEW INT guess = 1
NEW INT target = 5
WHILE guess != target
    PRINTLN "Guess: " .. guess
    SET guess = guess + 1
END WHILE
PRINTLN "Found target: " .. target
// Displays: Guess: 1, Guess: 2, Guess: 3, Guess: 4, Found target: 5

Example: Processing Until Sentinel

NEW INT i = 1
NEW INT sentinel = 0
WHILE i != sentinel
    PRINTLN "Processing item " .. i
    SET i = i - 1
    IF i < -2
        SET i = sentinel // Stop condition
    END IF
END WHILE
PRINTLN "Done processing"

Comparing Expression Results

Example: Checking Arithmetic Results

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

Example: Checking Modulo Results

NEW INT number = 7
NEW BOOL isOdd = ( number % 2 ) != 0
PRINTLN isOdd // Displays "TRUE"

Example: Comparing Multiple Expressions

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

Combining with Logical Operators

Example: Multiple Inequality Checks

NEW INT x = 5
NEW BOOL inRange = ( x != 0 ) && ( x != 10 )
PRINTLN inRange // Displays "TRUE"

Example: Excluding Multiple Values

NEW STR grade = "B"
NEW BOOL notTopOrBottom = ( grade != "A" ) && ( grade != "F" )
PRINTLN notTopOrBottom // Displays "TRUE"

Example: Either Condition

NEW STR status = "pending"
NEW BOOL needsAction = ( status != "complete" ) || ( status != "cancelled" )
PRINTLN needsAction // Displays "TRUE"

Is Not Equal To vs Is Equal To

Is Not Equal To is the logical opposite of Is Equal To. Any comparison that is True with == will be False with !=, and vice versa.

Example: Opposite Results

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

Practical Applications

Example: Input Validation

NEW INT age = 0
IF age != 0
    PRINTLN "Valid age: " .. age
ELSE
    PRINTLN "Please enter your age"
END IF
// Displays "Please enter your age"

Example: Access Control

NEW STR role = "user"
IF role != "admin"
    PRINTLN "Access restricted"
ELSE
    PRINTLN "Access granted"
END IF
// Displays "Access restricted"

Example: Processing Non-Zero Values

NEW INT counter = 5
WHILE counter != 0
    PRINTLN "Count: " .. counter
    SET counter = counter - 1
END WHILE
PRINTLN "Finished!"
// Displays: Count: 5, Count: 4, Count: 3, Count: 2, Count: 1, Finished!

Example: Filtering Invalid Data

NEW INT i = 1
WHILE i <= 5
    IF i != 3
        PRINTLN "Processing item " .. i
    ELSE
        PRINTLN "Skipping item " .. i
    END IF
    SET i = i + 1
END WHILE
// Displays all items, skipping item 3

Case Sensitivity in String Comparisons

Example: Case Matters

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

Negating Equality vs Using Inequality

Example: Two Ways to Check Difference

NEW INT x = 5
NEW INT y = 10
// Using Is Not Equal To
NEW BOOL different1 = x != y
// Using NOT with Is Equal To ( requires parentheses in expressions )
// Note: ProtoLang doesn't have a NOT operator in Version 1.0-beta
// So != is the direct way to check inequality
PRINTLN different1 // Displays "TRUE"

Important Notes

  • Is Not Equal To ( != ) returns True when values differ, False when they match.
  • Both values must be the same Data Type for comparison.
  • String comparisons are case-sensitive.
  • The result is always a Boolean ( True or False ).
  • != is the logical opposite of ==.

Common Errors

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: Confusing with Assignment

NEW INT x = 5
// Wrong: Trying to use != in assignment
// SET x != 10 // Syntax error!
// Correct: != is for comparison only
NEW BOOL isDifferent = x != 10
PRINTLN isDifferent // Displays "TRUE"

Error: Case Sensitivity Oversight

NEW STR status = "ACTIVE"
IF status != "active" // Will be TRUE due to case difference
    PRINTLN "Status is not active"
END IF
// Displays "Status is not active" even though intent was to match
// Correct: Match the case
IF status != "ACTIVE"
    PRINTLN "Status is not active"
END IF