ProtoLang.net

Reference: End While

☰ Hide Sidebar
TOKEN END WHILE
ALIAS None

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.

Syntax

The short syntax structure uses the words END WHILE:

WHILE CONDITION
    STATEMENTS
END WHILE

The long syntax structure uses the words END WHILE:

WHILE CONDITION
    STATEMENTS
END WHILE

How End While Works

End While the serves closing boundary for While loop blocks:

  1. Marks the end of the loop's code block
  2. When reached, execution returns to the While statement
  3. The loop condition is re-evaluated
  4. If condition is True, the loop body executes again
  5. If condition is False, execution continues after End While

Example: Basic Loop Flow

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 finished

Basic End While Usage

Example: Simple Loop Structure

NEW 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, Done

Example: Loop with Multiple Statements

NEW 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"

Example: Empty Loop Body

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"

End Return While Point

When execution reaches End While, control transfers back to the While statement for condition re-evaluation.

Example: Demonstrating Return Flow

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 iterations

Example: Condition Check After End While

NEW 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"

Example: Multiple Iterations

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: " .. n

End While with Different Loop Patterns

Example: Countdown Pattern

NEW INT countdown = 5
WHILE countdown > 0
    PRINTLN countdown
    SET countdown = countdown - 1
END WHILE // Returns and checks countdown > 0
PRINTLN "Liftoff!"

Example: Accumulator Pattern

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: " .. sum

Example: Infinite Loop Pattern

NEW 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"

Example: Boolean Flag Pattern

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"

End While in Nested Loops

Each While loop must have its own End While delimiter. In nested loops, End While closes the most recently opened While.

Example: Basic Nested Loops

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"

Example: Three-Level Nesting

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 loop

Example: Nested Loop with Different Conditions

NEW 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 ends

Example: Multiplication Table

NEW 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 loop

End While with Break and Continue

When 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.

Example: Break Bypasses End While

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 loop

Example: Continue Goes to End While

NEW 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)

Example: Multiple Break Points

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 = " .. value

Example: Continue in Nested Loops

NEW 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 WHILE

Scope and End While

Variables declared inside the loop exist only until End While. They are recreated each iteration.

Example: Variable Scope in Loop

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 here

Example: Variables Recreated Each Iteration

NEW INT count = 1
WHILE count <= 3
    PRINTLN "Iteration " .. count
    SET count = count + 1
END WHILE // message is destroyed
// Each iteration creates a new message variable

Example: Local vs Loop Variable

NEW 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 exists

End While Terminates Code Block

All code between While and End While is part of the loop body and will repeat.

Example: Everything Before End While Repeats

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"

Example: Conditional Code in Loop

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 repeats

Example: Multiple Operations Per Iteration

NEW 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: " .. product

End While in Different Loop Types

Example: Count-Up Loop

NEW INT i = 1
WHILE i <= 5
    PRINT i .. " "
    SET i = i + 1
END WHILE
PRINTLN ""
// Displays: 1 2 3 4 5

Example: Count-Down Loop

NEW INT i = 5
WHILE i >= 1
    PRINT i .. " "
    SET i = i - 1
END WHILE
PRINTLN ""
// Displays: 5 4 3 2 1

Example: Step-By Loop

NEW INT i = 0
WHILE i <= 10
    PRINT i .. " "
    SET i = i + 2
END WHILE
PRINTLN ""
// Displays: 0 2 4 6 8 10

Example: Condition-Based Loop

NEW INT value = 1
WHILE value < 100
    PRINT value .. " "
    SET value = value * 2
END WHILE
PRINTLN ""
// Displays: 1 2 4 8 16 32 64

Execution Flow After End While

After 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.

Example: Normal Loop Exit

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

Example: Break Exit vs Normal Exit

// 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 = " .. y

Example: Execution Continues After Loop

NEW 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 loop

End While with Complex Structures

Example: Loop with Nested Conditionals

NEW 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 structure

Example: Loop with Multiple Conditionals

NEW 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 WHILE

Example: Loop with String Building

NEW 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"

Practical Applications

Example: Processing a Sequence

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 ""

Example: Accumulation Until Target

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"

Example: Search Pattern

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 IF

End While Positioning and Formatting

Example: Standard Formatting

NEW INT i = 1
WHILE i <= 3
    PRINTLN i
    SET i = i + 1
END WHILE // Standard: same WHILE indentation

Example: Single Statement Loop

NEW INT x = 5
WHILE x > 0
    SET x = x - 1
END WHILE // Even single-statement loops need END WHILE

Zero-Iteration Loops

If the loop condition is False initially, the loop body never executes, and control goes directly past End While.

Example: Condition False from Start

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 loop

Example: Empty Set Processing

NEW 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"

Important Notes

  • Every While loop must have exactly one corresponding End While.
  • End While marks where the loop body ends and control returns to While.
  • In nested loops, End While closes the most recently opened While.
  • Variables declared inside the loop are destroyed at End While.
  • Break jumps directly to the statement after End While.
  • Continue jumps to End While, then back to While for condition check.
  • If the loop condition is False, execution continues after End While.
  • End While requires the statement terminator (; or END STATEMENT).

Common Errors

Error: Missing End While

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"

Error: Extra End While

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 one

Error: Mismatched Nested Loops

NEW 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 closed

Error: Wrong Delimiter

NEW 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 delimiter

Error: Code After End While in Wrong Position

NEW 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 formatting

Error: Missing Statement Terminator

NEW 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 required

Error: Confusing End While Scope in Nested Loops

NEW 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 loop

Error: Using End While with Other Structures

NEW 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 WHILE

Error: Accessing Loop Variable After End While

NEW 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 loop

Error: Forgetting End While in Deeply Nested Code

NEW 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 WHILE

Error: Misunderstanding End While Return Flow

NEW 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