while ( Parametric )

  • The while ... : line that begins this code block expresses a condition that causes the code indented beneath that condition to execute so long as that condition is true.
  • The last line of indented code redefines a particular variable used in the while ... : line.
  • See (on this page):

Also see :


Introduction to while loops :

A while loop runs when its while statement is true. The while statement is true in this example when n is less than or equal to 5. When n becomes 6, the while statement becomes false. The variable named n becomes 6 after 5 is printed -- and that's when the while loop ceases to run.

SDS2 Python Prompt

>>> n = 1 >>> while n <= 5: ... print(n,) ... n = n + 1 ... 1 2 3 4 5

The while statement in this example is true when n is less than or equal to 5. When n is 5, it is changed to 6 and then printed. After 6 is printed, the while condition is false, and the loop ends.

SDS2 Python Prompt

>>> n = 1 >>> while n <= 5: ... n = n + 1 ... print(n,) ... 2 3 4 5 6

Note: When n is 1, 2, 3, 4, 5, the loop runs and, within the loop, n is changed to 2, 3, 4, 5, 6, then printed.

A while statement can take an else clause. The else clause runs when the loop ends normally, without a break . In other words, it runs when the while statement becomes false.

SDS2 Python Prompt

>>> n = 1 >>> while n <= 5: ... print(n,) ... n = n + 1 ... else: ... print("The loop has ended; n is", n) ... 1 2 3 4 5 The loop has ended; n is 6

If n is a number , a simple while loop such as while n: can remain true so long as n is nonzero. If n is a string , tuple or list , while n: can remain true so long as n is not empty.

SDS2 Python Prompt

>>> n = -4 >>> while n < 0: ... print(n,) ... n = n + 1 ... else: ... print("The loop ends when n is zero") ... -4 -3 -2 -1 The loop ends when n is zero

A break statement can be used to end a while loop. Without the break in this example, the while loop would happily chug along, mindlessly outputting 1 over and over again, hogging precious CPU resources, from now until eternity. In Windows , you can use Task Manager to end the process that results from a never-ending loop. In Linux , use top .

SDS2 Python Prompt

>>> n = 1 >>> while n: ... print(n,) ... break #stops a never-ending loop.

A continue statement can also be used in a while loop to control the flow of information. In this example, continue sends instances of n that are even back to the top of the loop before the print statement is reached, thus causing only odd values that are less than the initial value of n to be printed. % in this example is the modulo operator. It divides the number on its left by the number on its right and returns the remainder. Even numbers return a remainder of 0 when divided by 2; odd numbers return a remainder of 1 when divided by 2.

SDS2 Python Prompt

>>> # Prints odd numbers less than n. >>> n = 8 >>> while n > 0: ... n = n - 1 ... if n % 2 == 0: # if n is even ... continue # cycle back before print is reached ... print(n,) ... 7 5 3 1

Examples :

The following script creates an array of construction lines through a point. When the script is Run, the user left-clicks ( Locate ) to define a point, then enters the number of degrees of separation between construction lines. The script then generates construction lines in a pattern like spokes around the hub of a wheel.

# Creates an array of con lines through a point.
from cons_line import ConsLine
from point import PointLocate
from param import Prompt
var = PointLocate("Locate a point")
num = Prompt(20, "How many degrees apart?")
degr = 0
while degr <= 180:
    cl1 = ConsLine()
    cl1.Point1 = (var.X, var.Y, var.Z)
    cl1.Rotation = 0 + degr
    cl1.PenNumber = "Blue"
    cl1.Add()
    degr = degr + num

This next script marks as held ( mem1.MarkedForHold = "Held" ) the member ( mem1 ) that the user selects using MemberLocate() . A yes-no dialog then opens ( response = yes_or_no("...") ). The dialog returns 1 when the user presses the " Yes " button, causing the while test to remain true; the user is then prompted to select another member to mark as held. When the user presses " No ", the dialog returns 0 , which breaks the loop. After the while loop ends, the selection is cleared ( ClearSelection() ), and the message "You have ended this operation" is printed.

# Updates "Member hold status" of each selected member.
from member import MemberLocate
from param import ClearSelection, yes_or_no
response = 1
while response == 1:
    mem1 = MemberLocate("Select a member")
    mem1.MarkedForHold = "Held"
    mem1.Update()
    response = yes_or_no("Mark another member as held?")
print("You have ended this operation.")
ClearSelection()

This next example lets the user enter different section sizes for multiple members. Press " Cancel " on the dialog ( Size = Prompt(size, "Section size:") ) to end the while loop ( break ). After the while loop ends, the selection is cleared ( ClearSelection() ), and the script ceases to run because the interpreter has reached the script's last line.

# While loop for changing section sizes of members. Press "Cancel" to end the operation.
from param import Prompt, ResponseNotOK, ClearSelection
from member import MemberLocate
default = "W14x90"
while default:
    try: 
        Size = Prompt(default, "Section size:")
    except ResponseNotOK:        # user presses "Cancel"
        break
    mem1 = MemberLocate("Select a member")
    mem1.SectionSize = Size
    mem1.Update()
ClearSelection()

Tip: See Copying and Pasting scripts from Help for information on how to paste these scripts into blank .py text files that you can Run as parametrics in Modeling .