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

Slides:



Advertisements
Similar presentations
Repetition Statements Perform the same task repeatedly Allow the computer to do the tedious, boring things.
Advertisements

Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
COMP 14 Introduction to Programming Mr. Joshua Stough February 16, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 6, 2005.
Introduction to Computers and Programming Lecture 9: For Loops New York University.
COMP 14 Introduction to Programming Miguel A. Otaduy May 21, 2004.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
1 Parts of a Loop (reminder) Every loop will always contain three main elements: –Priming: initialize your variables. –Testing: test against some known.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Repetition Statements repeat block of code until a condition is satisfied also called loops Java supports 3 kinds of loops: while statement – repeats a.
©2004 Brooks/Cole Chapter 5 Repetition. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Repetition So far, our programs processed a single set.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Loops –For For Reading for this Lecture, L&L, Part of 5.8.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Loops III Lecture 19, Wed Mar
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Copyright © Texas Education Agency, Computer Programming For Loops.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
COM S 207 While-Loop Statement Instructor: Ying Cai Department of Computer Science Iowa State University
Recursion Examples Fundamentals of CS Case 1: Code /* Recursion: Case 1 */ #include void count (int index); main () { count (0); getchar(); } void count.
© 2004 Pearson Addison-Wesley. All rights reserved February 20, 2006 ‘do’ and ‘for’ loops ComS 207: Programming I (in Java) Iowa State University, SPRING.
Flow Control in Java. Controlling which instruction to execute next Sequential  Similar to walking, one step after another Branching  Similar to a fork.
Chapter 5 Loops.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
Chapter 5: Control Structures II
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
03 August 2004 NLP-AI Java Lecture No. 4 Operators & Decision Constructs Satish Dethe.
Repetition and Iteration ANSI-C. Repetition We need a control instruction to allows us to execute an statement or a set of statements as many times as.
Chapter 5 – Part 3 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 Outline The if Statement and Conditions Other Conditional.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Fundamentals of Software Development IProgramming patterns involving iteration1 Overview LoopsOverview Loops We’ll also see how loops are often combined.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition.
Fundamentals of Software Development 1Slide 1 Loops A loop is:A loop is: –a block of code that executes repeatedly while some condition holds true. Java.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
Catie Welsh February 9,  Friday - No Lab! ◦ Bring questions on Project 2  Lab 3 due on Friday 2.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Designing with While loops.
Structured Programming Structured Programming is writing a program in terms of only 3 basic control structures: sequence selection repetition We have already.
Programming in Java (COP 2250) Lecture 12 & 13 Chengyong Yang Fall, 2005.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Follow up from lab See Magic8Ball.java Issues that you ran into.
Lecture 4 CS140 Dick Steflik. Reading Keyboard Input Import java.util.Scanner – A simple text scanner which can parse primitive types and strings using.
Topic : While, For, Do-While Loop Guided By : Branch : Batch :
Repetition Statements
Loops A loop is: Java provides three forms for explicit loops:
Chapter 4 Repetition Statements (loops)
Chapter 6 More Conditionals and Loops
Karel J Robot Chapter 6.
CiS 260: App Dev I Chapter 4: Control Structures II.
Iteration with While You can say that again.
Debugging October 3, 2007 ComS 207: Programming I (in Java)
Outline Altering flow of control Boolean expressions
Lecture Notes – Week 3 Lecture-2
Chapter 4 Loops Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Debugging October 4, 2006 ComS 207: Programming I (in Java)
Seating “chart” Front - Screen rows Back DOOR.
Repetition Statements
PROGRAM FLOWCHART Iteration Statements.
Building Java Programs
CSCI 1100/1202 February 6, 2002.
‘do’ and ‘for’ loops October 1, 2007 ComS 207: Programming I (in Java)
‘do’ and ‘for’ loops October 2, 2006 ComS 207: Programming I (in Java)
Presentation transcript:

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