CMSC 150 Introduction TO Computing

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
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 (
if (
if (
if (
if (
...
if (
dna.substring(0,3).equals(“ATG”)
dna.substring(1,4).equals(“ATG”)
dna.substring(2,5).equals(“ATG”)
dna.substring(3,6).equals(“ATG”)
dna.substring(4,7).equals(“ATG”)
)
)
)
)
)
dna.substring(10,12).equals(“ATG”) )
Loops
Loop Syntax

while ( condition )
{
statement;
}

for ( initialization; condition; update )
{
statement;
}
Loop Syntax

while ( condition )
{
Use while when you
don’t know in advance
the # of times to loop
statement;
}

for ( initialization; condition; update )
{
statement;
}
Loop Syntax

while ( condition )
{
statement;
}

Use for when you
know in advance
the # of times to loop
for ( initialization; condition; update )
{
statement;
}
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.
2.
initialize count to 0
evaluate condition:
count < 3 is true, so…
While Loop Action
int count = 0;
while ( count < 3 )
{
System.out.println( “count = “ + count );
count++;
}
1.
2.
3.
initialize count to 0
evaluate condition:
execute statement:
count < 3 is true, so…
prints count = 0
While Loop Action
int count = 0;
while ( count < 3 )
{
System.out.println( “count = “ + count );
count++;
}
1.
2.
3.
4.
initialize count to 0
evaluate condition:
execute statement:
execute statement:
count < 3 is true, so…
prints count = 0
increment count to 1
While Loop Action
int count = 0;
while ( count < 3 )
{
System.out.println( “count = “ + count );
count++;
}
1.
2.
3.
4.
5.
initialize count to 0
evaluate condition:
execute statement:
execute statement:
evaluate condition:
count < 3 is true, so…
prints count = 0
increment count to 1
count < 3 is true, so…
While Loop Action
int count = 0;
while ( count < 3 )
{
System.out.println( “count = “ + count );
count++;
}
1.
2.
3.
4.
5.
6.
initialize count to 0
evaluate condition:
execute statement:
execute statement:
evaluate condition:
execute statement:
count < 3 is true, so…
prints count = 0
increment count to 1
count < 3 is true, so…
prints count = 1
While Loop Action
int count = 0;
while ( count < 3 )
{
System.out.println( “count = “ + count );
count++;
}
1.
2.
3.
4.
5.
6.
7.
initialize count to 0
evaluate condition:
execute statement:
execute statement:
evaluate condition:
execute statement:
execute statement:
count < 3 is true, so…
prints count = 0
increment count to 1
count < 3 is true, so…
prints count = 1
increment count to 2
While Loop Action
int count = 0;
while ( count < 3 )
{
System.out.println( “count = “ + count );
count++;
}
1.
2.
3.
4.
5.
6.
7.
8.
initialize count to 0
evaluate condition:
execute statement:
execute statement:
evaluate condition:
execute statement:
execute statement:
evaluate condition:
count < 3 is true, so…
prints count = 0
increment count to 1
count < 3 is true, so…
prints count = 1
increment count to 2
count < 3 is true, so…
While Loop Action
int count = 0;
while ( count < 3 )
{
System.out.println( “count = “ + count );
count++;
}
1.
2.
3.
4.
5.
6.
7.
8.
9.
initialize count to 0
evaluate condition:
execute statement:
execute statement:
evaluate condition:
execute statement:
execute statement:
evaluate condition:
execute statement:
count < 3 is true, so…
prints count = 0
increment count to 1
count < 3 is true, so…
prints count = 1
increment count to 2
count < 3 is true, so…
prints count = 2
While Loop Action
int count = 0;
while ( count < 3 )
{
System.out.println( “count = “ + count );
count++;
}
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
initialize count to 0
evaluate condition:
execute statement:
execute statement:
evaluate condition:
execute statement:
execute statement:
evaluate condition:
execute statement:
execute statement:
count < 3 is true, so…
prints count = 0
increment count to 1
count < 3 is true, so…
prints count = 1
increment count to 2
count < 3 is true, so…
prints count = 2
increment count to 3
While Loop Action
int count = 0;
while ( count < 3 )
{
System.out.println( “count = “ + count );
count++;
}
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
initialize count to 0
evaluate condition:
execute statement:
execute statement:
evaluate condition:
execute statement:
execute statement:
evaluate condition:
execute statement:
execute statement:
evaluate condition:
count < 3 is true, so…
prints count = 0
increment count to 1
count < 3 is true, so…
prints count = 1
increment count to 2
count < 3 is true, so…
prints count = 2
increment count to 3
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

for ( initialization; condition; update )
{
statement_to_execute;
}
build a condition based on the loop variable that
eventually becomes false
count < 3;
evaluated each time before executing statement
For Loop Syntax

for ( initialization; condition; update )
{
statement_to_execute;
}
statement that advances the condition toward failure
count = count + 1
executed each time after executing statement
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 ( 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 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.
2.
3.
initialize count to 0
evaluate condition:
execute statement:
count < 3 is true, so…
prints Count = 0
For Loop Action
for ( int count = 0; count < 3; count++ )
{
System.out.println( "Count = " + count );
}
1.
initialize count to 0
3.
evaluate condition:
execute statement:
count < 3 is true, so…
prints Count = 0
4.
update:
increment count to 1
2.
For Loop Action
for ( int count = 0; count < 3; count++ )
{
System.out.println( "Count = " + count );
}
1.
initialize count to 0
3.
evaluate condition:
execute statement:
count < 3 is true, so…
prints Count = 0
4.
update:
increment count to 1
5.
evaluate condition:
count < 3 is true, so…
2.
For Loop Action
for ( int count = 0; count < 3; count++ )
{
System.out.println( "Count = " + count );
}
1.
initialize count to 0
3.
evaluate condition:
execute statement:
count < 3 is true, so…
prints Count = 0
4.
update:
increment count to 1
evaluate condition:
execute statement:
count < 3 is true, so…
prints Count = 1
2.
5.
6.
For Loop Action
for ( int count = 0; count < 3; count++ )
{
System.out.println( "Count = " + count );
}
1.
initialize count to 0
3.
evaluate condition:
execute statement:
count < 3 is true, so…
prints Count = 0
4.
update:
increment count to 1
6.
evaluate condition:
execute statement:
count < 3 is true, so…
prints Count = 1
7.
update:
increment count to 2
2.
5.
For Loop Action
for ( int count = 0; count < 3; count++ )
{
System.out.println( "Count = " + count );
}
1.
initialize count to 0
3.
evaluate condition:
execute statement:
count < 3 is true, so…
prints Count = 0
4.
update:
increment count to 1
6.
evaluate condition:
execute statement:
count < 3 is true, so…
prints Count = 1
7.
update:
increment count to 2
8.
evaluate condition:
count < 3 is true, so…
2.
5.
For Loop Action
for ( int count = 0; count < 3; count++ )
{
System.out.println( "Count = " + count );
}
1.
initialize count to 0
3.
evaluate condition:
execute statement:
count < 3 is true, so…
prints Count = 0
4.
update:
increment count to 1
6.
evaluate condition:
execute statement:
count < 3 is true, so…
prints Count = 1
7.
update:
increment count to 2
evaluate condition:
execute statement:
count < 3 is true, so…
prints Count = 2
2.
5.
8.
9.
For Loop Action
for ( int count = 0; count < 3; count++ )
{
System.out.println( "Count = " + count );
}
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
initialize count to 0
evaluate condition:
execute statement:
update:
evaluate condition:
execute statement:
update:
evaluate condition:
execute statement:
update:
count < 3 is true, so…
prints Count = 0
increment count to 1
count < 3 is true, so…
prints Count = 1
increment count to 2
count < 3 is true, so…
prints Count = 2
increment count to 3
For Loop Action
for ( int count = 0; count < 3; count++ )
{
System.out.println( "Count = " + count );
}
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
initialize count to 0
evaluate condition:
execute statement:
update:
evaluate condition:
execute statement:
update:
evaluate condition:
execute statement:
update:
evaluate condition:
count < 3 is true, so…
prints Count = 0
increment count to 1
count < 3 is true, so…
prints Count = 1
increment count to 2
count < 3 is true, so…
prints Count = 2
increment count to 3
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
if
if
if
if
if
if
(
(
(
(
(
(
(
dna.substring(0,3).equals(“ATG”)
dna.substring(1,4).equals(“ATG”)
dna.substring(2,5).equals(“ATG”)
dna.substring(3,6).equals(“ATG”)
dna.substring(4,7).equals(“ATG”)
dna.substring(5,8).equals(“ATG”)
dna.substring(6,9).equals(“ATG”)
)
)
)
)
)
)
)
While Loop or For Loop?
String dna = “ATATGCCTG”;
if
if
if
if
if
if
if
(
(
(
(
(
(
(
dna.substring(0,3).equals(“ATG”)
dna.substring(1,4).equals(“ATG”)
dna.substring(2,5).equals(“ATG”)
dna.substring(3,6).equals(“ATG”)
dna.substring(4,7).equals(“ATG”)
dna.substring(5,8).equals(“ATG”)
dna.substring(6,9).equals(“ATG”)
)
)
)
)
)
)
)
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
}
}
Building A For Loop
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
}
}
Write An Equivalent While Loop
for ( int i = 0; i < 7; i++ )
{
if ( dna.substring(i,i+3).equals(“ATG”) )
{
// do something magical
}
}
Digital Images
Digital Images
0
1
2
3
0
1
2
3
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
0
0
1
2
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
3
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 );
}
}