The Break token is used to immediately exit a loop, regardless of the loop's condition. When Break executes, control jumps to the first statement after the loop's End While. Break is essential for exiting loops early when a specific condition is met, without waiting for the normal loop termination condition.
The short syntax structure uses the word BREAK:
WHILE CONDITION
STATEMENTS
BREAK
STATEMENTS
END WHILEThe long syntax structure uses the word BREAK:
WHILE CONDITION
STATEMENTS
BREAK
STATEMENTS
END WHILEBreak provides an immediate exit mechanism for loops:
NEW INT i = 1
WHILE i <= 10
PRINTLN "Before break: " .. i
IF i == 3
BREAK
END IF
PRINTLN "After condition: " .. i
SET i = i + 1
END WHILE
PRINTLN "Loop exited"
// Displays: Before break: 1, After condition: 1, Before break: 2, After condition: 2, Before break: 3, Loop exitedNEW INT counter = 1
WHILE counter <= 10
IF counter == 5
PRINTLN "Breaking at " .. counter
BREAK
END IF
PRINTLN counter
SET counter = counter + 1
END WHILE
PRINTLN "Done"
// Displays: 1, 2, 3, 4, Breaking at 5, DoneNEW INT value = 0
WHILE TRUE
SET value = value + 10
PRINTLN "Value: " .. value
IF value >= 50
BREAK
END IF
END WHILE
PRINTLN "Final value: " .. value
// Displays: Value: 10, Value: 20, Value: 30, Value: 40, Value: 50, Final value: 50NEW INT i = 1
WHILE i <= 100
PRINTLN i
IF i == 5
BREAK // Exits early, doesn't reach 100
END IF
SET i = i + 1
END WHILE
PRINTLN "Stopped at " .. i
// Displays: 1, 2, 3, 4, 5, Stopped at 5Break is commonly used to exit loops once a target value is found.
NEW INT i = 1
NEW INT target = 7
NEW BOOL found = FALSE
WHILE i <= 10
IF i == target
PRINTLN "Found target: " .. target
SET found = TRUE
BREAK
END IF
SET i = i + 1
END WHILE
IF found == FALSE
PRINTLN "Target not found"
END IF
// Displays: Found target: 7NEW INT num = 2
WHILE num <= 100
IF num % 17 == 0
PRINTLN "First multiple of 17: " .. num
BREAK
END IF
SET num = num + 1
END WHILE
// Displays: First multiple of 17: 17NEW INT i = 1
NEW STR current = ""
WHILE i <= 5
IF i == 1
SET current = "apple"
ELIF i == 2
SET current = "banana"
ELIF i == 3
SET current = "cherry"
ELIF i == 4
SET current = "date"
ELIF i == 5
SET current = "elderberry"
END IF
IF current == "cherry"
PRINTLN "Found: " .. current
BREAK
END IF
SET i = i + 1
END WHILE
// Displays: Found: cherryNEW INT i = 1
NEW INT searchFor = 42
WHILE i <= 100
IF i == searchFor
PRINTLN "Found " .. searchFor .. " at position " .. i
BREAK
END IF
PRINTLN "Checking position " .. i
SET i = i + 1
END WHILEBreak is useful for stopping loops when a threshold or limit is reached.
NEW INT value = 1
NEW INT max = 50
WHILE TRUE
SET value = value * 2
PRINTLN value
IF value > max
PRINTLN "Exceeded maximum of " .. max
BREAK
END IF
END WHILE
// Displays: 2, 4, 8, 16, 32, 64, Exceeded maximum of 50NEW FLOAT spent = 0.0
NEW FLOAT budget = 100.0
NEW INT item = 1
NEW FLOAT itemCost = 15.0
WHILE item <= 20
SET spent = spent + itemCost
PRINTLN "Item " .. item .. " - Total spent: $" .. spent
IF spent > budget
PRINTLN "Budget exceeded!"
BREAK
END IF
SET item = item + 1
END WHILE
PRINTLN "Purchased " .. item .. " items"NEW INT temp = 70
NEW INT dangerLevel = 100
NEW INT minute = 0
WHILE minute < 60
SET temp = temp + 5
SET minute = minute + 1
PRINTLN "Minute " .. minute .. ": " .. temp .. "F"
IF temp >= dangerLevel
PRINTLN "DANGER! Temperature too high!"
BREAK
END IF
END WHILENEW INT attendees = 0
NEW INT capacity = 50
NEW INT ticket = 1
WHILE ticket <= 100
SET attendees = attendees + 1
PRINTLN "Ticket " .. ticket .. " sold. Attendees: " .. attendees
IF attendees >= capacity
PRINTLN "Venue at capacity!"
BREAK
END IF
SET ticket = ticket + 1
END WHILE
PRINTLN "Total tickets sold: " .. ticketBreak can exit loops when errors or invalid conditions are detected.
NEW INT i = 1
NEW BOOL errorOccurred = FALSE
WHILE i <= 10
PRINTLN "Processing item " .. i
IF i == 6
PRINTLN "ERROR: Invalid item detected"
SET errorOccurred = TRUE
BREAK
END IF
SET i = i + 1
END WHILE
IF errorOccurred == TRUE
PRINTLN "Process aborted due to error"
ELSE
PRINTLN "Process completed successfully"
END IFNEW INT value = 10
NEW BOOL isValid = TRUE
WHILE value <= 100
IF value % 7 == 0
IF value % 3 == 0
PRINTLN "Invalid: " .. value .. " divisible by both 3 and 7"
SET isValid = FALSE
BREAK
END IF
END IF
SET value = value + 1
END WHILE
IF isValid == TRUE
PRINTLN "All values valid"
END IFNEW INT pressure = 20
NEW INT cycle = 1
NEW BOOL safetyBreach = FALSE
WHILE cycle <= 10
SET pressure = pressure + 8
PRINTLN "Cycle " .. cycle .. ": Pressure = " .. pressure
IF pressure > 80
PRINTLN "ALERT: Pressure exceeds safe limit!"
SET safetyBreach = TRUE
BREAK
END IF
SET cycle = cycle + 1
END WHILECombining Break with Boolean variables provides clear loop exit logic.
NEW BOOL running = TRUE
NEW INT count = 0
WHILE running
SET count = count + 1
PRINTLN "Iteration: " .. count
IF count == 5
SET running = FALSE
PRINTLN "Stopping loop"
BREAK
END IF
END WHILE
// Displays: Iteration: 1, Iteration: 2, Iteration: 3, Iteration: 4, Iteration: 5, Stopping loopNEW BOOL success = FALSE
NEW INT attempt = 0
NEW INT maxAttempts = 5
WHILE attempt < maxAttempts
SET attempt = attempt + 1
PRINTLN "Attempt " .. attempt
IF attempt == 3
PRINTLN "Success!"
SET success = TRUE
BREAK
END IF
END WHILE
IF success == TRUE
PRINTLN "Operation succeeded"
ELSE
PRINTLN "Operation failed after " .. maxAttempts .. " attempts"
END IFNEW BOOL found = FALSE
NEW BOOL timeout = FALSE
NEW INT i = 0
WHILE TRUE
SET i = i + 1
IF i == 50
SET found = TRUE
PRINTLN "Target found!"
BREAK
END IF
IF i >= 100
SET timeout = TRUE
PRINTLN "Search timeout"
BREAK
END IF
END WHILEBreak is essential for exiting infinite loops (WHILE TRUE).
NEW INT selection = 0
WHILE TRUE
SET selection = selection + 1
PRINTLN "Menu - Option " .. selection
IF selection == 1
PRINTLN " Starting new game"
ELIF selection == 2
PRINTLN " Loading game"
ELIF selection == 3
PRINTLN " Exiting"
BREAK
END IF
END WHILE
PRINTLN "Program ended"NEW INT event = 0
NEW BOOL running = TRUE
WHILE TRUE
SET event = event + 1
PRINTLN "Processing event " .. event
IF event == 10
PRINTLN "Shutdown signal received"
BREAK
END IF
END WHILE
PRINTLN "Event loop terminated"NEW INT request = 0
NEW INT maxRequests = 100
WHILE TRUE
SET request = request + 1
PRINTLN "Handling request " .. request
IF request >= maxRequests
PRINTLN "Request limit reached"
BREAK
END IF
END WHILE
PRINTLN "Server stopped"NEW STR password = "secret123"
NEW INT attempt = 0
NEW INT maxAttempts = 3
NEW BOOL authenticated = FALSE
NEW STR input = ""
WHILE attempt < maxAttempts
SET attempt = attempt + 1
IF attempt == 1
SET input = "wrong1"
ELIF attempt == 2
SET input = "secret123"
ELIF attempt == 3
SET input = "wrong2"
END IF
PRINTLN "Attempt " .. attempt .. ": " .. input
IF input == password
PRINTLN "Access granted!"
SET authenticated = TRUE
BREAK
ELSE
PRINTLN "Incorrect password"
END IF
END WHILE
IF authenticated == FALSE
PRINTLN "Account locked"
END IFNEW INT stock = 100
NEW INT order = 1
NEW INT orderSize = 15
WHILE order <= 20
IF stock < orderSize
PRINTLN "Insufficient stock for order " .. order
PRINTLN "Remaining stock: " .. stock
BREAK
END IF
SET stock = stock - orderSize
PRINTLN "Order " .. order .. " fulfilled. Stock: " .. stock
SET order = order + 1
END WHILENEW INT score = 0
NEW INT level = 1
NEW INT targetScore = 100
WHILE level <= 20
SET score = score + ( level * 5 )
PRINTLN "Level " .. level .. " complete. Score: " .. score
IF score >= targetScore
PRINTLN "Achievement unlocked at level " .. level .. "!"
BREAK
END IF
SET level = level + 1
END WHILENEW INT processed = 0
NEW INT errors = 0
NEW INT maxErrors = 3
NEW INT item = 1
WHILE item <= 50
SET processed = processed + 1
PRINTLN "Processing item " .. item
IF item % 7 == 0
SET errors = errors + 1
PRINTLN " Error detected (total errors: " .. errors .. ")"
END IF
IF errors >= maxErrors
PRINTLN "Maximum error threshold reached"
BREAK
END IF
SET item = item + 1
END WHILE
PRINTLN "Processed " .. processed .. " items"NEW FLOAT balance = 500.0
NEW INT transaction = 1
NEW FLOAT amount = 75.0
WHILE transaction <= 20
PRINTLN "Transaction " .. transaction .. ": Withdrawing $" .. amount
IF balance < amount
PRINTLN "Insufficient funds! Balance: $" .. balance
BREAK
END IF
SET balance = balance - amount
PRINTLN " New balance: $" .. balance
SET transaction = transaction + 1
END WHILENEW INT seconds = 60
NEW BOOL stopped = FALSE
WHILE seconds > 0
PRINTLN "Time remaining: " .. seconds .. " seconds"
SET seconds = seconds - 1
IF seconds == 45
PRINTLN "Emergency stop activated"
SET stopped = TRUE
BREAK
END IF
END WHILE
IF stopped == TRUE
PRINTLN "Timer stopped early"
ELSE
PRINTLN "Timer completed"
END IFNEW INT resources = 0
NEW INT target = 50
NEW INT turn = 1
NEW INT collected = turn * 2
WHILE turn <= 20
SET resources = resources + collected
PRINTLN "Turn " .. turn .. ": Collected " .. collected .. " (Total: " .. resources .. ")"
IF resources >= target
PRINTLN "Target reached!"
BREAK
END IF
SET turn = turn + 1
END WHILENEW INT inspected = 0
NEW INT defects = 0
NEW INT maxDefectRate = 5
WHILE inspected < 100
SET inspected = inspected + 1
IF inspected % 15 == 0
SET defects = defects + 1
PRINTLN "Defect found in item " .. inspected
END IF
IF defects >= maxDefectRate
PRINTLN "Defect rate too high! Stopping production."
BREAK
END IF
END WHILE
PRINTLN "Inspected " .. inspected .. " items, found " .. defects .. " defects"// Using loop condition
NEW INT x = 1
PRINTLN "With condition:"
WHILE x <= 5
PRINTLN x
SET x = x + 1
END WHILE
// Displays: 1, 2, 3, 4, 5
// Using BREAK
NEW INT y = 1
PRINTLN "With BREAK:"
WHILE TRUE
PRINTLN y
SET y = y + 1
IF y > 5
BREAK
END IF
END WHILE
// Displays: 1, 2, 3, 4, 5A loop can have multiple Break statements for different exit conditions.
NEW INT value = 0
WHILE TRUE
SET value = value + 1
IF value == 10
PRINTLN "Reached 10"
BREAK
END IF
IF value % 7 == 0
PRINTLN "Found multiple of 7: " .. value
BREAK
END IF
END WHILE
// Displays: Found multiple of 7: 7NEW INT i = 0
WHILE i < 100
SET i = i + 1
IF i > 50
PRINTLN "Exceeded 50"
BREAK // This never executes
END IF
IF i == 25
PRINTLN "Hit 25 first"
BREAK // This executes first
END IF
END WHILE
// Displays: Hit 25 firstNEW INT temp = 60
NEW INT pressure = 20
NEW INT time = 0
WHILE time < 100
SET time = time + 1
SET temp = temp + 2
SET pressure = pressure + 3
IF temp > 100
PRINTLN "Critical: Temperature limit!"
BREAK // High priority
END IF
IF pressure > 80
PRINTLN "Warning: Pressure limit!"
BREAK // Medium priority
END IF
IF time > 50
PRINTLN "Info: Time limit"
BREAK // Low priority
END IF
END WHILENEW INT x = 0
NEW INT y = 100
WHILE x < 50
SET x = x + 1
SET y = y - 1
PRINTLN "x=" .. x .. ", y=" .. y
IF ( x > 20 ) && ( y < 70 )
PRINTLN "Both conditions met"
BREAK
END IF
END WHILENEW INT a = 0
NEW INT b = 0
NEW INT i = 0
WHILE i < 100
SET i = i + 1
SET a = a + 2
SET b = b + 3
IF ( a > 50 ) || ( b > 60 )
PRINTLN "First limit reached: a=" .. a .. ", b=" .. b
BREAK
END IF
END WHILEBreak can appear anywhere in the loop body, and execution stops immediately when it's reached.
NEW INT i = 0
WHILE i < 10
SET i = i + 1
IF i == 1
PRINTLN "Breaking immediately"
BREAK
END IF
PRINTLN "This never prints"
END WHILE
// Displays: Breaking immediatelyNEW INT count = 0
WHILE count < 10
SET count = count + 1
PRINTLN "Before break check: " .. count
IF count == 3
BREAK
END IF
PRINTLN "After break check: " .. count
END WHILE
// Displays: Before break check: 1, After break check: 1, Before break check: 2, After break check: 2, Before break check: 3NEW INT value = 0
WHILE value < 10
SET value = value + 1
PRINTLN "Processing: " .. value
IF value == 5
PRINTLN "Reached target"
BREAK // Last statement in loop
END IF
END WHILENEW INT x = 5
IF x == 5
BREAK // Runtime error! BREAK can only be used inside loops
END IF
// Correct: Use BREAK inside a loop
WHILE x > 0
IF x == 3
BREAK // Valid
END IF
SET x = x - 1
END WHILENEW INT i = 1
WHILE i <= 5
NEW INT j = 1
WHILE j <= 5
PRINTLN i .. "," .. j
IF j == 3
BREAK // ERROR: Only exits inner loop, not outer!
END IF
SET j = j + 1
END WHILE
SET i = i + 1
END WHILE
// Correct: Use flag to break outer loop
NEW INT a = 1
NEW BOOL breakAll = FALSE
WHILE a <= 5
NEW INT b = 1
WHILE b <= 5
IF b == 3
SET breakAll = TRUE
BREAK
END IF
SET b = b + 1
END WHILE
IF breakAll == TRUE
BREAK // Break outer loop
END IF
SET a = a + 1
END WHILENEW INT i = 1
WHILE i <= 10
IF i == 5
BREAK
PRINTLN "This never executes!" // Warning: Unreachable code
END IF
SET i = i + 1
END WHILE
// Correct: Put code before BREAK
WHILE i <= 10
IF i == 5
PRINTLN "Breaking now" // Executes before BREAK
BREAK
END IF
SET i = i + 1
END WHILENEW INT counter = 0
NEW BOOL found = FALSE
WHILE counter < 100
IF counter == 50
SET found = TRUE
BREAK
END IF
SET counter = counter + 1 // Never executes when counter is 50
END WHILE
PRINTLN "Counter: " .. counter // Displays 50, not 51
// This is actually correct behavior, but be aware!NEW INT x = 5
WHILE x < 10
IF x = 7 // Syntax error! Use == for comparison
BREAK
END IF
SET x = x + 1
END WHILE
// Correct: Use comparison operator
WHILE x < 10
IF x == 7
BREAK
END IF
SET x = x + 1
END WHILEWHILE TRUE
PRINTLN "Running forever!"
// ERROR: No BREAK statement, runs infinitely!
END WHILE
// Correct: Add condition with BREAK
NEW INT counter = 0
WHILE TRUE
SET counter = counter + 1
PRINTLN counter
IF counter >= 10
BREAK
END IF
END WHILENEW INT sum = 0
NEW INT i = 1
WHILE i <= 10
IF i == 5
BREAK // ERROR: Breaks before adding 5 to sum!
END IF
SET sum = sum + i
SET i = i + 1
END WHILE
PRINTLN sum // Displays 10, not 15
// Correct: Process before breaking
NEW INT total = 0
NEW INT j = 1
WHILE j <= 10
SET total = total + j
IF j == 5
BREAK // Breaks after adding 5
END IF
SET j = j + 1
END WHILE
PRINTLN total // Displays 15NEW INT i = 0
WHILE i < 10
SET i = i + 1
IF i == 5
BREAK // Exits loop entirely
END IF
PRINTLN i
END WHILE
// Displays: 1, 2, 3, 4 (stops at 5)
// If you want to skip 5 but continue:
NEW INT j = 0
WHILE j < 10
SET j = j + 1
IF j == 5
CONTINUE // Skips 5, continues loop
END IF
PRINTLN j
END WHILE
// Displays: 1, 2, 3, 4, 6, 7, 8, 9, 10NEW INT x = 5
IF x > 3
PRINTLN "x is greater than 3"
BREAK // Runtime error! BREAK only works in loops
END IF
// Correct: BREAK only in loops
WHILE x > 0
PRINTLN x
IF x == 3
BREAK // Valid in loop
END IF
SET x = x - 1
END WHILE