ProtoLang.net

Reference: And

☰ Hide Sidebar
TOKEN AND
ALIAS &&

The And Logical Operator is used to combine two Boolean expressions together. The And operator evaluates to True only if both the left side and the right side are True. If either side is False, or if both sides are False, the result is False.

Syntax

The short syntax structure uses the && symbol:

VALUE && VALUE

The long syntax structure uses the word AND:

VALUE AND VALUE

Truth Table

The And operator follows this logic:

Example: All Possible Combinations

PRINTLN TRUE && TRUE // Displays "TRUE"
PRINTLN TRUE && FALSE // Displays "FALSE"
PRINTLN FALSE && TRUE // Displays "FALSE"
PRINTLN FALSE && FALSE // Displays "FALSE"

Basic And Operations

Example: Combining Boolean Literals

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

Example: Combining Boolean Variables

NEW BOOL hasTicket = TRUE
NEW BOOL hasID = TRUE
NEW BOOL canEnter = hasTicket && hasID
PRINTLN canEnter // Displays "TRUE"

Example: One False Makes Result False

NEW BOOL isStudent = TRUE
NEW BOOL hasDiscount = FALSE
NEW BOOL getsDiscount = isStudent && hasDiscount
PRINTLN getsDiscount // Displays "FALSE"

Combining Comparison Expressions

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

Example: Range Checking

NEW INT age = 25
NEW BOOL inRange = ( age >= 18 ) && ( age <= 65 )
PRINTLN inRange // Displays "TRUE"

Example: Multiple Conditions

NEW INT score = 85
NEW INT attendance = 95
NEW BOOL passed = ( score >= 60 ) && ( attendance >= 80 )
PRINTLN passed // Displays "TRUE"

Example: Validating Multiple Requirements

NEW FLOAT temperature = 72.5
NEW FLOAT humidity = 45.0
NEW BOOL isComfortable = ( temperature >= 68.0 ) && ( temperature <= 78.0 ) && ( humidity <= 60.0 )
PRINTLN isComfortable // Displays "TRUE"

Using And in Conditionals

The And operator is frequently used in If statements to check multiple conditions.

Example: Access Control

NEW INT age = 21
NEW BOOL hasID = TRUE
IF ( age >= 18 ) && hasID
    PRINTLN "Access granted"
ELSE
    PRINTLN "Access denied"
END IF
// Displays "Access granted"

Example: Eligibility Check

NEW INT credits = 120
NEW FLOAT gpa = 3.5
IF ( credits >= 120 ) && ( gpa >= 2.0 )
    PRINTLN "Eligible for graduation"
ELSE
    PRINTLN "Requirements not met"
END IF
// Displays "Eligible for graduation"

Example: Multiple Validation

NEW STR username = "admin"
NEW STR password = "secure123"
NEW BOOL isLoggedIn = FALSE
IF ( username == "admin" ) && ( password == "secure123" )
    SET isLoggedIn = TRUE
    PRINTLN "Login successful"
ELSE
    PRINTLN "Invalid credentials"
END IF
// Displays "Login successful"

Using And in Loops

The And operator can control loop continuation by requiring multiple conditions to be True.

Example: Loop with Multiple Conditions

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

Example: Bounded Loop with Status Check

NEW INT i = 1
NEW BOOL hasError = FALSE
WHILE ( i <= 10 ) && ( hasError == FALSE )
    PRINTLN "Processing item " .. i
    SET i = i + 1
    IF i == 6
        SET hasError = TRUE
    END IF
END WHILE
PRINTLN "Loop ended"

Chaining Multiple And Operators

You can chain multiple And operators to require all conditions to be True.

Example: Three Conditions

NEW BOOL condition1 = TRUE
NEW BOOL condition2 = TRUE
NEW BOOL condition3 = TRUE
NEW BOOL allTrue = condition1 && condition2 && condition3
PRINTLN allTrue // Displays "TRUE"

Example: Multiple Requirements

NEW INT age = 25
NEW BOOL hasLicense = TRUE
NEW BOOL hasInsurance = TRUE
NEW BOOL canDrive = ( age >= 16 ) && hasLicense && hasInsurance
PRINTLN canDrive // Displays "TRUE"

Example: All Conditions Must Pass

NEW INT score1 = 85
NEW INT score2 = 90
NEW INT score3 = 78
NEW BOOL allPassing = ( score1 >= 60 ) && ( score2 >= 60 ) && ( score3 >= 60 )
PRINTLN allPassing // Displays "TRUE"

Practical Applications

Example: Date Validation

NEW INT day = 15
NEW INT month = 6
NEW INT year = 2026
NEW BOOL isValidDate = ( day >= 1 ) && ( day <= 31 ) && ( month >= 1 ) && ( month <= 12 ) && ( year > 0 )
IF isValidDate == TRUE
    PRINTLN "Valid date"
ELSE
    PRINTLN "Invalid date"
END IF
// Displays "Valid date"

Example: Password Strength Check

NEW INT passwordLength = 12
NEW BOOL hasUppercase = TRUE
NEW BOOL hasNumber = TRUE
NEW BOOL isStrong = ( passwordLength >= 8 ) && hasUppercase && hasNumber
IF isStrong == TRUE
    PRINTLN "Password is strong"
ELSE
    PRINTLN "Password is weak"
END IF
// Displays "Password is strong"

Example: Discount Eligibility

NEW BOOL isMember = TRUE
NEW FLOAT purchaseAmount = 150.0
NEW BOOL hasCode = TRUE
NEW BOOL getsDiscount = isMember && ( purchaseAmount >= 100.0 ) && hasCode
IF getsDiscount == TRUE
    PRINTLN "Discount applied!"
ELSE
    PRINTLN "No discount available"
END IF
// Displays "Discount applied!"

Example: Game Win Condition

NEW BOOL hasKey = TRUE
NEW BOOL hasMap = TRUE
NEW BOOL reachedExit = TRUE
NEW BOOL wins = hasKey && hasMap && reachedExit
IF wins == TRUE
    PRINTLN "You win!"
ELSE
    PRINTLN "Keep playing"
END IF
// Displays "You win!"

And vs Or

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

Example: And vs Or Comparison

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

Example: Different Results

NEW BOOL hasTicket = TRUE
NEW BOOL hasInvite = FALSE
NEW BOOL canEnterWithAnd = hasTicket && hasInvite
NEW BOOL canEnterWithOr = hasTicket || hasInvite
PRINTLN canEnterWithAnd // Displays "FALSE"
PRINTLN canEnterWithOr // Displays "TRUE"

Combining And with Or

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

Example: Complex Logic

NEW BOOL isStudent = TRUE
NEW BOOL isSenior = FALSE
NEW BOOL hasCoupon = TRUE
NEW BOOL getsDiscount = ( isStudent || isSenior ) && hasCoupon
PRINTLN getsDiscount // Displays "TRUE"

Example: Multiple Groups

NEW INT age = 25
NEW BOOL isVIP = FALSE
NEW BOOL hasMembership = TRUE
NEW BOOL hasAccess = ( age >= 18 ) && ( isVIP || hasMembership )
PRINTLN hasAccess // Displays "TRUE"

Short-Circuit Evaluation

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

Example: Understanding Short-Circuit

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

Common Patterns

Example: Boundary Checking

NEW INT value = 50
NEW INT min = 1
NEW INT max = 100
NEW BOOL inBounds = ( value >= min ) && ( value <= max )
PRINTLN inBounds // Displays "TRUE"

Example: All Requirements Met

NEW BOOL task1 = TRUE
NEW BOOL task2 = TRUE
NEW BOOL task3 = TRUE
NEW BOOL allComplete = task1 && task2 && task3
IF allComplete == TRUE
    PRINTLN "All tasks complete!"
ELSE
    PRINTLN "Some tasks remaining"
END IF
// Displays "All tasks complete!"

Example: Safety Check

NEW BOOL engineOn = TRUE
NEW INT fuelLevel = 50
NEW INT oilPressure = 30
NEW BOOL safeToOperate = engineOn && ( fuelLevel > 10 ) && ( oilPressure > 20 )
IF safeToOperate == TRUE
    PRINTLN "Safe to operate"
ELSE
    PRINTLN "Safety check failed"
END IF
// Displays "Safe to operate"

Important Notes

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

Common Errors

Error: Using And with Non-Boolean Values

NEW INT x = 5
NEW INT y = 10
PRINTLN x && y // Type error! And requires Boolean values

Error: Forgetting Comparison Operators

NEW INT age = 25
// Wrong: Missing comparison
// IF age && hasID // Syntax error!
// Correct: Use comparison operators
NEW BOOL hasID = TRUE
IF ( age >= 18 ) && hasID
    PRINTLN "Valid"
END IF

Error: Confusing And with Or

NEW BOOL hasTicket = TRUE
NEW BOOL hasInvite = FALSE
// And requires both (wrong if you only need one)
IF hasTicket && hasInvite
    PRINTLN "Can enter"
END IF
// Or requires at least one (correct for this scenario)
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"