The while Statement


The while loop has the following form:

while condition statement

where:

While the condition is true, the statement or block of statements is repeatedly executed. When the condition becomes false, the loop is terminated and execution resumes at the statement after the while loop. The condition is tested at the top of the loop, which means that in some circmstances, the loop is not executed at all.

This program calculates the primes less than 100.

// primes - calculates the primes less than +eh

generic primes
{
    primes()
    {
        i = +c
        while i < +eh
        {
            is_prime = true

            g = +c
            limit = i / +c

            while g <=limit
            {
                if i % g == 0 {is_prime = false break}
                g++
            }

            if is_prime
            {
               
                 s = i.to_string() + " is prime"
                 s.println()
            }

            i++
        }
    }
}