(c)2003 E.S.Boese1 Loops Chapter 13 - Student. (c)2003 E.S.Boese 2 Repetition Statements Repetition statements allow us to execute a statement ________.

Slides:



Advertisements
Similar presentations
CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay.
Advertisements

Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
June 10, 2015ICS102: while & do-while1 while and do-while Statements.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
(c) 2003 E.S.Boese1 Conditionals Chapter 10 - Student.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Loops –For For Reading for this Lecture, L&L, Part of 5.8.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
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.
Flow of Control Loops – Chapter 3.2. Java Loop Statements: Outline the while Statement the do-while Statement the for Statement.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
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.
Chapter 4: Control Structures II
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Chapter 5 Loops.
CIS 234: LOOPS Adapted from materials by Dr. Donald Bell, 2000 (updated April 2007)
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Chapter 4: Control Structures II
Repetition Statements while and do while loops
Chapter 5: Control Structures II
Chapter 4 Control Structures: Part I 1 3 “ There is No goto in Java ” Structured programming: the building blocks There are 3 different kinds.
Chapter 5 – Part 3 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 Outline The if Statement and Conditions Other Conditional.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Java the UML Way version Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
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:
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
CMSC 150 LOOPS CS 150: Fri 20 Jan Representing DNA AGTCCAGTGTCAA.
Catie Welsh February 9,  Friday - No Lab! ◦ Bring questions on Project 2  Lab 3 due on Friday 2.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
Repetition Statements b Repetition statements allow us to execute a statement multiple times repetitively b They are often simply referred to as loops.
Flow of Control (2) : Loops Clark Savage Turner, J.D., Ph.D. Some lecture slides have been adapted from those developed.
Loops Tonga Institute of Higher Education. Introduction Programs need to be able to execute tasks repeatedly. Use loops to repeat actions  For Loop 
Programming in Java (COP 2250) Lecture 12 & 13 Chengyong Yang Fall, 2005.
Java I--Copyright © Tom Hunter. Chapter 4 Control Structures: Part I.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
LESSON 5 Loop Control Structure. Loop Control Structure  Operation made over and over again.  Iterate statement.
Topic : While, For, Do-While Loop Guided By : Branch : Batch :
Chapter 4 Repetition Statements (loops)
REPETITION CONTROL STRUCTURE
Chapter 5: Control Structures II
The switch Statement, and Introduction to Looping
Chapter 6 More Conditionals and Loops
Chapter 5: Control Structures II
Loop Structures.
Repetition-Counter control Loop
Java Programming: Guided Learning with Early Objects
Chapter 2.2 Control Structures (Iteration)
Chapter 5: Control Structures II
Manipulating Pictures, Arrays, and Loops part 2
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Java I.
Debugging October 3, 2007 ComS 207: Programming I (in Java)
Outline Altering flow of control Boolean expressions
LOOPS BY: LAUREN & ROMEO.
Repetition Control Structure
Chapter 2.2 Control Structures (Iteration)
Debugging October 4, 2006 ComS 207: Programming I (in Java)
Repetition Statements
Chap 7. Advanced Control Statements in Java
Presentation transcript:

(c)2003 E.S.Boese1 Loops Chapter 13 - Student

(c)2003 E.S.Boese 2 Repetition Statements Repetition statements allow us to execute a statement ________ times Often they are referred to as _________ Like conditional statements, they are controlled by ____________ expressions Java has three kinds of repetition statements:  the while loop  the do loop  the for loop The programmer should choose the right kind of loop for the situation Slide from Lewis and Loftus Java Software Solutions

(c)2003 E.S.Boese 3 while while( booleanExpression ) { statements; } continually executes the statements until the booleanExpression becomes ________ the body of a while loop will execute _______________ statements true false condition evaluated

(c)2003 E.S.Boese 4 while Example int count = 1; while (count <= 5) { System.out.println (count); count = count + 1; }

(c)2003 E.S.Boese 5 while Example import java.awt.*; import javax.swing.*; public class whileEx extends JApplet { JTextArea ta_display = new JTextArea( 15,40 ); public void init() { int value = 1; while( value <= 100 ) { value= value +10; ta_display.append( "" + value + "\n" ); } add( ta_display ); }

(c)2003 E.S.Boese 6 while Example with Animation /* Demonstrate loops with animation */ import java.awt.*; import javax.swing.*; public class Animate extends JApplet { int x, y, size, move, speed, count; public void paint ( Graphics g ) { x = 0; y = 50; size = 20;// width and height same move = 1; //num pixels to move each time thru loop count = 0; speed = ; // slow down loop while ( count < 5000 ) { g.clearRect ( x, y, size, size); // erase space x = x + move; // new x coodinate, y stays same g.fillRect ( x, y, size, size ); // did we hit the edge of the applet? if ( x + size > getWidth( ) ) move = -move;// if so, go back for ( int i=0; i<speed; i++ ) ; // slow down painting so we can see it count = count + 1; }

(c)2003 E.S.Boese 7 for for( initialization ; booleanExpression ; incrementer ) { statements; } the body of a for loop will execute _______________

(c)2003 E.S.Boese 8 for for( initialization ; booleanExpression ; incrementer ) { statements; } statements true condition evaluated false increment initialization

(c)2003 E.S.Boese 9 for Example Write a loop that sums the values between 1 and 100 int sum = 0; System.out.println( sum );

(c)2003 E.S.Boese 10 for Example on a list: To print out all the items in a list, use a for loop and the method getItem on the list object import java.awt.*; import javax.swing.*; public class List2TA extends JApplet { JList list; DefaultListModel model; JTextArea textarea; public void init( ) { setLayout( new FlowLayout( ) ); setupList( ); textarea = new JTextArea( 5,10 ); add(textarea); addListItemsToTextarea( ); } public void setupList( ) { model = new DefaultListModel( ); list = new JList(model); model.addElement( "Milk" ); model.addElement( "Cookies" ); model.addElement( "Eggs" ); add(list); } // end init method public void addListItemsToTextarea( ) { // getSize returns the num of items in the list for( int i=0; i<model.getSize( ); i++ ) { // grab the item at index i textarea.append( (String )model.get(i) ); textarea.append( "\n" ); }

(c)2003 E.S.Boese 11 for Example in paint method public void paint( Graphics g ) { int stripes = 0; int width = 30; int height = 100; for (int x=0; stripes < 13; x=x+width) { if (stripes%2 == 0) { g.setColor(Color.RED); } else { g.setColor(Color.CYAN); } g.fillRect(x, 0, width, height); stripes = stripes + 1; } // end while loop } // end paint method }

(c)2003 E.S.Boese 12 Which loop to use? if ( you know the # of iterations )  use a ________ loop else if ( statements should be done at least once )  use a ________ loop else  use a ________ loop

(c)2003 E.S.Boese 13 Careful! Off-by-one error Missing squigglys Semi-colon syntax Infinite loops

(c)2003 E.S.Boese 14 Careful! Off-by-one error Want to add values 1 through 10 to textarea: for( int i=1; i<10; i=i+1 ) textarea.append( String.valueOf(i) ); What gets added to the textarea?

(c)2003 E.S.Boese 15 Careful! Missing squigglys int x = 10; while( x < 100 ) textarea.append( String.valueOf(x) ); textarea.append(“\n” ); x = x + 10; should be:

(c)2003 E.S.Boese 16 Careful! Semi-colon syntax You won’t get an error message for putting a ; at the end of your while or for loop – but you probably won’t get the results you expect! for( int x =1; x<=10; x=x+1 ) ; { textarea.append( “Hello” ); } The semi-colon declares an _______________ of the loop!

(c)2003 E.S.Boese 17 Infinite Loop Infinite Loops  loop with a conditional that never becomes ____________ while( true ) drinkCoffee(); for( int i=1; i>0; i++ ) walkOn( ); x = 1; while( x < 10 ); x = x + 5; y = 1; while( y < 10 ) ta.append( y ); y++;

(c)2003 E.S.Boese 18 Summary while loop for loop do… while loop When to use each loop Animation Common Errors