ProtoLang.net

Reference: Or

☰ Hide Sidebar
TOKEN OR
ALIAS ||

The Or Logical Operator is used to combine two Boolean expressions together. The Or operator evaluates to True if at least one side is True. It only evaluates to False when both the left side and the right side are False.

Syntax

The short syntax structure uses the || symbol:

VALUE || VALUE

The long syntax structure uses the word OR:

VALUE OR VALUE

Truth Table

The Or operator follows this logic:

Example: All Possible Combinations

PRINTLN TRUE || TRUE // Displays "TRUE"
PRINTLN TRUE || FALSE // Displays "TRUE"
PRINTLN FALSE || TRUE // Displays "TRUE"
PRINTLN FALSE || FALSE // Displays "FALSE"

Basic Or Operations

Example: Combining Boolean Literals

NEW BOOL result1 = TRUE || FALSE
NEW BOOL result2 = FALSE || TRUE
NEW BOOL result3 = FALSE || FALSE
PRINTLN result1 // Displays "TRUE"
PRINTLN result2 // Displays "TRUE"
PRINTLN result3 // Displays "FALSE"

Example: Combining Boolean Variables

NEW BOOL hasTicket = TRUE
NEW BOOL hasInvite = FALSE
NEW BOOL canEnter = hasTicket || hasInvite
PRINTLN canEnter // Displays "TRUE"

Example: One True Makes Result True

NEW BOOL isWeekend = FALSE
NEW BOOL isHoliday = TRUE
NEW BOOL isDayOff = isWeekend || isHoliday
PRINTLN isDayOff // Displays "TRUE"

Combining Comparison Expressions

The Or operator is commonly used to combine multiple Comparison Operators.

Example: Multiple Valid Options

NEW STR grade = "A"
NEW BOOL isTopGrade = ( grade == "A") || ( grade == "B" )
PRINTLN isTopGrade // Displays "TRUE"

Example: Out of Range Check

NEW INT value = 150
NEW BOOL outOfRange = ( value < 0 ) || ( value > 100 )
PRINTLN outOfRange // Displays "TRUE"

Example: Multiple Passing Conditions

NEW INT score = 55
NEW BOOL hasExtraCredit = TRUE
NEW BOOL passes = ( score >= 60 ) || hasExtraCredit
PRINTLN passes // Displays "TRUE"

Using Or in Conditionals

The Or operator is frequently used in If statements to check if any condition is met.

Example: Access with Multiple Options

NEW BOOL isAdmin = FALSE
NEW BOOL isModerator = TRUE
IF isAdmin || isModerator
    PRINTLN "Access granted"
ELSE
    PRINTLN "Access denied"
END IF
// Displays "Access granted"

Example: Multiple Valid Inputs

NEW STR answer = "yes"
IF ( answer == "yes" ) || ( answer == "y" ) || ( answer == "YES" )
    PRINTLN "Confirmed"
ELSE
    PRINTLN "Not confirmed"
END IF
// Displays "Confirmed"

Example: Error Detection

NEW INT statusCode = 404
IF ( statusCode == 404 ) || ( statusCode == 500 ) || ( statusCode == 503 )
    PRINTLN "Error occurred"
ELSE
    PRINTLN "Request successful"
END IF
// Displays "Error occurred"

Using Or in Loops

The Or operator can control loop continuation by allowing any condition to keep the loop running.

Example: Loop with Multiple Exit Conditions

NEW INT counter = 1
NEW BOOL maxReached = FALSE
WHILE ( counter <= 10 ) || ( maxReached == FALSE )
    PRINTLN "Iteration: " .. counter
    SET counter = counter + 1
    IF counter > 5
        SET maxReached = TRUE
        BREAK
    END IF
END WHILE
// Displays: Iteration: 1, Iteration: 2, Iteration: 3, Iteration: 4, Iteration: 5

Example: Continue Until Any Condition Met

NEW INT i = 1
NEW BOOL found = FALSE
WHILE ( i <= 10 ) || found
    IF i == 7
        SET found = TRUE
        PRINTLN "Found target at: " .. i
        BREAK
    END IF
    SET i = i + 1
END WHILE

Chaining Multiple Or Operators

You can chain multiple Or operators to check if any condition is True.

Example: Multiple Color Options

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

Example: Multiple Valid Statuses

NEW STR status = "pending"
NEW BOOL isActive = ( status == "active" ) || ( status == "pending" ) || ( status == "processing" )
PRINTLN isActive // Displays "TRUE"

Example: Any Condition Triggers Action

NEW INT temp = 105
NEW INT pressure = 80
NEW INT vibration = 30
NEW BOOL alertNeeded = ( temp > 100 ) || ( pressure > 90 ) || ( vibration > 50 )
IF alertNeeded == TRUE
    PRINTLN "System alert triggered"
ELSE
    PRINTLN "All systems normal"
END IF
// Displays "System alert triggered"

Practical Applications

Example: Weekend or Holiday Check

NEW STR dayOfWeek = "Saturday"
NEW BOOL isHoliday = FALSE
NEW BOOL isDayOff = ( dayOfWeek == "Saturday" ) || ( dayOfWeek == "Sunday" ) || isHoliday
IF isDayOff == TRUE
    PRINTLN "No work today!"
ELSE
    PRINTLN "Work day"
END IF
// Displays "No work today!"

Example: Multiple Payment Methods

NEW BOOL hasCash = FALSE
NEW BOOL hasCard = TRUE
NEW BOOL hasDigitalWallet = FALSE
NEW BOOL canPay = hasCash || hasCard || hasDigitalWallet
IF canPay == TRUE
    PRINTLN "Payment method available"
ELSE
    PRINTLN "No payment method"
END IF
// Displays "Payment method available"

Example: Discount Eligibility

NEW BOOL isStudent = TRUE
NEW BOOL isSenior = FALSE
NEW BOOL isMilitary = FALSE
NEW BOOL getsDiscount = isStudent || isSenior || isMilitary
IF getsDiscount == TRUE
    PRINTLN "Discount applied"
ELSE
    PRINTLN "Regular price"
END IF
// Displays "Discount applied"

Example: Emergency Conditions

NEW BOOL fireDanger = FALSE
NEW BOOL floodWarning = TRUE
NEW BOOL hurricaneAlert = FALSE
NEW BOOL evacuate = fireDanger || floodWarning || hurricaneAlert
IF evacuate == TRUE
    PRINTLN "Emergency evacuation required"
ELSE
    PRINTLN "No evacuation needed"
END IF
// Displays "Emergency evacuation required"

Or vs And

The Or operator requires at least one condition to be True, while the And operator requires all conditions to be True.

Example: Or vs And Comparison

NEW BOOL a = TRUE
NEW BOOL b = FALSE
PRINTLN a || b // Displays "TRUE" (at least one is TRUE)
PRINTLN a && b // Displays "FALSE" (both must be TRUE)

Example: Different Requirements

NEW BOOL hasTicket = TRUE
NEW BOOL hasInvite = FALSE
NEW BOOL canEnterOr = hasTicket || hasInvite
NEW BOOL canEnterAnd = hasTicket && hasInvite
PRINTLN canEnterOr // Displays "TRUE" (either works)
PRINTLN canEnterAnd // Displays "FALSE" (needs both)

Combining Or with And

You can combine Or and And operators to create complex logical expressions. Use parentheses to control the order of evaluation.

Example: Complex Eligibility

NEW INT age = 25
NEW BOOL hasLicense = TRUE
NEW BOOL hasPermit = FALSE
NEW BOOL canDrive = ( age >= 18 ) && ( hasLicense || hasPermit )
PRINTLN canDrive // Displays "TRUE"

Example: Nested Conditions

NEW BOOL isMember = FALSE
NEW BOOL isVIP = TRUE
NEW FLOAT purchaseAmount = 50.0
NEW BOOL getsDiscount = ( isMember || isVIP ) && ( purchaseAmount >= 25.0 )
PRINTLN getsDiscount // Displays "TRUE"

Example: Multiple Groups

NEW BOOL hasKeyA = TRUE
NEW BOOL hasKeyB = FALSE
NEW BOOL hasKeyC = FALSE
NEW BOOL unlocked = ( hasKeyA || hasKeyB ) && ( hasKeyA || hasKeyC )
PRINTLN unlocked // Displays "TRUE"

Short-Circuit Evaluation

When using Or, if the left side is True, the result is always True regardless of the right side. This is called short-circuit evaluation.

Example: Understanding Short-Circuit

NEW BOOL checkFirst = TRUE
NEW BOOL checkSecond = FALSE
NEW BOOL result = checkFirst || checkSecond
PRINTLN result // Displays "TRUE" (first condition passed, second doesn't matter)

Common Patterns

Example: Valid Input Check

NEW STR input = "y"
NEW BOOL isYes = ( input == "yes" ) || ( input == "y" ) || ( input == "Y" ) || ( input == "YES" )
PRINTLN isYes // Displays "TRUE"

Example: Any Task Incomplete

NEW BOOL task1 = TRUE
NEW BOOL task2 = FALSE
NEW BOOL task3 = TRUE
NEW BOOL anyIncomplete = ( task1 == FALSE ) || ( task2 == FALSE ) || ( task3 == FALSE )
IF anyIncomplete == TRUE
    PRINTLN "Some tasks incomplete"
ELSE
    PRINTLN "All tasks complete"
END IF
// Displays "Some tasks incomplete"

Example: Alternative Paths

NEW BOOL route1Open = FALSE
NEW BOOL route2Open = TRUE
NEW BOOL route3Open = FALSE
NEW BOOL canProceed = route1Open || route2Open || route3Open
IF canProceed == TRUE
    PRINTLN "Path available"
ELSE
    PRINTLN "All routes blocked"
END IF
// Displays "Path available"

Both False Equals False

Example: No Options Available

NEW BOOL option1 = FALSE
NEW BOOL option2 = FALSE
NEW BOOL hasOption = option1 || option2
PRINTLN hasOption // Displays "FALSE"

Example: All Conditions Failed

NEW BOOL qualified1 = FALSE
NEW BOOL qualified2 = FALSE
NEW BOOL qualified3 = FALSE
NEW BOOL isQualified = qualified1 || qualified2 || qualified3
IF isQualified == TRUE
    PRINTLN "Qualified"
ELSE
    PRINTLN "Not qualified"
END IF
// Displays "Not qualified"

Important Notes

  • Or returns True if at least one side is True.
  • Or only returns False when both sides are False.
  • Both operands must be Boolean values or expressions that evaluate to Boolean.
  • Use parentheses to make complex conditions clear.
  • Or requires at least one condition; And requires all conditions.

Common Errors

Error: Using Or with Non-Boolean Values

NEW INT x = 5
NEW INT y = 10
PRINTLN x || y // Type error! Or requires Boolean values

Error: Forgetting Comparison Operators

NEW STR status = "active"
// Wrong: Missing comparison
// IF status || "pending" // Syntax error!
// Correct: Use comparison operators
IF ( status == "active" ) || ( status == "pending" )
    PRINTLN "Valid status"
END IF

Error: Confusing Or with And

NEW BOOL hasTicket = TRUE
NEW BOOL hasInvite = FALSE
// Or allows either (correct if you only need one)
IF hasTicket || hasInvite
    PRINTLN "Can enter"
END IF
// And requires both (wrong if you only need one)
IF hasTicket && hasInvite
    PRINTLN "Can enter"
END IF
// Displays "Can enter"

Error: Missing Parentheses in Complex Expressions

NEW BOOL a = TRUE
NEW BOOL b = FALSE
NEW BOOL c = TRUE
// Ambiguous without parentheses
NEW BOOL result1 = a && b || c
// Clear with parentheses
NEW BOOL result2 = ( a && b ) || c
NEW BOOL result3 = a && ( b || c )
PRINTLN result2 // Displays "TRUE"
PRINTLN result3 // Displays "TRUE"