Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.

Slides:



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

Chapter 7 - Iteration. Chapter Goals Program repitiation statements – or loops – with the for, while, and do-while statements Program repitiation statements.
Week 5: Loops 1.  Repetition is the ability to do something over and over again  With repetition in the mix, we can solve practically any problem that.
LOOP / REPETITION while loop. for loop do/while loop We assume that loops are not meant to be infinite. That is, there should always be a way out of the.
Repeating Actions While and For Loops
June 10, 2015ICS102: while & do-while1 while and do-while Statements.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
Introduction to Computers and Programming Lecture 9: For Loops New York University.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Chapter 5: Control Structures II (Repetition)
Loops – While, Do, For Repetition Statements Introduction to Arrays
Recognizing Patterns Counting: continually updating a value by a fixed amount Counting raindrops int dropCount = 0; //Raindrop counter while (dropCount.
©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.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 6: Repetition  Some additional operators increment and decrement.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Repetition Statements.
©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
Chapter 5: Control Structures II (Repetition)
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Chapter 4 Loops Write code that prints out the numbers Very often, we want to repeat a (group of) statement(s). In C++, we have 3 major ways of.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
C++ for Engineers and Scientists, Third Edition1 Objectives In this chapter, you will learn about: Basic loop structures while loops Interactive while.
Computer Science Department LOOPS. Computer Science Department Loops Loops Cause a section of your program to be repeated a certain number of times. The.
Control Structures II (Repetition). Objectives In this chapter you will: Learn about repetition (looping) control structures Explore how to construct.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Repetition Statements while and do while loops
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Repetition Statements (Loops) The do while Loop The last iteration structure in C++ is the do while loop. A do while loop repeats a statement or.
Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Chapter 6 - Repetition. while Loop u Simplest loop u Two parts: test expression and loop body u Pre-tested loop –Execute loop body if test true –Bypass.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
REPETITION MTS3033 OBJECT ORIENTED PROGRAMMING 1.
CMSC 150 LOOPS CS 150: Fri 20 Jan Representing DNA AGTCCAGTGTCAA.
1 Week 9 Loops. 2 Repetition: Loops l Structure: »Usually some initialization code »body of loop »loop termination condition l Several logical organizations.
While ( number
Catie Welsh February 9,  Friday - No Lab! ◦ Bring questions on Project 2  Lab 3 due on Friday 2.
Repetition Statements b Repetition statements allow us to execute a statement multiple times repetitively b They are often simply referred to as loops.
Loops Tonga Institute of Higher Education. Introduction Programs need to be able to execute tasks repeatedly. Use loops to repeat actions  For Loop 
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied. Suppose you want to execute some code/s 10 times.
Lecture 4b Repeating With Loops
Chapter 4 Repetition Statements (loops)
Lecture 6 Repetition Richard Gesick.
Control Structures II (Repetition)
CiS 260: App Dev I Chapter 4: Control Structures II.
Programming Fundamentals
Looping and Repetition
MSIS 655 Advanced Business Applications Programming
Outline Altering flow of control Boolean expressions
LRobot Game.
Introduction to Object-Oriented Programming with Java--Wu
Control Statements Loops.
Repetition Control Structure
3.5- The while Statement The while statement has the following syntax:
Chapter 6: Repetition Statements
Repetition Statements (Loops) - 2
Control Statements Loops.
PROGRAM FLOWCHART Iteration Statements.
CS2011 Introduction to Programming I Loop Statements (I)
Looping and Repetition
Presentation transcript:

Repetition Statements

 Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements

While statements  while statements have the following syntax:  while ( )  The statements execute 0 or more times based on the boolean expression  If the expression is true, the statements will be executed and then the expression will be tested again.  When the expression evaluates to false, the loop is exited

While statements  int sum = 0, number = 1;  while (number <= 100){ sum += number; number++; } int sum = 0, number = 1;

While statements  int sum = 0, number = 1;  while (number <= 100){ sum += number; number++; } int sum = 0, number = 1; number <= 100 True False

While statements  int sum = 0, number = 1;  while (number <= 100){ sum += number; number++; } int sum = 0, number = 1; number <= 100 True False

While statements  int sum = 0, number = 1;  while (number <= 100){ sum += number; number++; } int sum = 0, number = 1; number <= 100 True False sum += number; number++;

While statements  int sum = 0, number = 1;  while (number <= 100){ sum += number; number++; } int sum = 0, number = 1; number <= 100 True False sum += number; number++;

While statements  int sum = 0, number = 1;  while (number <= 100){ sum += number; number++; } int sum = 0, number = 1; number <= 100 True False sum += number; number++;

While statements  int sum = 0, number = 1;  while (number <= 100){ sum += number; number++; } condition True False Statements

While statements  Questions??

While statements  InputBox in = new InputBox(mw); int x = in.getInteger(“Enter a positive number”); while (x < 0) x = in.getInteger(“Enter a positive number”); InputBox in = new InputBox(mw); int x = in.getInteger(“Enter a positive number”);

While statements  InputBox in = new InputBox(mw); int x = in.getInteger(“Enter a positive number”); while (x < 0) x = in.getInteger(“Enter a positive number”); x < 0 InputBox in = new InputBox(mw); int x = in.getInteger(“Enter a positive number”);

While statements  InputBox in = new InputBox(mw); int x = in.getInteger(“Enter a positive number”); while (x < 0) x = in.getInteger(“Enter a positive number”); x < 0 InputBox in = new InputBox(mw); int x = in.getInteger(“Enter a positive number”); False

While statements  InputBox in = new InputBox(mw); int x = in.getInteger(“Enter a positive number”); while (x < 0) x = in.getInteger(“Enter a positive number”); x < 0 InputBox in = new InputBox(mw); int x = in.getInteger(“Enter a positive number”); False x = in.getInteger(“Enter a positive number”); True

While statements  InputBox in = new InputBox(mw); int x = in.getInteger(“Enter a positive number”); while (x < 0) x = in.getInteger(“Enter a positive number”); x < 0 InputBox in = new InputBox(mw); int x = in.getInteger(“Enter a positive number”); False x = in.getInteger(“Enter a positive number”); True

while statements Write a method that adds the numbers x through y

do while statements  do while loops are a variation of the while loop  Syntax: do { } while ( );  This is the same as a while loop, except the statements are always executed at least once.

do while statements while (<boolean expression) do{ } while ( ); condition True False Statements condition False Statements True

While statements  InputBox in = new InputBox(mw); int x = in.getInteger(“Enter a positive number”); while (x < 0) x = in.getInteger(“Enter a positive number”);  int x; InputBox in = new InputBox(mw); do{ x = in.getInteger(“Enter a positive number”); } while (x < 0); while loop do while loop

While statements  Questions?

for statements  for statements have four parts  Initalization – executed once (initially)  Condition – boolean expression  Statements – executed based on condition  Increment – executed after statements and before condition is reevaluated  They have the following syntax:  for( [ initialization ]; [ condition ]; [increment] )  Example:  sum = 0;  for( int i = 0; i < 100; i++ ) sum = sum + i;

for statements for( [ initialization ]; [ condition ]; [increment] ) initalization condition false statements true increment

for statements  sum = 0;  for( int i = 0; i < 100; i++ ) sum = sum + i;

for statements  sum = 0;  for( int i = 0; i < 100; i++ ) sum = sum + i; sum = 0;

for statements  sum = 0;  for( int i = 0; i < 100; i++ ) sum = sum + i; sum = 0; int i = 0;

for statements  sum = 0;  for( int i = 0; i < 100; i++ ) sum = sum + i; sum = 0; int i = 0; i < 100 true false

for statements  sum = 0;  for( int i = 0; i < 100; i++ ) sum = sum + i; sum = 0; int i = 0; i < 100 true false sum = sum + i;

for statements  sum = 0;  for( int i = 0; i < 100; i++ ) sum = sum + i; sum = 0; int i = 0; i < 100 true false sum = sum + i; i++;

for statements Questions????

Nested for statements  Nested for statements are just like regular for statements  They are very useful for many processing tasks  Multiplication Table Example:  OutputBox out = new OutputBox(mw);  out.show();  for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); }

Nested for statements OutputBox out = new OutputBox(mw); out.show(); for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); }

Nested for statements OutputBox out = new OutputBox(mw); out.show(); for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); } i 1

Nested for statements OutputBox out = new OutputBox(mw); out.show(); for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); } i 1

Nested for statements OutputBox out = new OutputBox(mw); out.show(); for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); } i 1 j 1

Nested for statements OutputBox out = new OutputBox(mw); out.show(); for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); } i 1 j 1

Nested for statements OutputBox out = new OutputBox(mw); out.show(); for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); } i 1 j 1 1 _

Nested for statements OutputBox out = new OutputBox(mw); out.show(); for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); } i 1 j 2 1 _

Nested for statements OutputBox out = new OutputBox(mw); out.show(); for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); } i 1 j 2 1 _

Nested for statements OutputBox out = new OutputBox(mw); out.show(); for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); } i 1 j _

Nested for statements OutputBox out = new OutputBox(mw); out.show(); for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); } i 1 j _

Nested for statements OutputBox out = new OutputBox(mw); out.show(); for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); } i 1 j _

Nested for statements OutputBox out = new OutputBox(mw); out.show(); for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); } i 1 j _

Nested for statements OutputBox out = new OutputBox(mw); out.show(); for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); } i 1 j _

Nested for statements OutputBox out = new OutputBox(mw); out.show(); for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); } i 1 j _

Nested for statements OutputBox out = new OutputBox(mw); out.show(); for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); } i 1 j _

Nested for statements OutputBox out = new OutputBox(mw); out.show(); for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); } i 1 j _

Nested for statements OutputBox out = new OutputBox(mw); out.show(); for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); } i 1 j _

Nested for statements OutputBox out = new OutputBox(mw); out.show(); for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); } i _

Nested for statements OutputBox out = new OutputBox(mw); out.show(); for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); } i _

Nested for statements OutputBox out = new OutputBox(mw); out.show(); for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); } i 2 j _

Nested for statements OutputBox out = new OutputBox(mw); out.show(); for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); } i 2 j _

Nested for statements OutputBox out = new OutputBox(mw); out.show(); for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); } i 2 j _

Nested for statements OutputBox out = new OutputBox(mw); out.show(); for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); } i 2 j _

Nested for statements OutputBox out = new OutputBox(mw); out.show(); for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); } i 2 j _

Nested for statements OutputBox out = new OutputBox(mw); out.show(); for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); } i 2 j _

Nested for statements OutputBox out = new OutputBox(mw); out.show(); for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); } i 2 j _

Nested for statements OutputBox out = new OutputBox(mw); out.show(); for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); } i 2 j _

Nested for statements OutputBox out = new OutputBox(mw); out.show(); for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); } i 2 j _

Nested for statements OutputBox out = new OutputBox(mw); out.show(); for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); } i 2 j _

Nested for statements OutputBox out = new OutputBox(mw); out.show(); for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); } i 2 j _

Nested for statements OutputBox out = new OutputBox(mw); out.show(); for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); } i 2 j _

Nested for statements OutputBox out = new OutputBox(mw); out.show(); for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); } i 2 j _

Nested for statements OutputBox out = new OutputBox(mw); out.show(); for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); } i 2 j _

Nested for statements OutputBox out = new OutputBox(mw); out.show(); for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); } i 2 j _

Nested for statements OutputBox out = new OutputBox(mw); out.show(); for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); } i 10 j At the end, you get this >

Nested for statements Questions???

Loops  There are two ways loops can be used  Sentinel controlled loops  Loop is exited based on a sentinel value  while (x != 0) // do something  Count controlled  Loop is exited when a certain count is reached  for (x = 0; x < 10; x++) // do something

Pitfalls  Infinite loops  Make sure loops can exit!  Real number addition 1.0/ / /3.0 =? 1.0  for( x = 0; x != 1.0; x+= )  In general avoid using real numbers as counter variables  Use, >= and not !=  for(x = 0; x < 3; x++);

Pitfalls  Off by one errors  for(int i = 0; i <= 10; i++)  // this will execute 11 times  for(int i=1; i < 10; i++)  // this will execute 9 times  for(int i = 1; i <= 10; i++)  // this will execute 10 times  for(int i =0; I < 10; i++)  // this will execute 10 times

Pitfalls  Off by one errors  for(int i = 0; i <= 10; i++)  // this will execute 11 times  for(int i=1; i < 10; i++)  // this will execute 9 times  for(int i = 1; i <= 10; i++)  // this will execute 10 times  for(int i =0; I < 10; i++)  // this will execute 10 times Use zero based indexing

Pitfalls  Make sure the loop body contains a statement that will eventually cause the loop to terminate  Make sure the loop repeats exactly the correct number of times  If you want to execute the loop body N times, then go 0 to < n or 1 to <= n