Presentation is loading. Please wait.

Presentation is loading. Please wait.

CMSC 150 LOOPS CS 150: Fri 20 Jan 2012. Representing DNA AGTCCAGTGTCAA.

Similar presentations


Presentation on theme: "CMSC 150 LOOPS CS 150: Fri 20 Jan 2012. Representing DNA AGTCCAGTGTCAA."— Presentation transcript:

1 CMSC 150 LOOPS CS 150: Fri 20 Jan 2012

2 Representing DNA AGTCCAGTGTCAA

3 Start Codon: ATG

4

5 Consider in Java String dna = “AGTCCAGTGTCAA”;

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

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

8 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

9 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  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

13 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

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 time before executing statement

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

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

17 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…

18 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

19 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

20 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…

21 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

22 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

23 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…

24 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

25 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 10. execute statement: increment count to 3

26 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 10. execute statement: increment count to 3 11. 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 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; }

29 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; }

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 ( initialization; condition; update ) { statement_to_execute; }  for ( int count = 0; count < 3; count++ ) { System.out.println( "Count = " + count ); } For Loop Example

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

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

34 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… { }

35 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 { }

36 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 { }

37 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… { }

38 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 { }

39 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 { }

40 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… { }

41 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 { }

42 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 10. update: increment count to 3 { }

43 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 10. update: increment count to 3 11. 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 ); { }

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

52 What happens? for ( int count = 0; count < 10; count += 2); System.out.println( "Bill Maher" ); { }

53 What happens? for ( int count = 0; count < 10; count += 2); System.out.println("Bill Maher" ); { } Prints only once because of this!

54 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" ); { }

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

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

57 Guess the Number

58 Start Codon: ATG

59 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”) )

60 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?

61 Building A For Loop String dna = “ATATGCCTG”; for ( ???????; ???????; ??????? ) { if ( dna.substring(i,i+3).equals(“ATG”) ) { // do something magical }

62 Building A For Loop String dna = “ATATGCCTG”; for ( ???????; ???????; ??????? ) { if ( dna.substring(?,?).equals(“ATG”) ) { // do something magical }

63 String dna = “ATATGCCTG”; for ( ???????; ???????; ??????? ) { if ( dna.substring(?,?).equals(“ATG”) ) { // do something magical } Building A For Loop

64 String dna = “ATATGCCTG”; for ( int i = 0; ???????; ??????? ) { if ( dna.substring(?,?).equals(“ATG”) ) { // do something magical } Building A For Loop

65 String dna = “ATATGCCTG”; for ( int i = 0; i < 7; ??? ) { if ( dna.substring(?,?).equals(“ATG”) ) { // do something magical } Building A For Loop

66 String dna = “ATATGCCTG”; for ( int i = 0; i <= dna.length()-3; ????? ) { if ( dna.substring(?,?).equals(“ATG”) ) { // do something magical } Building A For Loop

67 String dna = “ATATGCCTG”; for ( int i = 0; i <= dna.length()-3; ????? ) { if ( dna.substring(?,?).equals(“ATG”) ) { // do something magical } Building A For Loop

68 String dna = “ATATGCCTG”; for ( int i = 0; i <= dna.length()-3; i++ ) { if ( dna.substring(?,?).equals(“ATG”) ) { // do something magical } Building A For Loop

69 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

70 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

71 for ( int i = 0; i < 7; i++ ) { if ( dna.substring(i,i+3).equals(“ATG”) ) { // do something magical } Write An Equivalent While Loop

72 Digital Images

73 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 ); } 01230123 0 1 2 3

74 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 ); } 01230123 0 1 2 3 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


Download ppt "CMSC 150 LOOPS CS 150: Fri 20 Jan 2012. Representing DNA AGTCCAGTGTCAA."

Similar presentations


Ads by Google