ProtoLang.net

Reference: End If

☰ Hide Sidebar
TOKEN END IF
ALIAS None

The End If token is used to mark the end of an If conditional block. Every If statement must have a corresponding End If to define where the conditional structure terminates. The End If statement closes the entire If-Elif-Else chain and is required regardless of whether Elif or Else clauses are present.

Syntax

The short syntax structure uses the words END IF:

IF CONDITION
    STATEMENTS
END IF

The long syntax structure uses the words END IF:

IF CONDITION
    STATEMENTS
END IF

Basic End If Usage

End If is required to close every If statement.

Example: Simple If with End If

NEW INT age = 20
IF age >= 18
    PRINTLN "You are an adult"
END IF
PRINTLN "Program continues"
// Displays "You are an adult" and "Program continues"

Example: If-Else with End If

NEW INT score = 85
IF score >= 90
    PRINTLN "Grade: A"
ELSE
    PRINTLN "Grade: B or below"
END IF
PRINTLN "Grading complete"
// Displays "Grade: B or below" and "Grading complete"

Example: If-Elif-Else with End If

NEW FLOAT temperature = 75.0
IF temperature >= 90.0
    PRINTLN "Hot"
ELIF temperature >= 70.0
    PRINTLN "Warm"
ELSE
    PRINTLN "Cool"
END IF
PRINTLN "Temperature check complete"
// Displays "Warm" and "Temperature check complete"

Example: Multiple Statements After End If

NEW INT x = 5
IF x > 10
    PRINTLN "x is large"
END IF
PRINTLN "After conditional"
PRINTLN "Program running"
NEW INT y = 20
// All statements after END IF execute normally

End If Closes Entire If Block

End If terminates the entire conditional structure, including all If, Elif, and Else clauses.

Example: End If After Multiple Elif

NEW INT day = 3
IF day == 1
    PRINTLN "Monday"
ELIF day == 2
    PRINTLN "Tuesday"
ELIF day == 3
    PRINTLN "Wednesday"
ELIF day == 4
    PRINTLN "Thursday"
END IF
PRINTLN "Day processed"
// Displays "Wednesday" and "Day processed"

Example: End If After Long Chain

NEW INT score = 72
IF score >= 90
    PRINTLN "Grade: A"
ELIF score >= 80
    PRINTLN "Grade: B"
ELIF score >= 70
    PRINTLN "Grade: C"
ELIF score >= 60
    PRINTLN "Grade: D"
ELSE
    PRINTLN "Grade: F"
END IF
PRINTLN "Report card generated"
// Displays "Grade: C" and "Report card generated"

Example: Multiple Conditionals in Sequence

NEW INT x = 15
NEW INT y = 20
IF x > 10
    PRINTLN "x is greater than 10"
END IF
IF y > 10
    PRINTLN "y is greater than 10"
END IF
// Both conditionals are independent
// Displays both messages

End If with Nested Conditionals

Each If statement requires its own End If, including nested conditionals.

Example: Nested If with Multiple End If

NEW INT age = 25
NEW BOOL hasLicense = TRUE
IF age >= 18
    IF hasLicense == TRUE
        PRINTLN "Can drive"
    ELSE
        PRINTLN "Need license"
    END IF
ELSE
    PRINTLN "Too young"
END IF
PRINTLN "Check complete"
// Inner IF needs END IF, outer IF needs END IF

Example: Multiple Levels of Nesting

NEW INT level = 2
NEW INT points = 150
IF level >= 1
    IF level >= 2
        IF points >= 100
            PRINTLN "Advanced player"
        END IF
    END IF
END IF
PRINTLN "Player status checked"
// Three IF statements require three END IF statements

Example: Nested If-Elif-Else

NEW STR userType = "premium"
NEW INT accountAge = 15
IF userType == "premium"
    IF accountAge >= 12
        PRINTLN "Long-term premium member"
    ELIF accountAge >= 6
        PRINTLN "Established premium member"
    ELSE
        PRINTLN "New premium member"
    END IF
ELIF userType == "standard"
    PRINTLN "Standard member"
END IF
PRINTLN "Member status complete"
// Inner IF-ELIF-ELSE needs END IF
// Outer IF-ELIF needs END IF

Example: Complex Nesting

NEW INT a = 10
NEW INT b = 20
IF a > 5
    PRINTLN "a is greater than 5"
    IF b > 15
        PRINTLN "b is greater than 15"
        IF ( a + b ) > 25
            PRINTLN "Sum is greater than 25"
        END IF
    END IF
END IF
PRINTLN "All checks complete"
// Each IF requires its own END IF

End If Marks Execution Boundary

Code after End If always executes, regardless of which branch was taken in the conditional.

Example: Guaranteed Execution After End If

NEW INT value = 5
IF value > 10
    PRINTLN "Inside IF block"
ELSE
    PRINTLN "Inside ELSE block"
END IF
PRINTLN "This always executes"
// "This always executes" runs regardless of condition

Example: Variable Access After End If

NEW STR status = ""
IF TRUE
    SET status = "active"
END IF
PRINTLN "Status: " .. status
// Variables modified inside IF are accessible after END IF

Example: Sequential Logic After End If

NEW INT score = 85
NEW INT bonus = 0
IF score >= 80
    SET bonus = 10
END IF
NEW INT finalScore = score + bonus
PRINTLN "Final score: " .. finalScore
// Calculations after END IF use updated values

Example: Multiple Operations After End If

NEW INT x = 15
IF x > 10
    PRINTLN "Condition met"
END IF
SET x = x + 5
PRINTLN "x is now: " .. x
IF x > 20
    PRINTLN "x exceeds 20"
END IF
// Multiple statements and conditionals after END IF

End If in Loops

If statements inside loops require End If before the loop can continue to the next iteration.

Example: If Inside While Loop

NEW INT i = 1
WHILE i <= 5
    IF ( i % 2 ) == 0
        PRINTLN i .. " is even"
    ELSE
        PRINTLN i .. " is odd"
    END IF
    SET i = i + 1
END WHILE
// END IF closes the conditional before loop continues

Example: Multiple Conditionals in Loop

NEW INT num = 1
WHILE num <= 10
    IF num < 5
        PRINTLN num .. " is low"
    END IF
    IF num >= 5
        PRINTLN num .. " is high"
    END IF
    SET num = num + 1
END WHILE
// Each IF needs its own END IF within the loop

Example: Nested If in Loop

NEW INT i = 1
WHILE i <= 5
    IF i > 2
        IF i < 5
            PRINTLN i .. " is in range"
        END IF
    END IF
    SET i = i + 1
END WHILE
// Nested conditionals each need END IF

Example: Break After End If

NEW INT counter = 1
WHILE counter <= 10
    IF counter == 5
        PRINTLN "Stopping at 5"
        BREAK
    END IF
    PRINTLN "Count: " .. counter
    SET counter = counter + 1
END WHILE
// END IF comes before BREAK

End If Scope and Variables

Variables created or modified before End If remain accessible after End If.

Example: Variable Persists After End If

NEW STR result = ""
IF TRUE
    SET result = "completed"
END IF
PRINTLN result
// Displays "completed" - variable accessible after END IF

Example: Multiple Variable Updates

NEW INT total = 0
NEW INT count = 0
IF TRUE
    SET total = 100
    SET count = 5
END IF
NEW INT average = total / count
PRINTLN "Average: " .. average
// Variables updated in IF are used after END IF

Example: Conditional Variable Creation

NEW BOOL hasBonus = TRUE
NEW INT salary = 50000
NEW INT bonus = 0
IF hasBonus == TRUE
    SET bonus = 5000
END IF
NEW INT totalPay = salary + bonus
PRINTLN "Total compensation: " .. totalPay
// Displays "Total compensation: 55000"

Practical Applications

Example: Input Validation Complete

NEW INT age = 25
NEW BOOL validAge = FALSE
IF ( age >= 0 ) && ( age <= 120 )
    SET validAge = TRUE
    PRINTLN "Age is valid"
ELSE
    PRINTLN "Invalid age"
END IF
IF validAge == TRUE
    PRINTLN "Processing user data"
END IF
// Second conditional uses result from first

Example: Multi-Stage Processing

NEW INT score = 85
NEW STR grade = ""
IF score >= 90
    SET grade = "A"
ELIF score >= 80
    SET grade = "B"
ELIF score >= 70
    SET grade = "C"
ELSE
    SET grade = "F"
END IF
PRINTLN "Grade assigned: " .. grade
NEW BOOL passed = FALSE
IF grade != "F"
    SET passed = TRUE
END IF
IF passed == TRUE
    PRINTLN "Student passed the course"
END IF
// Multiple conditionals process sequentially

Example: Sequential Checks

NEW STR username = "admin"
NEW STR password = "pass123"
NEW BOOL authenticated = FALSE
IF username == "admin"
    PRINTLN "Username verified"
END IF
IF password == "pass123"
    PRINTLN "Password verified"
END IF
IF ( username == "admin" ) && ( password == "pass123" )
    SET authenticated = TRUE
END IF
IF authenticated == TRUE
    PRINTLN "Access granted"
END IF
// Each check completes before next begins

Example: Status Update Chain

NEW FLOAT balance = 1500.0
NEW STR accountStatus = "active"
IF balance >= 1000.0
    SET accountStatus = "premium"
    PRINTLN "Upgraded to premium"
END IF
PRINTLN "Current status: " .. accountStatus
NEW FLOAT fee = 0.0
IF accountStatus == "premium"
    SET fee = 0.0
ELSE
    SET fee = 10.0
END IF
PRINTLN "Monthly fee: $" .. fee
// Status from first IF affects second IF

Example: Error Handling Flow

NEW INT errorCode = 404
NEW STR errorMessage = ""
IF errorCode == 404
    SET errorMessage = "Not Found"
ELIF errorCode == 500
    SET errorMessage = "Server Error"
ELIF errorCode == 403
    SET errorMessage = "Forbidden"
ELSE
    SET errorMessage = "Unknown Error"
END IF
PRINTLN "Error " .. errorCode .. ": " .. errorMessage
NEW BOOL shouldRetry = FALSE
IF errorCode == 500
    SET shouldRetry = TRUE
END IF
IF shouldRetry == TRUE
    PRINTLN "Retrying request..."
END IF
// Error handling completes at END IF

Example: Configuration Builder

NEW INT userLevel = 2
NEW INT maxConnections = 10
IF userLevel >= 3
    SET maxConnections = 100
ELIF userLevel >= 2
    SET maxConnections = 50
ELIF userLevel >= 1
    SET maxConnections = 25
END IF
PRINTLN "Max connections: " .. maxConnections
NEW INT timeout = 30
IF userLevel >= 2
    SET timeout = 60
END IF
PRINTLN "Timeout: " .. timeout .. " seconds"
// Configuration built step by step

Example: Point Calculation System

NEW INT basePoints = 100
NEW INT multiplier = 1
NEW INT difficulty = 3
IF difficulty >= 3
    SET multiplier = 3
ELIF difficulty >= 2
    SET multiplier = 2
END IF
NEW INT totalPoints = basePoints * multiplier
PRINTLN "Points earned: " .. totalPoints
NEW INT bonus = 0
IF totalPoints >= 200
    SET bonus = 50
END IF
NEW INT finalPoints = totalPoints + bonus
PRINTLN "Final points: " .. finalPoints
// Points calculated across multiple conditionals

End If Position Requirements

Example: End If Must Close All Clauses

NEW INT x = 5
IF x > 10
    PRINTLN "High"
ELIF x > 5
    PRINTLN "Medium"
ELSE
    PRINTLN "Low"
END IF
// END IF must come after all IF/ELIF/ELSE clauses
PRINTLN "Conditional complete"

Example: End If Cannot Appear Early

NEW INT score = 85
IF score >= 90
    PRINTLN "A"
ELIF score >= 80
    PRINTLN "B"
END IF
// END IF closes entire IF-ELIF structure
// Cannot have more ELIF after END IF

Example: Proper If-Elif-Else-End If Order

NEW INT value = 50
IF value > 75
    PRINTLN "High"
ELIF value > 50
    PRINTLN "Medium"
ELIF value > 25
    PRINTLN "Low"
ELSE
    PRINTLN "Very Low"
END IF
// Order: IF -> ELIF( s ) -> ELSE -> END IF

End If with All Data Types

Example: Boolean Conditional

NEW BOOL isActive = TRUE
IF isActive == TRUE
    PRINTLN "System active"
END IF
PRINTLN "Status check complete"

Example: Integer Conditional

NEW INT count = 10
IF count > 5
    PRINTLN "Count exceeds threshold"
END IF
PRINTLN "Count processed"

Example: Float Conditional

NEW FLOAT temperature = 98.6
IF temperature > 98.0
    PRINTLN "Elevated temperature"
END IF
PRINTLN "Temperature reading complete"

Example: String Conditional

NEW STR command = "start"
IF command == "start"
    PRINTLN "Starting system"
END IF
PRINTLN "Command processed"

Important Notes

  • Every If statement must have exactly one End If.
  • End If must appear after all If, Elif, and Else clauses.
  • End If marks the end of the entire conditional structure.
  • Code after End If always executes regardless of which branch was taken.
  • Nested If statements each require their own End If.
  • End If has no condition or parameters.
  • Variables modified before End If remain accessible after End If.
  • Multiple If statements in sequence each need their own End If.
  • End If cannot appear before Elif or Else clauses.
  • Missing End If results in a syntax error.

Common Errors

Error: Missing End If

NEW INT x = 5
IF x > 3
    PRINTLN "Greater than 3"
// Syntax error! Missing END IF
PRINTLN "This won't compile"
// Correct:
IF x > 3
    PRINTLN "Greater than 3"
END IF
PRINTLN "Program continues"

Error: End If Without If

NEW INT x = 5
PRINTLN "Starting"
END IF // Syntax error! END IF without matching IF
// Correct:
IF x > 3
    PRINTLN "Greater than 3"
END IF

Error: Too Many End If

NEW INT x = 5
IF x > 3
    PRINTLN "Greater than 3"
END IF
END IF // Syntax error! Extra END IF
// Correct:
IF x > 3
    PRINTLN "Greater than 3"
END IF

Error: End If Before Else

NEW INT x = 5
IF x > 10
    PRINTLN "High"
END IF // Syntax error! END IF too early
ELSE
    PRINTLN "Low"
END IF
// Correct:
IF x > 10
    PRINTLN "High"
ELSE
    PRINTLN "Low"
END IF

Error: End If Before Elif

NEW INT score = 85
IF score >= 90
    PRINTLN "A"
END IF // Syntax error! END IF too early
ELIF score >= 80
    PRINTLN "B"
END IF
// Correct:
IF score >= 90
    PRINTLN "A"
ELIF score >= 80
    PRINTLN "B"
END IF

Error: Missing End If in Nested Structure

NEW INT x = 10
NEW INT y = 20
IF x > 5
    IF y > 15
        PRINTLN "Both conditions met"
    // Missing END IF for inner IF
END IF
// Syntax error! Inner IF needs END IF
// Correct:
IF x > 5
    IF y > 15
        PRINTLN "Both conditions met"
    END IF
END IF

Error: End If in Wrong Position

NEW INT x = 5
IF x > 10
    PRINTLN "High"
    END IF // Syntax error! END IF indented too far
// Correct:
IF x > 10
    PRINTLN "High"
END IF

Error: Mismatched Nested End If

NEW INT a = 5
NEW INT b = 10
IF a > 0
    IF b > 5
        PRINTLN "Both positive"
END IF
    END IF
// Syntax error! END IF order is wrong
// Correct:
IF a > 0
    IF b > 5
        PRINTLN "Both positive"
    END IF
END IF

Error: End If After Loop Close

NEW INT i = 1
WHILE i <= 5
    IF i > 3
        PRINTLN i
END WHILE
    END IF
// Syntax error! END IF should be before END WHILE
// Correct:
WHILE i <= 5
    IF i > 3
        PRINTLN i
    END IF
    SET i = i + 1
END WHILE

Error: End If with Condition

NEW INT x = 5
IF x > 3
    PRINTLN "Greater than 3"
END IF x > 3 // Syntax error! END IF takes no parameters
// Correct:
IF x > 3
    PRINTLN "Greater than 3"
END IF

Error: Mixing Up End Statements

NEW INT i = 1
WHILE i <= 5
    IF i > 3
        PRINTLN i
    END WHILE // Syntax error! Should be END IF
    SET i = i + 1
END IF // Syntax error! Should be END WHILE
// Correct:
WHILE i <= 5
    IF i > 3
        PRINTLN i
    END IF
    SET i = i + 1
END WHILE

Error: Attempting to Continue After Missing End If

NEW INT x = 5
IF x > 3
    PRINTLN "Greater than 3"
// Missing END IF
NEW INT y = 10 // This won't execute - syntax error above
// Correct:
IF x > 3
    PRINTLN "Greater than 3"
END IF
NEW INT y = 10