Understanding Loops in Java Programming - Examples & Syntax
740 likes | 833 Vues
Explore how loops work in Java programming with syntax examples for while and for loops, including explanations and code snippets. Enhance your coding skills!
Understanding Loops in Java Programming - Examples & Syntax
E N D
Presentation Transcript
CMSC 150Loops CS 150: Fri 20 Jan 2012
Representing DNA AGTCCAGTGTCAA
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 ( 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”) )
Loop Syntax • while ( condition) { statement; } • for ( initialization; condition; update) { statement; }
Loop Syntax Use while when you don’t know in advance the # of times to loop • while ( condition) { 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 timebefore executing statement
While Loop Example • while ( condition ) { statement_to_execute; } • intcount = 0; while ( count < 3 ) { System.out.println( “count = “ + count); count++; }
While Loop Action intcount = 0; while ( count < 3) { System.out.println( “count = “ + count ); count++; } • initialize count to 0
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…
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
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
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…
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
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
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…
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
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
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
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 timebefore executing statement
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
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 ); { } • initialize count to 0
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…
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
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
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…
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
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
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…
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
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
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
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 ); { }