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

Slides:



Advertisements
Similar presentations
For loops Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas.
Advertisements

While loops.
While loops Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas.
Loops continued and coding efficiently Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas.
CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
File input and output if-then-else Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble.
True BASIC Ch. 6 Practice Questions. What is the output? PRINT X LET X = -1 PRINT X FOR X = 4 TO 5 STEP 2 PRINT X NEXT X PRINT X END.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
CS 106 Introduction to Computer Science I 02 / 11 / 2008 Instructor: Michael Eckmann.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Lecture 12 Another loop for repetition The while loop construct © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
CS 106 Introduction to Computer Science I 09 / 28 / 2007 Instructor: Michael Eckmann.
New Mexico Computer Science For All More Looping in NetLogo Maureen Psaila-Dombrowski.
Loops and Iteration Chapter 5 Python for Informatics: Exploring Information
The University of Texas – Pan American
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
Mastery Objective: Students will understand how to use while loops in computer programming.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
For loops in programming Assumes you have seen assignment statements and print statements.
Ch. 10 For Statement Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2012.
Python 101 Dr. Bernard Chen University of Central Arkansas PyArkansas.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
File input and output and conditionals Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas.
9. ITERATIONS AND LOOP STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Computer Program Flow Control structures determine the order of instruction execution: 1. sequential, where instructions are executed in order 2. conditional,
Computer Programming 12 Lesson 6 – Loop structure By: Dan Lunney.
PH2150 Scientific Computing Skills Control Structures in Python In general, statements are executed sequentially, top to bottom. There are many instances.
Introduction to Computing Science and Programming I
REPETITION CONTROL STRUCTURE
For loops Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble.
Introduction To Repetition The for loop
The switch Statement, and Introduction to Looping
Python: Control Structures
Warm-up Program Use the same method as your first fortune cookie project and write a program that reads in a string from the user and, at random, will.
Chapter 5: Loops and Files.
Topics Introduction to Repetition Structures
Python - Loops and Iteration
File input and output Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble Notes from 2009: Sample problem.
PH2150 Scientific Computing Skills
Dictionaries GENOME 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble.
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Conditionals (if-then-else)
While loops Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble.
LESSON 11 – WHILE LOOPS UNIT 5 – 1/10/17.
Arrays, For loop While loop Do while loop
For loops Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble Notes for 2010: I skipped slide 10. This is.
Python Primer 2: Functions and Control Flow
CMSC 120: Visualizing Information 3/25/08
Outline Altering flow of control Boolean expressions
String and Lists Dr. José M. Reyes Álamo.
Repetition Control Structure
3.5- The while Statement The while statement has the following syntax:
CMPT 102 Introduction to Scientific Computer Programming
3. Decision Structures Rocky K. C. Chang 19 September 2018
Fundamentals of visual basic
More for loops Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble.
Sequence comparison: Local alignment
Introduction to Repetition Structures
ICT Programming Lesson 3:
CHAPTER 6: Control Flow Tools (for and while loops)
While loops Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble.
Introduction to Computer Science
Introduction to Computer Science
Presentation transcript:

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

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

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

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 = 0 + 1 = 1 count = 1 + 1 = 2 sum = 1 + 2 = 3 count = 2 + 1 = 3 count < 3? FALSE 3 3

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

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.

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>

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).

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

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”.

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

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.

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

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.

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

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.

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