The End While token marks the end of a While loop's code block. Every While loop must have a corresponding End While delimiter to define where the loop body ends. When execution reaches End While, control returns to the beginning of the loop where the condition is re-evaluated.
The short syntax structure uses the words END WHILE:
WHILE CONDITION
STATEMENTS
END WHILEThe long syntax structure uses the words END WHILE:
WHILE CONDITION
STATEMENTS
END WHILEEnd While the serves closing boundary for While loop blocks:
NEW INT i = 1
WHILE i <= 3
PRINTLN "Start of iteration " .. i
SET i = i + 1
PRINTLN "End of iteration"
END WHILE // Control returns to WHILE, condition is checked
PRINTLN "Loop finished"
// Displays: Start of iteration 1, End of iteration, Start of iteration 2, End of iteration, Start of iteration 3, End of iteration, Loop finishedNEW INT counter = 1
WHILE counter <= 5
PRINTLN counter
SET counter = counter + 1
END WHILE // Loop ends here
PRINTLN "Done"
// Displays: 1, 2, 3, 4, 5, DoneNEW INT x = 1
WHILE x <= 3
PRINTLN "Value: " .. x
PRINTLN "Squared: " .. x * x
SET x = x + 1
END WHILE // All statements above are part of loop
PRINTLN "Exited loop"NEW INT count = 5
WHILE count > 0
SET count = count - 1
END WHILE // Loop can have just one statement
PRINTLN "Count: " .. count // Displays "Count: 0"When execution reaches End While, control transfers back to the While statement for condition re-evaluation.
NEW INT i = 1
WHILE i <= 3
PRINTLN "At start: i = " .. i
SET i = i + 1
PRINTLN "At end: i = " .. i
END WHILE // Returns to WHILE, checks if i <= 3
PRINTLN "Final: i = " .. i
// Shows how i changes through iterationsNEW INT value = 10
WHILE value > 5
PRINTLN "Value is " .. value
SET value = value - 1
END WHILE // Returns to check: Is value > 5?
PRINTLN "Stopped when value = " .. value // Displays "Stopped when value = 5"NEW INT n = 0
WHILE n < 3
SET n = n + 1
PRINTLN "Iteration " .. n .. " complete"
END WHILE // Returns to WHILE after each iteration
PRINTLN "Total iterations: " .. nNEW INT countdown = 5
WHILE countdown > 0
PRINTLN countdown
SET countdown = countdown - 1
END WHILE // Returns and checks countdown > 0
PRINTLN "Liftoff!"NEW INT i = 1
NEW INT sum = 0
WHILE i <= 10
SET sum = sum + i
SET i = i + 1
END WHILE // Loop continues until i > 10
PRINTLN "Sum: " .. sumNEW INT count = 0
WHILE TRUE
SET count = count + 1
PRINTLN count
IF count >= 5
BREAK
END IF
END WHILE // Would return to WHILE if no BREAK
PRINTLN "Stopped"NEW BOOL running = TRUE
NEW INT i = 0
WHILE running
SET i = i + 1
PRINTLN i
IF i >= 3
SET running = FALSE
END IF
END WHILE // Returns and checks running == TRUE
PRINTLN "Finished"Each While loop must have its own End While delimiter. In nested loops, End While closes the most recently opened While.
NEW INT outer = 1
WHILE outer <= 3
PRINTLN "Outer: " .. outer
NEW INT inner = 1
WHILE inner <= 2
PRINTLN " Inner: " .. inner
SET inner = inner + 1
END WHILE // Closes inner loop
SET outer = outer + 1
DELETE inner
END WHILE // Closes outer loop
PRINTLN "Both loops done"NEW INT i = 1
NEW INT j = 1
NEW INT k = 1
WHILE i <= 2
WHILE j <= 2
WHILE k <= 2
PRINTLN i .. "," .. j .. "," .. k
SET k = k + 1
END WHILE // Closes k loop
SET j = j + 1
END WHILE // Closes j loop
SET i = i + 1
END WHILE // Closes i loopNEW INT x = 1
WHILE x < 4
PRINTLN "x = " .. x
NEW INT y = 10
WHILE y > 7
PRINTLN " y = " .. y
SET y = y - 1
END WHILE // Inner loop ends
SET x = x + 1
DELETE y
END WHILE // Outer loop endsNEW INT row = 1
WHILE row <= 3
NEW INT col = 1
WHILE col <= 3
PRINT row * col .. " "
SET col = col + 1
END WHILE // End of column loop
PRINTLN ""
SET row = row + 1
DELETE col
END WHILE // End of row loopWhen Break executes, control jumps directly to the statement after End While. When Continue executes, control jumps to End While, then returns to While for condition check.
NEW INT i = 1
WHILE i <= 10
IF i == 5
PRINTLN "Breaking at " .. i
BREAK // Jumps directly past END WHILE
END IF
PRINTLN i
SET i = i + 1
END WHILE // BREAK skips to here
PRINTLN "After loop"
// Displays: 1, 2, 3, 4, Breaking at 5, After loopNEW INT i = 0
WHILE i < 5
SET i = i + 1
IF i == 3
CONTINUE // Jumps to END WHILE
END IF
PRINTLN i
END WHILE // CONTINUE jumps here, then back to WHILE
// Displays: 1, 2, 4, 5 (skips 3)NEW INT value = 0
WHILE TRUE
SET value = value + 1
IF value == 10
PRINTLN "Reached 10"
BREAK // First possible exit
END IF
IF value % 7 == 0
PRINTLN "Found multiple of 7"
BREAK // Second possible exit
END IF
END WHILE // Both BREAKs jump here
PRINTLN "Exited at value = " .. valueNEW INT outer = 1
WHILE outer <= 3
PRINTLN "Outer: " .. outer
NEW INT inner = 0
WHILE inner < 3
SET inner = inner + 1
IF inner == 2
CONTINUE // Jumps to inner END WHILE
END IF
PRINTLN " Inner: " .. inner
END WHILE // Inner CONTINUE jumps here
SET outer = outer + 1
DELETE inner
END WHILE // Outer loop's END WHILEVariables declared inside the loop exist only until End While. They are recreated each iteration.
NEW INT i = 1
WHILE i <= 3
NEW INT temp = i * 10
PRINTLN "temp = " .. temp
SET i = i + 1
DELETE temp
END WHILE // temp no longer exists after this point
// PRINTLN temp // Error: temp doesn't exist hereNEW INT count = 1
WHILE count <= 3
PRINTLN "Iteration " .. count
SET count = count + 1
END WHILE // message is destroyed
// Each iteration creates a new message variableNEW INT outer = 1
WHILE outer <= 3
NEW INT inner = outer * 2
PRINTLN "outer=" .. outer .. ", inner=" .. inner
SET outer = outer + 1
DELETE inner
END WHILE // inner destroyed, outer persists
PRINTLN "Final outer: " .. outer // outer still existsAll code between While and End While is part of the loop body and will repeat.
NEW INT i = 1
WHILE i <= 2
PRINTLN "First statement"
PRINTLN "Second statement"
PRINTLN "Third statement"
SET i = i + 1
END WHILE // All four statements above repeat
PRINTLN "This executes once"NEW INT n = 1
WHILE n <= 5
IF n % 2 == 0
PRINTLN n .. " is even"
ELSE
PRINTLN n .. " is odd"
END IF
SET n = n + 1
END WHILE // Entire IF/ELSE structure repeatsNEW INT x = 1
NEW INT sum = 0
NEW INT product = 1
WHILE x <= 4
SET sum = sum + x
SET product = product * x
PRINTLN "x=" .. x .. ", sum=" .. sum .. ", product=" .. product
SET x = x + 1
END WHILE // All calculations repeat
PRINTLN "Final sum: " .. sum
PRINTLN "Final product: " .. productNEW INT i = 1
WHILE i <= 5
PRINT i .. " "
SET i = i + 1
END WHILE
PRINTLN ""
// Displays: 1 2 3 4 5NEW INT i = 5
WHILE i >= 1
PRINT i .. " "
SET i = i - 1
END WHILE
PRINTLN ""
// Displays: 5 4 3 2 1NEW INT i = 0
WHILE i <= 10
PRINT i .. " "
SET i = i + 2
END WHILE
PRINTLN ""
// Displays: 0 2 4 6 8 10NEW INT value = 1
WHILE value < 100
PRINT value .. " "
SET value = value * 2
END WHILE
PRINTLN ""
// Displays: 1 2 4 8 16 32 64After End While, if the condition is False, execution continues with the next statement. If the condition is True, execution returns to the start of the loop body.
NEW INT i = 1
WHILE i <= 3
PRINTLN "Loop iteration " .. i
SET i = i + 1
END WHILE // When i becomes 4, condition is False
PRINTLN "First statement after loop"
PRINTLN "Second statement after loop"
// Loop exits normally when condition becomes False// Normal exit
NEW INT x = 1
WHILE x <= 3
PRINTLN x
SET x = x + 1
END WHILE // Exits when x > 3
PRINTLN "Exited normally, x = " .. x
// Break exit
NEW INT y = 1
WHILE y <= 10
PRINTLN y
IF y == 3
BREAK
END IF
SET y = y + 1
END WHILE // BREAK jumps here
PRINTLN "Exited early, y = " .. yNEW INT sum = 0
NEW INT i = 1
WHILE i <= 5
SET sum = sum + i
SET i = i + 1
END WHILE // Loop ends
PRINTLN "Sum of 1-5: " .. sum // This executes after loop
NEW INT average = sum / 5
PRINTLN "Average: " .. average // This also executes after loopNEW INT num = 1
WHILE num <= 10
IF num % 3 == 0
IF num % 2 == 0
PRINTLN num .. " is divisible by 6"
ELSE
PRINTLN num .. " is divisible by 3 only"
END IF
END IF
SET num = num + 1
END WHILE // Closes the entire structureNEW INT i = 1
WHILE i <= 10
IF i < 5
PRINTLN i .. " is less than 5"
ELIF i == 5
PRINTLN i .. " equals 5"
ELSE
PRINTLN i .. " is greater than 5"
END IF
SET i = i + 1
END WHILENEW INT i = 1
NEW STR result = ""
WHILE i <= 5
SET result = result .. i
IF i < 5
SET result = result .. ", "
END IF
SET i = i + 1
END WHILE
PRINTLN "Result: " .. result // Displays "Result: 1, 2, 3, 4, 5"NEW INT current = 1
NEW INT previous = 0
NEW INT temp = 0
NEW INT count = 1
WHILE count <= 8
PRINT current .. " "
SET temp = current
SET current = current + previous
SET previous = temp
SET count = count + 1
END WHILE // Generates Fibonacci sequence
PRINTLN ""NEW INT total = 0
NEW INT add = 1
NEW INT target = 50
WHILE total < target
SET total = total + add
PRINTLN "Added " .. add .. ", total now: " .. total
SET add = add + 1
END WHILE
PRINTLN "Reached target in " .. ( add - 1 ) .. " additions"NEW INT i = 1
NEW BOOL found = FALSE
NEW INT searchFor = 42
WHILE ( i <= 100 ) && ( found == FALSE )
IF i == searchFor
PRINTLN "Found " .. searchFor .. " at position " .. i
SET found = TRUE
END IF
SET i = i + 1
END WHILE
IF found == FALSE
PRINTLN "Not found"
END IFNEW INT i = 1
WHILE i <= 3
PRINTLN i
SET i = i + 1
END WHILE // Standard: same WHILE indentationNEW INT x = 5
WHILE x > 0
SET x = x - 1
END WHILE // Even single-statement loops need END WHILEIf the loop condition is False initially, the loop body never executes, and control goes directly past End While.
NEW INT x = 10
PRINTLN "Before loop"
WHILE x < 5
PRINTLN "This never prints"
SET x = x + 1
END WHILE // Skipped entirely
PRINTLN "After loop"
// Displays: Before loop, After loopNEW INT count = 0
NEW INT processed = 0
WHILE processed < count
PRINTLN "Processing"
SET processed = processed + 1
END WHILE // Never enters loop
PRINTLN "Nothing to process"NEW INT i = 1
WHILE i <= 5
PRINTLN i
SET i = i + 1
// Syntax error! Missing END WHILE
PRINTLN "After loop"
// Correct: Include END WHILE
NEW INT j = 1
WHILE j <= 5
PRINTLN j
SET j = j + 1
END WHILE // Required
PRINTLN "After loop"NEW INT i = 1
WHILE i <= 5
PRINTLN i
SET i = i + 1
END WHILE
END WHILE // Syntax error! Extra END WHILE with no matching WHILE
// Correct: One END WHILE per WHILE
NEW INT j = 1
WHILE j <= 5
PRINTLN j
SET j = j + 1
END WHILE // Only oneNEW INT i = 1
WHILE i <= 3
NEW INT j = 1
WHILE j <= 3
PRINTLN i .. "," .. j
SET j = j + 1
// Syntax error! Missing END WHILE for inner loop
SET i = i + 1
END WHILE
// Correct: Each WHILE needs END WHILE
NEW INT a = 1
WHILE a <= 3
NEW INT b = 1
WHILE b <= 3
PRINTLN a .. "," .. b
SET b = b + 1
END WHILE // Inner loop closed
SET a = a + 1
END WHILE // Outer loop closedNEW INT i = 1
WHILE i <= 5
PRINTLN i
SET i = i + 1
END IF // Syntax error! Wrong delimiter - should be END WHILE
// Correct: Use END WHILE for loops
WHILE i <= 5
PRINTLN i
SET i = i + 1
END WHILE // Correct delimiterNEW INT i = 1
WHILE i <= 5
PRINTLN i
SET i = i + 1
END WHILE
PRINTLN "This has wrong indentation" // Confusing indentation
// Correct: Code after loop not indented
NEW INT j = 1
WHILE j <= 5
PRINTLN j
SET j = j + 1
END WHILE
PRINTLN "This is correctly formatted" // Proper formattingNEW INT i = 1
WHILE i <= 5
PRINTLN i
SET i = i + 1
END WHILE // Syntax error! Missing semicolon
PRINTLN "Done"
// Correct: END WHILE needs terminator
WHILE i <= 5
PRINTLN i
SET i = i + 1
END WHILE // Semicolon requiredNEW INT i = 1
WHILE i <= 2
NEW INT j = 1
WHILE j <= 2
PRINTLN i .. "," .. j
SET j = j + 1
END WHILE // Error: This closes inner loop but is at wrong indentation level
SET i = i + 1
END WHILE
// Correct: Proper indentation for clarity
NEW INT a = 1
WHILE a <= 2
NEW INT b = 1
WHILE b <= 2
PRINTLN a .. "," .. b
SET b = b + 1
END WHILE // Clear: closes inner loop
SET a = a + 1
END WHILE // Clear: closes outer loopNEW INT x = 5
IF x > 3
PRINTLN "x is large"
END WHILE // Syntax error! IF uses END IF, not END WHILE
// Correct: Use proper delimiters
IF x > 3
PRINTLN "x is large"
END IF // Correct for IF
NEW INT i = 1
WHILE i <= 5
PRINTLN i
SET i = i + 1
END WHILE // Correct for WHILENEW INT i = 1
WHILE i <= 3
NEW INT temp = i * 10
PRINTLN temp
SET i = i + 1
END WHILE
PRINTLN temp // Runtime error! temp doesn't exist outside loop
// Correct: Use variables declared outside loop
NEW INT j = 1
NEW INT lastTemp = 0
WHILE j <= 3
SET lastTemp = j * 10
PRINTLN lastTemp
SET j = j + 1
END WHILE
PRINTLN lastTemp // Valid: lastTemp declared outside loopNEW INT i = 1
WHILE i <= 3
NEW INT j = 1
WHILE j <= 3
IF i == j
PRINTLN "Match: " .. i
END IF
SET j = j + 1
// Error: Forgot END WHILE for inner loop
SET i = i + 1
END WHILE
// Correct: Track each WHILE with its END WHILE
NEW INT a = 1
WHILE a <= 3
NEW INT b = 1
WHILE b <= 3
IF a == b
PRINTLN "Match: " .. a
END IF
SET b = b + 1
END WHILE // Close inner WHILE
SET a = a + 1
END WHILE // Close outer WHILENEW INT i = 1
WHILE i <= 3
PRINTLN "i = " .. i
SET i = i + 1
PRINTLN "After increment: i = " .. i
END WHILE // Returns to WHILE, checks condition with new i value
// Common mistake: thinking it checks condition before increment
// Understanding: The condition check happens at WHILE, not END WHILE
// So when i becomes 4, we're already at END WHILE
// The loop exits because condition (4 <= 3) is False