ProtoLang.net

Reference: Continue

☰ Hide Sidebar
TOKEN CONTINUE
ALIAS None

The Continue token is used to skip the remaining statements in the current loop iteration and immediately jump to the next iteration. When Continue executes, control transfers to the End While, and then back to the While statement where the condition is re-evaluated. Continue is useful for skipping specific iterations based on a condition without exiting the loop entirely.

Syntax

The short syntax structure uses the word CONTINUE:

WHILE CONDITION
    STATEMENTS
    CONTINUE
    STATEMENTS
END WHILE

The long syntax structure uses the word CONTINUE:

WHILE CONDITION
    STATEMENTS
    CONTINUE
    STATEMENTS
END WHILE

How Continue Works

Continue provides a way to skip the remainder of the current iteration:

  1. When Continue executes, the remaining statements in the loop body are skipped
  2. Control transfers to the End While
  3. Execution returns to the While statement
  4. The loop condition is re-evaluated
  5. If the condition is True, the next iteration begins

Example: Basic Continue Usage

NEW INT i = 0
WHILE i < 5
    SET i = i + 1
    IF i == 3
        CONTINUE
    END IF
    PRINTLN i
END WHILE
// Displays: 1, 2, 4, 5 (skips 3)

Basic Continue Operations

Example: Skipping a Single Value

NEW INT i = 0
WHILE i < 6
    SET i = i + 1
    IF i == 4
        CONTINUE
    END IF
    PRINTLN i
END WHILE
// Displays: 1, 2, 3, 5, 6

Example: Skipping Even Numbers

NEW INT i = 0
WHILE i < 10
    SET i = i + 1
    IF i % 2 == 0
        CONTINUE
    END IF
    PRINTLN i
END WHILE
// Displays: 1, 3, 5, 7, 9

Example: Skipping Odd Numbers

NEW INT i = 0
WHILE i < 10
    SET i = i + 1
    IF i % 2 != 0
        CONTINUE
    END IF
    PRINTLN i
END WHILE
// Displays: 2, 4, 6, 8, 10

Filtering Patterns

Continue is commonly used to filter out values that should not be processed.

Example: Skip Multiples of a Number

NEW INT i = 1
WHILE i <= 15
    IF i % 3 == 0
        SET i = i + 1
        CONTINUE
    END IF
    PRINTLN i
    SET i = i + 1
END WHILE
// Displays: 1, 2, 4, 5, 7, 8, 10, 11, 13, 14

Example: Processing Only Valid Values

NEW INT i = 1
NEW INT sum = 0
WHILE i <= 20
    IF i % 5 == 0
        SET i = i + 1
        CONTINUE
    END IF
    SET sum = sum + i
    SET i = i + 1
END WHILE
PRINTLN "Sum (excluding multiples of 5): " .. sum

Example: Skipping a Range of Values

NEW INT i = 1
WHILE i <= 10
    IF ( i >= 4 ) && ( i <= 6 )
        SET i = i + 1
        CONTINUE
    END IF
    PRINTLN i
    SET i = i + 1
END WHILE
// Displays: 1, 2, 3, 7, 8, 9, 10

Example: String Status Filter

NEW INT i = 1
NEW STR status = ""
WHILE i <= 5
    IF i == 1
        SET status = "active"
    ELIF i == 2
        SET status = "inactive"
    ELIF i == 3
        SET status = "active"
    ELIF i == 4
        SET status = "pending"
    ELIF i == 5
        SET status = "active"
    END IF
    IF status != "active"
        SET i = i + 1
        CONTINUE
    END IF
    PRINTLN "Processing item " .. i .. " (" .. status .. ")"
    SET i = i + 1
END WHILE
// Displays: Processing item 1 (active), Processing item 3 (active), Processing item 5 (active)

Accumulator Patterns with Continue

Continue is useful when accumulating values that meet a specific condition.

Example: Sum of Even Numbers

NEW INT i = 1
NEW INT sum = 0
WHILE i <= 10
    IF i % 2 != 0
        SET i = i + 1
        CONTINUE
    END IF
    SET sum = sum + i
    SET i = i + 1
END WHILE
PRINTLN "Sum of evens: " .. sum // Displays "Sum of evens: 30"

Example: Count Matching Values

NEW INT i = 1
NEW INT count = 0
WHILE i <= 30
    IF i % 4 != 0
        SET i = i + 1
        CONTINUE
    END IF
    SET count = count + 1
    SET i = i + 1
END WHILE
PRINTLN "Multiples of 4: " .. count // Displays "Multiples of 4: 7"

Example: Product of Qualifying Values

NEW INT i = 1
NEW INT product = 1
WHILE i <= 10
    IF i % 2 == 0
        SET i = i + 1
        CONTINUE
    END IF
    SET product = product * i
    SET i = i + 1
END WHILE
PRINTLN "Product of odds: " .. product // Displays "Product of odds: 945"

Continue vs Break

Continue skips the current iteration and moves to the next, while Break exits the loop entirely.

Example: Continue vs Break Comparison

// Using CONTINUE - skips value, loop continues
NEW INT i = 0
PRINTLN "With CONTINUE:"
WHILE i < 6
    SET i = i + 1
    IF i == 3
        CONTINUE
    END IF
    PRINTLN i
END WHILE
// Displays: 1, 2, 4, 5, 6

// Using BREAK - exits loop entirely
NEW INT j = 0
PRINTLN "With BREAK:"
WHILE j < 6
    SET j = j + 1
    IF j == 3
        BREAK
    END IF
    PRINTLN j
END WHILE
// Displays: 1, 2

Example: Using Both Continue and Break

NEW INT i = 0
WHILE i < 20
    SET i = i + 1
    IF i % 2 == 0
        CONTINUE
    END IF
    IF i > 9
        BREAK
    END IF
    PRINTLN i
END WHILE
// Displays: 1, 3, 5, 7, 9

Continue with Complex Conditions

Continue can be used with Logical Operators to skip iterations based on multiple criteria.

Example: Using And Operator

NEW INT i = 1
WHILE i <= 20
    IF ( i % 2 == 0 ) && ( i % 3 == 0 )
        SET i = i + 1
        CONTINUE
    END IF
    PRINTLN i
    SET i = i + 1
END WHILE
// Displays all numbers except multiples of both 2 and 3

Example: Using Or Operator

NEW INT i = 1
WHILE i <= 15
    IF ( i % 2 == 0 ) || ( i % 5 == 0 )
        SET i = i + 1
        CONTINUE
    END IF
    PRINTLN i
    SET i = i + 1
END WHILE
// Displays: 1, 3, 7, 9, 11, 13

Continue in Nested While Loops

In nested loops, Continue only affects the innermost loop containing it.

Example: Continue in Inner Loop

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
        END IF
        PRINTLN "  Inner: " .. inner
    END WHILE
    SET outer = outer + 1
    DELETE inner
END WHILE
// Displays inner values 1 and 3 for each outer iteration, skipping 2

Example: Continue in Outer Loop Only

NEW INT outer = 0
WHILE outer < 4
    SET outer = outer + 1
    IF outer == 2
        CONTINUE
    END IF
    PRINTLN "Outer: " .. outer
    NEW INT inner = 1
    WHILE inner <= 2
        PRINTLN "  Inner: " .. inner
        SET inner = inner + 1
    END WHILE
    DELETE inner
END WHILE
// Skips entire inner loop for outer == 2

Practical Applications

Example: FizzBuzz Skip Pattern

NEW INT i = 1
WHILE i <= 15
    IF ( i % 3 == 0 ) && ( i % 5 == 0 )
        PRINTLN "FizzBuzz"
        SET i = i + 1
        CONTINUE
    END IF
    IF i % 3 == 0
        PRINTLN "Fizz"
        SET i = i + 1
        CONTINUE
    END IF
    IF i % 5 == 0
        PRINTLN "Buzz"
        SET i = i + 1
        CONTINUE
    END IF
    PRINTLN i
    SET i = i + 1
END WHILE

Example: Skip Negative Processing

NEW INT i = -3
NEW INT positiveSum = 0
WHILE i <= 5
    IF i <= 0
        SET i = i + 1
        CONTINUE
    END IF
    SET positiveSum = positiveSum + i
    SET i = i + 1
END WHILE
PRINTLN "Sum of positives: " .. positiveSum // Displays "Sum of positives: 15"

Example: Reporting with Exceptions

NEW INT day = 1
WHILE day <= 7
    IF ( day == 6 ) || ( day == 7 )
        SET day = day + 1
        CONTINUE
    END IF
    PRINTLN "Work day: " .. day
    SET day = day + 1
END WHILE
// Displays: Work day: 1, Work day: 2, Work day: 3, Work day: 4, Work day: 5

Example: Inventory Skip

NEW INT item = 1
NEW INT totalValue = 0
NEW INT itemValue = 0
WHILE item <= 10
    SET itemValue = item * 5
    IF itemValue < 20
        SET item = item + 1
        CONTINUE
    END IF
    SET totalValue = totalValue + itemValue
    PRINTLN "Item " .. item .. " valued at $" .. itemValue
    SET item = item + 1
END WHILE
PRINTLN "Total value: $" .. totalValue

Example: Score Filter

NEW INT player = 1
NEW INT score = 0
NEW INT totalScore = 0
NEW INT qualifiedCount = 0
WHILE player <= 8
    SET score = player * 12
    IF score < 50
        PRINTLN "Player " .. player .. " did not qualify (score: " .. score .. ")"
        SET player = player + 1
        CONTINUE
    END IF
    SET totalScore = totalScore + score
    SET qualifiedCount = qualifiedCount + 1
    PRINTLN "Player " .. player .. " qualified (score: " .. score .. ")"
    SET player = player + 1
END WHILE
PRINTLN "Qualified players: " .. qualifiedCount
PRINTLN "Combined score: " .. totalScore

Important Notes

  • Continue skips only the remaining statements in the current iteration, not the entire loop.
  • After Continue, the loop condition is re-evaluated before the next iteration.
  • Always ensure the loop variable is updated before Continue to avoid infinite loops.
  • In nested loops, Continue only affects the innermost loop containing it.
  • Continue is different from Break, which exits the loop entirely.
  • No code after Continue in the same conditional block will execute.
  • Continue can be used multiple times within a single loop body.
  • The condition can use any Comparison or Logical operators.

Common Errors

Error: Forgetting to Update Counter Before Continue

NEW INT i = 0
WHILE i < 10
    IF i % 2 == 0
        CONTINUE // ERROR! i never increments when even, infinite loop!
    END IF
    PRINTLN i
    SET i = i + 1
END WHILE
// Correct: Update counter before CONTINUE
NEW INT j = 0
WHILE j < 10
    SET j = j + 1 // Update first
    IF j % 2 == 0
        CONTINUE
    END IF
    PRINTLN j
END WHILE

Error: Continue Outside Loop

NEW INT x = 5
IF x == 5
    CONTINUE // Runtime error! CONTINUE can only be used inside loops
END IF
// Correct: Use CONTINUE inside a loop
NEW INT i = 0
WHILE i < 10
    SET i = i + 1
    IF i == 5
        CONTINUE // Valid
    END IF
    PRINTLN i
END WHILE

Error: Unreachable Code After Continue

NEW INT i = 0
WHILE i < 5
    SET i = i + 1
    IF i == 3
        CONTINUE
        PRINTLN "This never executes!" // Warning: Unreachable code
    END IF
    PRINTLN i
END WHILE
// Correct: Put code before CONTINUE
NEW INT j = 0
WHILE j < 5
    SET j = j + 1
    IF j == 3
        PRINTLN "Skipping 3" // Executes before CONTINUE
        CONTINUE
    END IF
    PRINTLN j
END WHILE

Error: Confusing Continue with Break

NEW INT i = 0
WHILE i < 10
    SET i = i + 1
    IF i == 5
        CONTINUE // Skips 5, continues to 6, 7, 8, 9, 10
    END IF
    PRINTLN i
END WHILE
// Displays: 1, 2, 3, 4, 6, 7, 8, 9, 10

// If you want to stop at 5:
NEW INT j = 0
WHILE j < 10
    SET j = j + 1
    IF j == 5
        BREAK // Exits loop entirely
    END IF
    PRINTLN j
END WHILE
// Displays: 1, 2, 3, 4

Error: Using Continue in Wrong Control Structure

NEW INT x = 5
IF x > 3
    PRINTLN "x is greater than 3"
    CONTINUE // Runtime error! CONTINUE only works in loops
END IF
// Correct: CONTINUE only in loops
NEW INT i = 0
WHILE i < 10
    SET i = i + 1
    IF i > 3
        CONTINUE // Valid in loop
    END IF
    PRINTLN i
END WHILE

Error: Infinite Loop from Missing Update

NEW INT i = 1
WHILE i <= 10
    IF i % 2 != 0
        CONTINUE // ERROR! i never increments for odd numbers
    END IF
    PRINTLN i
    SET i = i + 1
END WHILE
// Correct: Always update loop variable before CONTINUE
NEW INT j = 1
WHILE j <= 10
    SET j = j + 1 // Increment at top of loop
    IF j % 2 != 0
        CONTINUE
    END IF
    PRINTLN j
END WHILE

Error: Mixing Integer and Boolean in Condition

NEW INT i = 0
WHILE i < 10
    SET i = i + 1
    IF i // Type error! Condition must be Boolean, not Integer
        CONTINUE
    END IF
    PRINTLN i
END WHILE
// Correct: Use comparison to create Boolean
NEW INT j = 0
WHILE j < 10
    SET j = j + 1
    IF j % 2 == 0 // Boolean result from comparison
        CONTINUE
    END IF
    PRINTLN j
END WHILE