1 / 74

CMSC 150 Loops

CMSC 150 Loops. CS 150: Fri 20 Jan 2012. Representing DNA. AGTCCAGTGTCAA. Start Codon : ATG. Start Codon : ATG. Consider in Java. String dna = “AGTCCAGTGTCAA”;. Consider in Java. String dna = “AGTCCAGTGTCAA”; if ( dna.substring(0,3).equals(“ATG”) ). Consider in Java.

arvin
Download Presentation

CMSC 150 Loops

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CMSC 150Loops CS 150: Fri 20 Jan 2012

  2. Representing DNA AGTCCAGTGTCAA

  3. Start Codon: ATG

  4. Start Codon: ATG

  5. Consider in Java String dna = “AGTCCAGTGTCAA”;

  6. Consider in Java String dna = “AGTCCAGTGTCAA”; if ( dna.substring(0,3).equals(“ATG”) )

  7. Consider in Java String dna = “AGTCCAGTGTCAA”; if ( dna.substring(0,3).equals(“ATG”) ) if ( dna.substring(1,4).equals(“ATG”) )

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

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

  10. Loops

  11. Loop Syntax • while ( condition) { statement; } • for ( initialization; condition; update) { statement; }

  12. Loop Syntax Use while when you don’t know in advance the # of times to loop • while ( condition) { statement; } • for ( initialization; condition; update) { statement; }

  13. Loop Syntax • while ( condition) { statement; } Use for when you know in advance the # of times to loop • for ( initialization; condition; update) { statement; }

  14. 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 timebefore executing statement

  15. While Loop Example • while ( condition ) { statement_to_execute; } • intcount = 0; while ( count < 3 ) { System.out.println( “count = “ + count); count++; }

  16. While Loop Action intcount = 0; while ( count < 3) { System.out.println( “count = “ + count ); count++; } • initialize count to 0

  17. While Loop Action intcount = 0; while ( count < 3) { System.out.println( “count = “ + count ); count++; } • initialize count to 0 • evaluate condition: count < 3 is true, so…

  18. While Loop Action intcount = 0; while ( count < 3) { System.out.println( “count = “ + count ); count++; } • initialize count to 0 • evaluate condition: count < 3 is true, so… • execute statement: prints count = 0

  19. While Loop Action intcount = 0; while ( count < 3) { System.out.println( “count = “ + count ); count++; } • initialize count to 0 • evaluate condition: count < 3 is true, so… • execute statement: prints count = 0 • execute statement: increment count to 1

  20. While Loop Action intcount = 0; while ( count < 3) { System.out.println( “count = “ + count ); count++; } • initialize count to 0 • evaluate condition: count < 3 is true, so… • execute statement: prints count = 0 • execute statement: increment count to 1 • evaluate condition: count < 3 is true, so…

  21. While Loop Action intcount = 0; while ( count < 3) { System.out.println( “count = “ + count ); count++; } • initialize count to 0 • evaluate condition: count < 3 is true, so… • execute statement: prints count = 0 • execute statement: increment count to 1 • evaluate condition: count < 3 is true, so… • execute statement: prints count = 1

  22. While Loop Action intcount = 0; while ( count < 3) { System.out.println( “count = “ + count ); count++; } • initialize count to 0 • evaluate condition: count < 3 is true, so… • execute statement: prints count = 0 • execute statement: increment count to 1 • evaluate condition: count < 3 is true, so… • execute statement: prints count = 1 • execute statement: increment count to 2

  23. While Loop Action intcount = 0; while ( count < 3) { System.out.println( “count = “ + count ); count++; } • initialize count to 0 • evaluate condition: count < 3 is true, so… • execute statement: prints count = 0 • execute statement: increment count to 1 • evaluate condition: count < 3 is true, so… • execute statement: prints count = 1 • execute statement: increment count to 2 • evaluate condition: count < 3 is true, so…

  24. While Loop Action intcount = 0; while ( count < 3) { System.out.println( “count = “ + count ); count++; } • initialize count to 0 • evaluate condition: count < 3 is true, so… • execute statement: prints count = 0 • execute statement: increment count to 1 • evaluate condition: count < 3 is true, so… • execute statement: prints count = 1 • execute statement: increment count to 2 • evaluate condition: count < 3 is true, so… • execute statement: prints count = 2

  25. While Loop Action intcount = 0; while ( count < 3) { System.out.println( “count = “ + count ); count++; } • initialize count to 0 • evaluate condition: count < 3 is true, so… • execute statement: prints count = 0 • execute statement: increment count to 1 • evaluate condition: count < 3 is true, so… • execute statement: prints count = 1 • execute statement: increment count to 2 • evaluate condition: count < 3 is true, so… • execute statement: prints count = 2 • execute statement: increment count to 3

  26. While Loop Action intcount = 0; while ( count < 3) { System.out.println( “count = “ + count ); count++; } • initialize count to 0 • evaluate condition: count < 3 is true, so… • execute statement: prints count = 0 • execute statement: increment count to 1 • evaluate condition: count < 3 is true, so… • execute statement: prints count = 1 • execute statement: increment count to 2 • evaluate condition: count < 3 is true, so… • execute statement: prints count = 2 • execute statement: increment count to 3 • evaluate condition: count < 3 is false, so exit the loop

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

  28. For Loop Syntax • for ( initialization; condition; update ) { statement_to_execute; } build a condition based on the loop variable that eventually becomes false count < 3; evaluated each timebefore executing statement

  29. For Loop Syntax • for ( initialization; condition; update ) { statement_to_execute; } statement that advances the condition toward failure count = count + 1 executed each timeafter executing statement

  30. For Loop Example • for ( initialization; condition; update ) { statement_to_execute; } • for ( int count = 0; count < 3; count++ ) { System.out.println( "Count = " + count ); }

  31. For Loop Example • for ( initialization; condition; update ) { statement_to_execute; } • for ( int count = 0; count < 3; count++ ) { System.out.println( "Count = " + count ); }

  32. For Loop Example • for ( initialization; condition; update ) { statement_to_execute; } • for ( int count = 0; count < 3; count++ ) { System.out.println( "Count = " + count ); }

  33. For Loop Action for ( int count = 0; count < 3; count++ ) System.out.println( "Count = " + count ); { } • initialize count to 0

  34. For Loop Action for ( int count = 0; count < 3; count++ ) System.out.println( "Count = " + count ); { } • initialize count to 0 • evaluate condition: count < 3 is true, so…

  35. For Loop Action for ( int count = 0; count < 3; count++ ) System.out.println( "Count = " + count ); { } • initialize count to 0 • evaluate condition: count < 3 is true, so… • execute statement: prints Count = 0

  36. For Loop Action for ( int count = 0; count < 3; count++ ) System.out.println( "Count = " + count ); { } • initialize count to 0 • evaluate condition: count < 3 is true, so… • execute statement: prints Count = 0 • update: increment count to 1

  37. For Loop Action for ( int count = 0; count < 3; count++ ) System.out.println( "Count = " + count ); { } • initialize count to 0 • evaluate condition: count < 3 is true, so… • execute statement: prints Count = 0 • update: increment count to 1 • evaluate condition: count < 3 is true, so…

  38. For Loop Action for ( int count = 0; count < 3; count++ ) System.out.println( "Count = " + count ); { } • initialize count to 0 • evaluate condition: count < 3 is true, so… • execute statement: prints Count = 0 • update: increment count to 1 • evaluate condition: count < 3 is true, so… • execute statement: prints Count = 1

  39. For Loop Action for ( int count = 0; count < 3; count++ ) System.out.println( "Count = " + count ); { } • initialize count to 0 • evaluate condition: count < 3 is true, so… • execute statement: prints Count = 0 • update: increment count to 1 • evaluate condition: count < 3 is true, so… • execute statement: prints Count = 1 • update: increment count to 2

  40. For Loop Action for ( int count = 0; count < 3; count++ ) System.out.println( "Count = " + count ); { } • initialize count to 0 • evaluate condition: count < 3 is true, so… • execute statement: prints Count = 0 • update: increment count to 1 • evaluate condition: count < 3 is true, so… • execute statement: prints Count = 1 • update: increment count to 2 • evaluate condition: count < 3 is true, so…

  41. For Loop Action for ( int count = 0; count < 3; count++ ) System.out.println( "Count = " + count ); { } • initialize count to 0 • evaluate condition: count < 3 is true, so… • execute statement: prints Count = 0 • update: increment count to 1 • evaluate condition: count < 3 is true, so… • execute statement: prints Count = 1 • update: increment count to 2 • evaluate condition: count < 3 is true, so… • execute statement: prints Count = 2

  42. For Loop Action for ( int count = 0; count < 3; count++ ) System.out.println( "Count = " + count ); { } • initialize count to 0 • evaluate condition: count < 3 is true, so… • execute statement: prints Count = 0 • update: increment count to 1 • evaluate condition: count < 3 is true, so… • execute statement: prints Count = 1 • update: increment count to 2 • evaluate condition: count < 3 is true, so… • execute statement: prints Count = 2 • update: increment count to 3

  43. For Loop Action for ( int count = 0; count < 3; count++ ) System.out.println( "Count = " + count ); { } • initialize count to 0 • evaluate condition: count < 3 is true, so… • execute statement: prints Count = 0 • update: increment count to 1 • evaluate condition: count < 3 is true, so… • execute statement: prints Count = 1 • update: increment count to 2 • evaluate condition: count < 3 is true, so… • execute statement: prints Count = 2 • update: increment count to 3 • evaluate condition: count < 3 is false, so exit the loop

  44. What happens? for ( int count = 0; count > 3; count++ ) System.out.println( "Count = " + count ); { }

  45. What happens? for ( int count = 0; count > 3; count++ ) System.out.println( "Count = " + count ); { }

  46. What happens? for ( int count = 0; count < 10; count = count++) System.out.println( "Count = " + count ); { }

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

  48. What happens? for ( int count = 0; count < 10;) System.out.println( "Count = " + count ); { }

  49. What happens? for ( int count = 0; count < 10;) System.out.println( "Count = " + count ); { } infinite loop!! no update, so count is always 0

  50. What happens? for ( int count = 1; count != 10; count += 2) System.out.println( "Count = " + count ); { }

More Related