CMSC 150 LOOPS CS 150: Fri 20 Jan 2012
Representing DNA AGTCCAGTGTCAA
Start Codon: ATG
Consider in Java String dna = “AGTCCAGTGTCAA”;
Consider in Java String dna = “AGTCCAGTGTCAA”; if ( dna.substring(0,3).equals(“ATG”) )
String dna = “AGTCCAGTGTCAA”; if ( dna.substring(0,3).equals(“ATG”) ) if ( dna.substring(1,4).equals(“ATG”) ) Consider in Java
String dna = “AGTCCAGTGTCAA”; if ( dna.substring(0,3).equals(“ATG”) ) if ( dna.substring(1,4).equals(“ATG”) ) if ( dna.substring(2,5).equals(“ATG”) ) Consider in Java
String dna = “AGTCCAGTGTCAA”; if ( dna.substring(0,3).equals(“ATG”) ) if ( dna.substring(1,4).equals(“ATG”) ) if ( dna.substring(2,5).equals(“ATG”) ) if ( dna.substring(3,6).equals(“ATG”) ) if ( dna.substring(4,7).equals(“ATG”) )... if ( dna.substring(10,12).equals(“ATG”) )
Loops
Loop Syntax while ( condition ) { statement ; } for ( initialization; condition; update ) { statement ; }
Loop Syntax while ( condition ) { statement ; } for ( initialization; condition; update ) { statement ; } Use while when you don’t know in advance the # of times to loop Use while when you don’t know in advance the # of times to loop
Loop Syntax while ( condition ) { statement ; } for ( initialization; condition; update ) { statement ; } Use for when you know in advance the # of times to loop Use for when you know in advance the # of times to loop
While Loop Syntax while ( condition ) { statement_to_execute; } build a condition that eventually becomes false need statement w/in body to advance toward false condition evaluated each time before executing statement
While Loop Example while ( condition ) { statement_to_execute; } int count = 0; while ( count < 3 ) { System.out.println( “count = “ + count); count++; }
While Loop Action int count = 0; while ( count < 3 ) { System.out.println( “count = “ + count ); count++; } 1. initialize count to 0
While Loop Action int count = 0; while ( count < 3 ) { System.out.println( “count = “ + count ); count++; } 1. initialize count to 0 2. evaluate condition: count < 3 is true, so…
While Loop Action int count = 0; while ( count < 3 ) { System.out.println( “count = “ + count ); count++; } 1. initialize count to 0 2. evaluate condition: count < 3 is true, so… 3. execute statement: prints count = 0
While Loop Action int count = 0; while ( count < 3 ) { System.out.println( “count = “ + count ); count++; } 1. initialize count to 0 2. evaluate condition: count < 3 is true, so… 3. execute statement: prints count = 0 4. execute statement: increment count to 1
While Loop Action int count = 0; while ( count < 3 ) { System.out.println( “count = “ + count ); count++; } 1. initialize count to 0 2. evaluate condition: count < 3 is true, so… 3. execute statement: prints count = 0 4. execute statement: increment count to 1 5. evaluate condition: count < 3 is true, so…
While Loop Action int count = 0; while ( count < 3 ) { System.out.println( “count = “ + count ); count++; } 1. initialize count to 0 2. evaluate condition: count < 3 is true, so… 3. execute statement: prints count = 0 4. execute statement: increment count to 1 5. evaluate condition: count < 3 is true, so… 6. execute statement: prints count = 1
While Loop Action int count = 0; while ( count < 3 ) { System.out.println( “count = “ + count ); count++; } 1. initialize count to 0 2. evaluate condition: count < 3 is true, so… 3. execute statement: prints count = 0 4. execute statement: increment count to 1 5. evaluate condition: count < 3 is true, so… 6. execute statement: prints count = 1 7. execute statement: increment count to 2
int count = 0; while ( count < 3 ) { System.out.println( “count = “ + count ); count++; } While Loop Action 1. initialize count to 0 2. evaluate condition: count < 3 is true, so… 3. execute statement: prints count = 0 4. execute statement: increment count to 1 5. evaluate condition: count < 3 is true, so… 6. execute statement: prints count = 1 7. execute statement: increment count to 2 8. evaluate condition: count < 3 is true, so…
int count = 0; while ( count < 3 ) { System.out.println( “count = “ + count ); count++; } While Loop Action 1. initialize count to 0 2. evaluate condition: count < 3 is true, so… 3. execute statement: prints count = 0 4. execute statement: increment count to 1 5. evaluate condition: count < 3 is true, so… 6. execute statement: prints count = 1 7. execute statement: increment count to 2 8. evaluate condition: count < 3 is true, so… 9. execute statement: prints count = 2
int count = 0; while ( count < 3 ) { System.out.println( “count = “ + count ); count++; } While Loop Action 1. initialize count to 0 2. evaluate condition: count < 3 is true, so… 3. execute statement: prints count = 0 4. execute statement: increment count to 1 5. evaluate condition: count < 3 is true, so… 6. execute statement: prints count = 1 7. execute statement: increment count to 2 8. evaluate condition: count < 3 is true, so… 9. execute statement: prints count = execute statement: increment count to 3
int count = 0; while ( count < 3 ) { System.out.println( “count = “ + count ); count++; } While Loop Action 1. initialize count to 0 2. evaluate condition: count < 3 is true, so… 3. execute statement: prints count = 0 4. execute statement: increment count to 1 5. evaluate condition: count < 3 is true, so… 6. execute statement: prints count = 1 7. execute statement: increment count to 2 8. evaluate condition: count < 3 is true, so… 9. execute statement: prints count = execute statement: increment count to evaluate condition: count < 3 is false, so exit the loop
For Loop Syntax for ( initialization; condition; update ) { statement_to_execute; } typically declare and initialize a variable for the loop: int count = 0; executed exactly once, as loop starts
For Loop Syntax build a condition based on the loop variable that eventually becomes false count < 3; evaluated each time before executing statement for ( initialization; condition; update ) { statement_to_execute; }
For Loop Syntax statement that advances the condition toward failure count = count + 1 executed each time after executing statement for ( initialization; condition; update ) { statement_to_execute; }
For Loop Example for ( initialization; condition; update ) { statement_to_execute; } for ( int count = 0; count < 3; count++ ) { System.out.println( "Count = " + count ); }
for ( initialization; condition; update ) { statement_to_execute; } for ( int count = 0; count < 3; count++ ) { System.out.println( "Count = " + count ); } For Loop Example
for ( initialization; condition; update ) { statement_to_execute; } for ( int count = 0; count < 3; count++ ) { System.out.println( "Count = " + count ); } For Loop Example
For Loop Action for ( int count = 0; count < 3; count++ ) System.out.println( "Count = " + count ); 1. initialize count to 0 { }
For Loop Action for ( int count = 0; count < 3; count++ ) System.out.println( "Count = " + count ); 1. initialize count to 0 2. evaluate condition: count < 3 is true, so… { }
For Loop Action for ( int count = 0; count < 3; count++ ) System.out.println( "Count = " + count ); 1. initialize count to 0 2. evaluate condition: count < 3 is true, so… 3. execute statement: prints Count = 0 { }
For Loop Action for ( int count = 0; count < 3; count++ ) System.out.println( "Count = " + count ); 1. initialize count to 0 2. evaluate condition: count < 3 is true, so… 3. execute statement: prints Count = 0 4. update: increment count to 1 { }
For Loop Action for ( int count = 0; count < 3; count++ ) System.out.println( "Count = " + count ); 1. initialize count to 0 2. evaluate condition: count < 3 is true, so… 3. execute statement: prints Count = 0 4. update: increment count to 1 5. evaluate condition: count < 3 is true, so… { }
For Loop Action for ( int count = 0; count < 3; count++ ) System.out.println( "Count = " + count ); 1. initialize count to 0 2. evaluate condition: count < 3 is true, so… 3. execute statement: prints Count = 0 4. update: increment count to 1 5. evaluate condition: count < 3 is true, so… 6. execute statement: prints Count = 1 { }
For Loop Action for ( int count = 0; count < 3; count++ ) System.out.println( "Count = " + count ); 1. initialize count to 0 2. evaluate condition: count < 3 is true, so… 3. execute statement: prints Count = 0 4. update: increment count to 1 5. evaluate condition: count < 3 is true, so… 6. execute statement: prints Count = 1 7. update: increment count to 2 { }
For Loop Action for ( int count = 0; count < 3; count++ ) System.out.println( "Count = " + count ); 1. initialize count to 0 2. evaluate condition: count < 3 is true, so… 3. execute statement: prints Count = 0 4. update: increment count to 1 5. evaluate condition: count < 3 is true, so… 6. execute statement: prints Count = 1 7. update: increment count to 2 8. evaluate condition: count < 3 is true, so… { }
For Loop Action for ( int count = 0; count < 3; count++ ) System.out.println( "Count = " + count ); 1. initialize count to 0 2. evaluate condition: count < 3 is true, so… 3. execute statement: prints Count = 0 4. update: increment count to 1 5. evaluate condition: count < 3 is true, so… 6. execute statement: prints Count = 1 7. update: increment count to 2 8. evaluate condition: count < 3 is true, so… 9. execute statement: prints Count = 2 { }
For Loop Action for ( int count = 0; count < 3; count++ ) System.out.println( "Count = " + count ); 1. initialize count to 0 2. evaluate condition: count < 3 is true, so… 3. execute statement: prints Count = 0 4. update: increment count to 1 5. evaluate condition: count < 3 is true, so… 6. execute statement: prints Count = 1 7. update: increment count to 2 8. evaluate condition: count < 3 is true, so… 9. execute statement: prints Count = update: increment count to 3 { }
For Loop Action for ( int count = 0; count < 3; count++ ) System.out.println( "Count = " + count ); 1. initialize count to 0 2. evaluate condition: count < 3 is true, so… 3. execute statement: prints Count = 0 4. update: increment count to 1 5. evaluate condition: count < 3 is true, so… 6. execute statement: prints Count = 1 7. update: increment count to 2 8. evaluate condition: count < 3 is true, so… 9. execute statement: prints Count = update: increment count to evaluate condition: count < 3 is false, so exit the loop { }
What happens? for ( int count = 0; count > 3; count++ ) System.out.println( "Count = " + count ); { }
What happens? for ( int count = 0; count > 3; count++ ) System.out.println( "Count = " + count ); { }
What happens? for ( int count = 0; count < 10; count = count++) System.out.println( "Count = " + count ); { }
What happens? for ( int count = 0; count < 10; count = count++) System.out.println( "Count = " + count ); infinite loop!! because count++ returns value of count before incrementing { }
What happens? for ( int count = 0; count < 10; ) System.out.println( "Count = " + count ); { }
What happens? for ( int count = 0; count < 10; ) System.out.println( "Count = " + count ); { } infinite loop!! no update, so count is always 0
What happens? for ( int count = 1; count != 10; count += 2) System.out.println( "Count = " + count ); { }
What happens? for ( int count = 1; count != 10; count += 2) System.out.println( "Count = " + count ); { }
What happens? for ( int count = 0; count < 10; count += 2); System.out.println( "Bill Maher" ); { }
What happens? for ( int count = 0; count < 10; count += 2); System.out.println("Bill Maher" ); { } Prints only once because of this!
Equivalent for ( int count = 0; count < 10; count += 2); System.out.println( "Bill Maher" ); { } for ( int count = 0; count < 10; count += 2) ; // do-nothing statement System.out.println( "Bill Maher" ); { }
What happens? for ( int count = 0; count < 10; count += 2); System.out.println( "Count = " + count ); { }
What happens? for ( int count = 0; count < 10; count += 2); System.out.println( "Count = " + count ); { }
Guess the Number
Start Codon: ATG
While Loop or For Loop? String dna = “ATATGCCTG”; if ( dna.substring(0,3).equals(“ATG”) ) if ( dna.substring(1,4).equals(“ATG”) ) if ( dna.substring(2,5).equals(“ATG”) ) if ( dna.substring(3,6).equals(“ATG”) ) if ( dna.substring(4,7).equals(“ATG”) ) if ( dna.substring(5,8).equals(“ATG”) ) if ( dna.substring(6,9).equals(“ATG”) )
String dna = “ATATGCCTG”; if ( dna.substring(0,3).equals(“ATG”) ) if ( dna.substring(1,4).equals(“ATG”) ) if ( dna.substring(2,5).equals(“ATG”) ) if ( dna.substring(3,6).equals(“ATG”) ) if ( dna.substring(4,7).equals(“ATG”) ) if ( dna.substring(5,8).equals(“ATG”) ) if ( dna.substring(6,9).equals(“ATG”) ) While Loop or For Loop?
Building A For Loop String dna = “ATATGCCTG”; for ( ???????; ???????; ??????? ) { if ( dna.substring(i,i+3).equals(“ATG”) ) { // do something magical }
Building A For Loop String dna = “ATATGCCTG”; for ( ???????; ???????; ??????? ) { if ( dna.substring(?,?).equals(“ATG”) ) { // do something magical }
String dna = “ATATGCCTG”; for ( ???????; ???????; ??????? ) { if ( dna.substring(?,?).equals(“ATG”) ) { // do something magical } Building A For Loop
String dna = “ATATGCCTG”; for ( int i = 0; ???????; ??????? ) { if ( dna.substring(?,?).equals(“ATG”) ) { // do something magical } Building A For Loop
String dna = “ATATGCCTG”; for ( int i = 0; i < 7; ??? ) { if ( dna.substring(?,?).equals(“ATG”) ) { // do something magical } Building A For Loop
String dna = “ATATGCCTG”; for ( int i = 0; i <= dna.length()-3; ????? ) { if ( dna.substring(?,?).equals(“ATG”) ) { // do something magical } Building A For Loop
String dna = “ATATGCCTG”; for ( int i = 0; i <= dna.length()-3; ????? ) { if ( dna.substring(?,?).equals(“ATG”) ) { // do something magical } Building A For Loop
String dna = “ATATGCCTG”; for ( int i = 0; i <= dna.length()-3; i++ ) { if ( dna.substring(?,?).equals(“ATG”) ) { // do something magical } Building A For Loop
String dna = “ATATGCCTG”; for ( int i = 0; i <= dna.length()-3; i++ ) { if ( dna.substring(i,i+3).equals(“ATG”) ) { // do something magical } Building A For Loop
if ( dna.substring(0,3).equals(“ATG”) ) … if ( dna.substring(1,4).equals(“ATG”) ) … if ( dna.substring(2,5).equals(“ATG”) ) … if ( dna.substring(3,6).equals(“ATG”) ) … if ( dna.substring(4,7).equals(“ATG”) ) … if ( dna.substring(5,8).equals(“ATG”) ) … if ( dna.substring(6,9).equals(“ATG”) ) … for ( int i = 0; i <= dna.length()-3; i++ ) { if ( dna.substring(i,i+3).equals(“ATG”) ) { // do something magical } Building A For Loop
for ( int i = 0; i < 7; i++ ) { if ( dna.substring(i,i+3).equals(“ATG”) ) { // do something magical } Write An Equivalent While Loop
Digital Images
for ( int row = 0; row < image.getNumRows(); row = row + 1 ) { for ( int col = 0; col < image.getNumCols(); col = col + 1 ) { image.getPixelAt( row, col ).brightenByAmt( 10 ); }
Digital Images for ( int row = 0; row < image.getNumRows(); row = row + 1 ) { for ( int col = 0; col < image.getNumCols(); col = col + 1 ) { image.getPixelAt( row, col ).brightenByAmt( 10 ); } row = 0, col loops through values 0…3 row = 1, col loops through values 0…3 row = 2, col loops through values 0…3 row = 3, col loops through values 0…3