Difference between revisions of "Software tutorial/Loops"

From Process Model Formulation and Solution: 3E4
Jump to navigation Jump to search
m
m
 
Line 1: Line 1:
{{Navigation|Book=Software tutorial|previous=My first program|current=Tutorial index|next=}}
{{Navigation|Book=Software tutorial|previous=My first program|current=Tutorial index|next=Scripts and functions}}
__TOC__
__TOC__
== <tt>for</tt> loops ==
== <tt>for</tt> loops ==

Latest revision as of 12:43, 20 September 2010

← My first program (previous step) Tutorial index Next step: Scripts and functions →

for loops

Looping with a for loop is used when you know ahead of time how many iterations will be required.

MATLAB Python

A simple for-loop can be written as:

for k = 0:4
    disp(k)
end

and the output should be:

  0
  1
  2
  3
  4

The reason why this works is that instruction k = 0:4 creates a vector, starting at 0 and ending at 4, in steps of 1.0. You can verify this in MATLAB:

>> k = 0:4
k =
     0     1     2     3     4

Try looping and printing with these vectors. What is the output you expect when:

  • >> j = 0:0.2:5
    
  • >> i = 10:-2:-10
    

If you need help in MATLAB, you can type, for example:

help colon

and it will show you how to use the colon operator.

A simple for-loop can be written as:

for k in range(5):
    print(k)

and the output should be:

0
1
2
3
4

Why does the output not include the number 5? In Python, you can always check what a function does by using the help command. For example:

>>> help(range)
range([start,] stop[, step]) -> list of integers
    
    Return a list containing an arithmetic progression of integers.
    range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.
    When step is given, it specifies the increment (or decrement).
    For example, range(4) returns [0, 1, 2, 3].  The end point is omitted!
    These are exactly the valid indices for a list of 4 elements.

This shows you that the only input required to the range() function is the stop input - the other inputs, [start] and [step] have square brackets in the HELP text, indicating that they are optional inputs.

Using the above HELP text, what would you expect in Python if you typed:

  • >>> range(1,5)
    
  • >>> range(10, -10, -2)
    

Key differences

    • In MATLAB: you must close the loop with an end statement
    • In Python: you do not close the loop
    • In MATLAB: you must end every line with a semicolon ";" to prevent it showing the value of the variable.
    • In Python: you do not need to add semicolons; output will not be printed unless you explicitly use the print function.

while loops

Looping with a while loop is used when you do not know ahead of time how many iterations will be required.

MATLAB Python

A simple while-loop can be written as:

x = 100;
while x > 5
    x = x / 2.5;
    disp(x)
end

and the output you should see is:

    40
    16
    6.4000
    2.5600


A similar while-loop can be written in Python as:

x = 100
while x > 5:
    x = x / 2.5
    print(x)

and you should see the output as:

40.0
16.0
6.4
2.56

You could be even more concise in Python, if you prefer:

x = 100
while x > 5:
    x /= 2.5  # note the difference
    print(x)

Try the following commands in Python:

a = 10.0
a += 3  # what is the value of variable a?
a -= 5
a *= 2
a /= 4  # and the final value of a is ....