Array

Creating Loops

Must Read

Admin
Admin
Just post!

Computers are far better at repetitive tasks than humans. A computer will perform the same task as long as you want it to. In fact, your computer is currently performing tasks repetitively, even if you didn’t start those tasks. For example, you probably have a firewall that’s looking for terrifying inputs from outside sources, using some type of repetitive procedure. Applications handle these repetitive tasks using loops. IronPython provides two kinds of loops as described in the following sections:

  • for…in
  • while

Using for . . . in

The for…in loop is the best way to process lists of things in most cases. You’ll find the for…in loop used with both arrays and collections in many situations (rather than the while loop, which can appear somewhat clumsy and can perform poorly for list processing). Listing 3-5 shows a for…in loop in action.

Listin g 3-5: Looping through data using for…in

[code]

# Create an array of strings.
MyList = ‘Hello’, ‘Goodbye’, ‘Red’, ‘Green’
# Process the array.
for ThisString in MyList:
# Display the individual values.
print ‘The current value is:’, ThisString
# Pause after the debug session.
raw_input(‘Press any key to continue…’)

[/code]

The code begins by creating an array. This is a very simple array, but it demonstrates just how easy IronPython makes certain programming tasks. You don’t have to worry about doing anything odd when creating the array. In addition, you’ll find that arrays, like strings, come with a wealth of methods. For example, if you want to add a new member to an array, you simply call on the append() method to perform the task. The site at http://docs.python.org/dev/3.0/library/array.html describes array methods and types in greater detail.

After the code creates the array, it uses the for…in loop to process it. The interpreter automatically calls the loop code once for each of the values in the array. The individual array values appear in ThisString. In this case, the code merely prints out the values of ThisString, as shown in Figure 3-3.

The for loop is exceptionally efficient at list processing.

The for…in loop always has two parts: a target and an expression list. The target can be another expression list, in which case you can nest for…in loops to process the target. It’s possible to dig down into just about any hierarchy of objects using a for…in loop.

Using while

The while loop might not be quite as pretty as the for…in loop, but it serves an important purpose. The for…in loop works with a fixed number of elements. The while loop can work with an arbitrary number of elements, or can remain running until told to stop. You use a while loop in situations where you don’t know how many times a loop will occur during design time. Of course, this means that you must give the while loop a positive method of ending, as shown in Listing 3-6.

Listin g 3-6: Looping through data using while

[code]

# Create an array of strings.
MyList = ‘Hello’, ‘Goodbye’, ‘Red’, ‘Green’
# Define a counter variable.
Counter = 0
# Define the current string.
ThisString = ‘’
# Process the array.
while ThisString != ‘Green’:
# Get the next value.
ThisString = MyList[Counter]
# Display the individual values.
print ‘The current value is:’, ThisString
# Update the counter.
Counter+=1
# Pause after the debug session.
raw_input(‘Press any key to continue…’)

[/code]

This example begins with the same array as shown in Listing 3-5. In order to make the while loop work, the code must define Counter, which keeps track of the current array element. The code also defines ThisString, which holds the current array value. As you can already see, the while loop isn’t nearly as automatic as the for…in loop, but it does provide considerable flexibility.

In this case, the while loop continues to run until ThisString is equal to ‘Green‘, the last value in the array. As always, the while statement ends with a colon and every indented statement after it is part of the while structure.

The code relies on Counter to access the individual array elements. You simply provide Counter as an index into MyList using MyList[Counter]. The code then prints out the value found in ThisString. Finally, the code updates Counter. Notice that IronPython supports the use of the += shortcut. The output from this application appears in Figure 3-4.

Use the while loop when you need to perform tasks an indeterminate number of times.

You might have noticed that IronPython doesn’t appear to support the vast array of loop statement subsets found in other languages. For example, there isn’t any version of the while statement that tests for the condition after executing the code the first time. In general, this apparent limitation actually makes things considerably easier. You simply need to write your code in such a way as to accommodate the “test first” orientation of the IronPython while loop.

- Advertisement -

Latest News

Elevate Your Bentley Experience: The Bespoke Elegance of Bentayga EWB by Mulliner

Bentley Motors redefines the essence of bespoke luxury with the introduction of the Bentayga EWB's groundbreaking two-tone customization option—a...
- Advertisement -

More Articles Like This

- Advertisement -