Presentation is loading. Please wait.

Presentation is loading. Please wait.

While loops Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble.

Similar presentations


Presentation on theme: "While loops Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble."— Presentation transcript:

1 while loops Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble

2 while loop statement1 statement2 while (<test>):
In English: While some logical test is true, continue executing the block of statements.

3 What does this program do?
sum = 0 count = 1 while (count < 3): sum = sum + count count = count + 1 print count print sum

4 What does this program do?
sum = 0 count = 1 while (count < 3): sum = sum + count count = count + 1 print count print sum sum = 0 count = 1 count < 3? TRUE sum = = 1 count = = 2 sum = = 3 count = = 3 count < 3? FALSE 3 3

5 Increment operator (+=)
x = x + y is the same as x += y sum = 0 count = 1 while (count < 3): sum = sum + count count = count + 1 print count print sum sum = 0 count = 1 while (count < 3): sum += count count += 1 print count print sum

6 For versus while loops You will probably use for loops more.
For is natural to loop through a list, characters in a string, etc. (anything of determinate size). While is natural to loop an indeterminate number of times until some condition is met.

7 Examples of for loops for base in sequence:
<do something with each base> for sequence in database: <do something with each sequence> for index in range(5,200): <do something with each index>

8 Examples of while loops
while (error > 0.05): <do something that will reduce error> while (score > 0): <traceback through a DP matrix, each time setting the current score> Warning: if you write a while loop and the conditional test stays True, the loop will run forever (infinite loop).

9 Terminating a loop while loops use continue and break in the same way as for loops: continue : jumps to the top of the enclosing loop break : breaks completely out of the enclosing loop

10 Sample problem #1 Convert the following program to a while loop:
import sys total = 0 for i in range(1, len(sys.argv)): total += int(sys.argv[i]) print total Run both programs with arguments “1 2 3”.

11 Solution #1 import sys total = 0 i = 1 while (i < len(sys.argv)) :
total += int(sys.argv[i]) i += 1 print total

12 Sample problem #2 Write a python program that uses a while loop to print all the square numbers up to a user-specified maximum: > python print-squares.py 20 1 4 9 16 My solution has 6 lines.

13 Solution #2 import sys max_int = int(sys.argv[1]) index = 1
while (index * index < max_int): print index * index index += 1

14 Sample problem #3 Write a python program to count the lines in a file using readline() and a while loop. > python count-lines.py myfile.txt 3 My solution has 8 lines.

15 Solution #3 import sys myfile = open(sys.argv[1], “r”) lineCount = 0
line = myfile.readline() while (line != ""): lineCount += 1 print lineCount

16 Sample problem #4: Optional command line arguments.
Write just the command line parsing portion of the following program: You can assume that required arguments come after optional ones.

17

18

19 Sample problem #5: Finish the full program for problem #4 (optional)


Download ppt "While loops Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble."

Similar presentations


Ads by Google