+ All Categories
Home > Documents > CMSC 150 Loops

CMSC 150 Loops

Date post: 24-Feb-2016
Category:
Upload: arvin
View: 28 times
Download: 0 times
Share this document with a friend
Description:
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. - PowerPoint PPT Presentation
74
CMSC 150 LOOPS CS 150: Fri 20 Jan 2012
Transcript
Page 1: CMSC 150 Loops

CMSC 150LOOPS

CS 150: Fri 20 Jan 2012

Page 2: CMSC 150 Loops

Representing DNA

AGTCCAGTGTCAA

Page 3: CMSC 150 Loops

Start Codon: ATG

Page 4: CMSC 150 Loops

Start Codon: ATG

Page 5: CMSC 150 Loops

Consider in JavaString dna = “AGTCCAGTGTCAA”;

Page 6: CMSC 150 Loops

Consider in JavaString dna = “AGTCCAGTGTCAA”;

if ( dna.substring(0,3).equals(“ATG”) )

Page 7: CMSC 150 Loops

String dna = “AGTCCAGTGTCAA”;

if ( dna.substring(0,3).equals(“ATG”) )if ( dna.substring(1,4).equals(“ATG”) )

Consider in Java

Page 8: CMSC 150 Loops

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

Page 9: CMSC 150 Loops

Consider in JavaString 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”) )

Page 10: CMSC 150 Loops

Loops

Page 11: CMSC 150 Loops

Loop Syntax

while ( condition ) {

statement; }

for ( initialization; condition; update )

{

statement; }

Page 12: CMSC 150 Loops

Loop Syntax

while ( condition ) {

statement; }

for ( initialization; condition; update )

{

statement; }

Use while when you

don’t know in advance

the # of times to loop

Page 13: CMSC 150 Loops

Loop Syntax

while ( condition ) {

statement; }

for ( initialization; condition; update )

{

statement; }

Use for when you know in advance the # of times to

loop

Page 14: CMSC 150 Loops

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

Page 15: CMSC 150 Loops

While Loop Example while ( condition ) {

statement_to_execute; }

int count = 0; while ( count < 3 ) { System.out.println( “count = “ + count); count++;}

Page 16: CMSC 150 Loops

While Loop Actionint count = 0;

while ( count < 3 )

{System.out.println( “count = “ + count );count++;

}

1. initialize count to 0

Page 17: CMSC 150 Loops

While Loop Actionint count = 0;

while ( count < 3 )

{System.out.println( “count = “ + count );count++;

}

1. initialize count to 02. evaluate condition: count < 3 is true, so…

Page 18: CMSC 150 Loops

While Loop Actionint count = 0;

while ( count < 3 )

{System.out.println( “count = “ + count );count++;

}

1. initialize count to 02. evaluate condition: count < 3 is true, so…3. execute statement: prints count = 0

Page 19: CMSC 150 Loops

While Loop Actionint count = 0;

while ( count < 3 )

{System.out.println( “count = “ + count );count++;

}

1. initialize count to 02. evaluate condition: count < 3 is true, so…3. execute statement: prints count = 04. execute statement: increment count to 1

Page 20: CMSC 150 Loops

While Loop Actionint count = 0;

while ( count < 3 )

{System.out.println( “count = “ + count );count++;

}

1. initialize count to 02. evaluate condition: count < 3 is true, so…3. execute statement: prints count = 04. execute statement: increment count to 15. evaluate condition: count < 3 is true, so…

Page 21: CMSC 150 Loops

While Loop Actionint count = 0;

while ( count < 3 )

{System.out.println( “count = “ + count );count++;

}

1. initialize count to 02. evaluate condition: count < 3 is true, so…3. execute statement: prints count = 04. execute statement: increment count to 15. evaluate condition: count < 3 is true, so…6. execute statement: prints count = 1

Page 22: CMSC 150 Loops

While Loop Actionint count = 0;

while ( count < 3 )

{System.out.println( “count = “ + count );count++;

}

1. initialize count to 02. evaluate condition: count < 3 is true, so…3. execute statement: prints count = 04. execute statement: increment count to 15. evaluate condition: count < 3 is true, so…6. execute statement: prints count = 17. execute statement: increment count to 2

Page 23: CMSC 150 Loops

int count = 0;

while ( count < 3 )

{System.out.println( “count = “ + count );count++;

}

While Loop Action

1. initialize count to 02. evaluate condition: count < 3 is true, so…3. execute statement: prints count = 04. execute statement: increment count to 15. evaluate condition: count < 3 is true, so…6. execute statement: prints count = 17. execute statement: increment count to 28. evaluate condition: count < 3 is true, so…

Page 24: CMSC 150 Loops

int count = 0;

while ( count < 3 )

{System.out.println( “count = “ + count );count++;

}

While Loop Action

1. initialize count to 02. evaluate condition: count < 3 is true, so…3. execute statement: prints count = 04. execute statement: increment count to 15. evaluate condition: count < 3 is true, so…6. execute statement: prints count = 17. execute statement: increment count to 28. evaluate condition: count < 3 is true, so…9. execute statement: prints count = 2

Page 25: CMSC 150 Loops

int count = 0;

while ( count < 3 )

{System.out.println( “count = “ + count );count++;

}

While Loop Action

1. initialize count to 02. evaluate condition: count < 3 is true, so…3. execute statement: prints count = 04. execute statement: increment count to 15. evaluate condition: count < 3 is true, so…6. execute statement: prints count = 17. execute statement: increment count to 28. evaluate condition: count < 3 is true, so…9. execute statement: prints count = 210. execute statement: increment count to 3

Page 26: CMSC 150 Loops

int count = 0;

while ( count < 3 )

{System.out.println( “count = “ + count );count++;

}

While Loop Action

1. initialize count to 02. evaluate condition: count < 3 is true, so…3. execute statement: prints count = 04. execute statement: increment count to 15. evaluate condition: count < 3 is true, so…6. execute statement: prints count = 17. execute statement: increment count to 28. evaluate condition: count < 3 is true, so…9. execute statement: prints count = 210. execute statement: increment count to 311. evaluate condition: count < 3 is false, so exit the loop

Page 27: CMSC 150 Loops

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

Page 28: CMSC 150 Loops

For Loop Syntax

build a condition based on the loop variable thateventually becomes false

count < 3;

evaluated each time before executing statement

for ( initialization; condition; update )

{ statement_to_execute; }

Page 29: CMSC 150 Loops

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

Page 30: CMSC 150 Loops

For Loop Example for ( initialization; condition; update ) {

statement_to_execute; }

for ( int count = 0; count < 3; count++ ) {

System.out.println( "Count = " + count );

}

Page 31: CMSC 150 Loops

for ( initialization; condition; update ) {

statement_to_execute; }

for ( int count = 0; count < 3; count++ ) {

System.out.println( "Count = " + count );

}

For Loop Example

Page 32: CMSC 150 Loops

for ( initialization; condition; update ) {

statement_to_execute; }

for ( int count = 0; count < 3; count++ ) {

System.out.println( "Count = " + count );

}

For Loop Example

Page 33: CMSC 150 Loops

For Loop Actionfor ( int count = 0; count < 3; count++ )

System.out.println( "Count = " + count );

1. initialize count to 0

{}

Page 34: CMSC 150 Loops

For Loop Actionfor ( int count = 0; count < 3; count++ )

System.out.println( "Count = " + count );

1. initialize count to 02. evaluate condition: count < 3 is true, so…

{}

Page 35: CMSC 150 Loops

For Loop Actionfor ( int count = 0; count < 3; count++ )

System.out.println( "Count = " + count );

1. initialize count to 02. evaluate condition: count < 3 is true, so…3. execute statement: prints Count = 0

{}

Page 36: CMSC 150 Loops

For Loop Actionfor ( int count = 0; count < 3; count++ )

System.out.println( "Count = " + count );

1. initialize count to 02. evaluate condition: count < 3 is true, so…3. execute statement: prints Count = 04. update: increment count to 1

{}

Page 37: CMSC 150 Loops

For Loop Actionfor ( int count = 0; count < 3; count++ )

System.out.println( "Count = " + count );

1. initialize count to 02. evaluate condition: count < 3 is true, so…3. execute statement: prints Count = 04. update: increment count to 15. evaluate condition: count < 3 is true, so…

{}

Page 38: CMSC 150 Loops

For Loop Actionfor ( int count = 0; count < 3; count++ )

System.out.println( "Count = " + count );

1. initialize count to 02. evaluate condition: count < 3 is true, so…3. execute statement: prints Count = 04. update: increment count to 15. evaluate condition: count < 3 is true, so…6. execute statement: prints Count = 1

{}

Page 39: CMSC 150 Loops

For Loop Actionfor ( int count = 0; count < 3; count++ )

System.out.println( "Count = " + count );

1. initialize count to 02. evaluate condition: count < 3 is true, so…3. execute statement: prints Count = 04. update: increment count to 15. evaluate condition: count < 3 is true, so…6. execute statement: prints Count = 17. update: increment count to 2

{}

Page 40: CMSC 150 Loops

For Loop Actionfor ( int count = 0; count < 3; count++ )

System.out.println( "Count = " + count );

1. initialize count to 02. evaluate condition: count < 3 is true, so…3. execute statement: prints Count = 04. update: increment count to 15. evaluate condition: count < 3 is true, so…6. execute statement: prints Count = 17. update: increment count to 28. evaluate condition: count < 3 is true, so…

{}

Page 41: CMSC 150 Loops

For Loop Actionfor ( int count = 0; count < 3; count++ )

System.out.println( "Count = " + count );

1. initialize count to 02. evaluate condition: count < 3 is true, so…3. execute statement: prints Count = 04. update: increment count to 15. evaluate condition: count < 3 is true, so…6. execute statement: prints Count = 17. update: increment count to 28. evaluate condition: count < 3 is true, so…9. execute statement: prints Count = 2

{}

Page 42: CMSC 150 Loops

For Loop Actionfor ( int count = 0; count < 3; count++ )

System.out.println( "Count = " + count );

1. initialize count to 02. evaluate condition: count < 3 is true, so…3. execute statement: prints Count = 04. update: increment count to 15. evaluate condition: count < 3 is true, so…6. execute statement: prints Count = 17. update: increment count to 28. evaluate condition: count < 3 is true, so…9. execute statement: prints Count = 210. update: increment count to 3

{}

Page 43: CMSC 150 Loops

For Loop Actionfor ( int count = 0; count < 3; count++ )

System.out.println( "Count = " + count );

1. initialize count to 02. evaluate condition: count < 3 is true, so…3. execute statement: prints Count = 04. update: increment count to 15. evaluate condition: count < 3 is true, so…6. execute statement: prints Count = 17. update: increment count to 28. evaluate condition: count < 3 is true, so…9. execute statement: prints Count = 210. update: increment count to 311. evaluate condition: count < 3 is false, so exit the loop

{}

Page 44: CMSC 150 Loops

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

{}

Page 45: CMSC 150 Loops

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

{}

Page 46: CMSC 150 Loops

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

}

Page 47: CMSC 150 Loops

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

{}

Page 48: CMSC 150 Loops

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

{}

Page 49: CMSC 150 Loops

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

{}

infinite loop!!

no update, so count is always 0

Page 50: CMSC 150 Loops

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

{}

Page 51: CMSC 150 Loops

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

{}

Page 52: CMSC 150 Loops

What happens?for ( int count = 0; count < 10; count += 2);

System.out.println( "Bill Maher" );{}

Page 53: CMSC 150 Loops

What happens?for ( int count = 0; count < 10; count += 2);

System.out.println("Bill Maher" );{}

Prints only once because

of this!

Page 54: CMSC 150 Loops

Equivalentfor ( 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" );

{}

Page 55: CMSC 150 Loops

What happens?for ( int count = 0; count < 10; count += 2);

System.out.println( "Count = " + count );

{}

Page 56: CMSC 150 Loops

What happens?for ( int count = 0; count < 10; count += 2);

System.out.println( "Count = " + count );

{}

Page 57: CMSC 150 Loops

Guess the Number

Page 58: CMSC 150 Loops

Start Codon: ATG

Page 59: CMSC 150 Loops

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

Page 60: CMSC 150 Loops

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?

Page 61: CMSC 150 Loops

Building A For LoopString dna = “ATATGCCTG”;

for ( ???????; ???????; ??????? ){if ( dna.substring(i,i+3).equals(“ATG”) ){

// do something magical}

}

Page 62: CMSC 150 Loops

Building A For LoopString dna = “ATATGCCTG”;

for ( ???????; ???????; ??????? ){if ( dna.substring(?,?).equals(“ATG”) ){

// do something magical}

}

Page 63: CMSC 150 Loops

String dna = “ATATGCCTG”;

for ( ???????; ???????; ??????? ){if ( dna.substring(?,?).equals(“ATG”) ){

// do something magical}

}

Building A For Loop

Page 64: CMSC 150 Loops

String dna = “ATATGCCTG”;

for ( int i = 0; ???????; ??????? ){if ( dna.substring(?,?).equals(“ATG”) ){

// do something magical}

}

Building A For Loop

Page 65: CMSC 150 Loops

String dna = “ATATGCCTG”;

for ( int i = 0; i < 7; ??? ){if ( dna.substring(?,?).equals(“ATG”) ){

// do something magical}

}

Building A For Loop

Page 66: CMSC 150 Loops

String dna = “ATATGCCTG”;

for ( int i = 0; i <= dna.length()-3; ????? ){if ( dna.substring(?,?).equals(“ATG”) ){

// do something magical}

}

Building A For Loop

Page 67: CMSC 150 Loops

String dna = “ATATGCCTG”;

for ( int i = 0; i <= dna.length()-3; ????? ){if ( dna.substring(?,?).equals(“ATG”) ){

// do something magical}

}

Building A For Loop

Page 68: CMSC 150 Loops

String dna = “ATATGCCTG”;

for ( int i = 0; i <= dna.length()-3; i++ ){if ( dna.substring(?,?).equals(“ATG”) ){

// do something magical}

}

Building A For Loop

Page 69: CMSC 150 Loops

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

Page 70: CMSC 150 Loops

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

Page 71: CMSC 150 Loops

for ( int i = 0; i < 7; i++ )

{

if ( dna.substring(i,i+3).equals(“ATG”) ){

// do something magical

}

}

Write An Equivalent While Loop

Page 72: CMSC 150 Loops

Digital Images

Page 73: CMSC 150 Loops

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

}

0

1

2

3

0 1 2 3

Page 74: CMSC 150 Loops

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

}

0

1

2

3

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


Recommended