Loops Chapter 4. It repeats a set of statements while a condition is true. while (condition) { execute these statements; } “while” structures.

Slides:



Advertisements
Similar presentations
Overloading Having more than one method with the same name is known as overloading. Overloading is legal in Java as long as each version takes different.
Advertisements

Intro Programming By Trevor Decker Team 1743 By Trevor Decker Team 1743.
Computer Science 1620 Loops.
Loops Notes adapted from Dr. Flores. It repeats a set of statements while a condition is true. while (condition) { execute these statements; } “while”
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Copyright 2006 by Pearson Education 1 reading: 4.1 Cumulative sum.
Arrays, Loops weeks 4-6 (change from syllabus for week 6) Chapter 4.
Loops Chapter 4. It repeats a set of statements while a condition is true. while (condition) { execute these statements; } “while” structures.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Chapter 4: Control Structures II
Building Java Programs
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
Loops and Iteration for Statements, while Statements and do-while Statements.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-3: Loop Figures and Constants reading: self-checks: 27 exercises:
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.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
1 BUILDING JAVA PROGRAMS CHAPTER 2 Pseudocode and Scope.
Control Structures II: Repetition.  Learn about repetition (looping) control structures  Explore how to construct and use count-controlled, sentinel-controlled,
Control Structures - Selections - Repetitions/iterations (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
1 Building Java Programs Chapter 3: Introduction to Parameters and Objects These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 4: Loop Figures and Constants reading:
Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?
ITI 1120 Lab #3 Tracing and Branching Contributors: G. Arbez, M. Eid, D. Inkpen, A. Williams, D. Amyot.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
CSE 201 – Elementary Computer Programming 1 Extra Exercises Sourceshttp://
Printing with for Loops To print a character multiple times, use a for loop. for (int j = 1; j
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 4.1, 5.1.
Programming With Java ICS201 University Of Ha’il1 Chapter 11 Recursion.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
Copyright 2008 by Pearson Education 1 Nested loops reading: 2.3 self-check: exercises: videos: Ch. 2 #4.
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
Chapter 5: Control Structures II
Building Java Programs
Building Java Programs
Primitive Data, Variables, Loops (Maybe)
Repetition-Counter control Loop
CiS 260: App Dev I Chapter 4: Control Structures II.
Lecture Notes – Week 3 Lecture-2
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs Chapter 2
Building Java Programs
Building Java Programs
Building Java Programs
Chapter 2 Programming Basics.
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs Chapter 2
Question 1a) What is printed by the following Java program? int s;
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
REPETITION Why Repetition?
Presentation transcript:

loops Chapter 4

It repeats a set of statements while a condition is true. while (condition) { execute these statements; } “while” structures

It repeats a set of statements while a condition is true. while ( condition ) { execute these statements; } The dynamics of “while” Evaluate condition: if TRUE go to 2 If FALSE go to 3 Execute statements, and then go to 1 Continue with next statement. The dynamics of “while” Evaluate condition: if TRUE go to 2 If FALSE go to 3 Execute statements, and then go to 1 Continue with next statement

“while” structures It repeats a set of statements while a condition is true. int speedLimit = 55; int speed = 0; while ( speed <= speedLimit ) { speed = speed + 1; } // since we don’t want a ticket… speed = speed - 1; What is the value of “speed” after the last statement? A.55 B.54 C.56 D.0 E.none of the above

“while” structures It repeats a set of statements while a condition is true. int speedLimit = 55; int speed = 0; while ( speed < speedLimit ) { speed = speed + 1; } // since we don’t want a ticket… speed = speed - 1; initialize variables in conditional initialize variables in conditional 1 modify variables in conditional modify variables in conditional 2

“while” structures: Exercises Determine the output of the following methods: public void foo1() { int i=0; while (i <= 20) { System.out.println( i ); i = i + 4; // or i+= 4; } public void foo2() { int i = 20; while (i > 0) { i = i / 2; System.out.println( i ); } A B C D E.none of the above (on separate lines) A B C D E.none of the above (on separate lines)

Do on the board 1.Write a method named “countDown” that receives an integer parameter named “number”, and displays (using System.out.println ) all numbers from the number down to 0. For example, if the parameter was 8, the method should display numbers 7, 6, 5, 4, 3, 2, 1, 0, in this order and with each number in one line. 2.Write a method named “countEven” that receives an integer parameter named “number”, displays (using System.out.println ) all even numbers between 0 and the number received, and returns a integer value with the number of even numbers displayed. For example, if the parameter was 8, the method should display numbers 2, 4 and 6, in this order and with each number in one line, and return a value of 3 (which is how many even numbers were between 0 and 8).

“for” structures It (also) repeats statements while a condition is true. Works best when loop will be repeated a known number of times initial statement loop condition modify statement The dynamics of “for” 1. Initialize condition variables. 2. Evaluate loop condition: if TRUE go to 3 If FALSE go to 5 3. Execute statements; then go to 4 4. Modify condition variables; then go to 2 5. Continue with next statements. The dynamics of “for” 1. Initialize condition variables. 2. Evaluate loop condition: if TRUE go to 3 If FALSE go to 5 3. Execute statements; then go to 4 4. Modify condition variables; then go to 2 5. Continue with next statements. for ( ; ; ) { statements; }

prime using for Write a method named “prime” that returns a boolean value indicating whether an integer parameter named number is a prime number. How to write Java: Answer the questions: Exactly how do you do this? What is a step by step description? WRITE IN ENGLISH. NOT Java or Draw pictures. or both Debug the English: Trace through with numbers. especially with going through the loop 0 times, 1 time, >= 1 times. Fix the ENGLISH.

Write a method that returns a boolean value indicating whether an integer parameter named number is a prime number. 1.Brainstorm. How do you figure out if a number is prime? prime(11) prime(12). Talk to each other. 2.Write an algorithm for that 3.Translate the algorithm to Java

Write a method that returns a boolean value indicating whether an integer parameter named number is a prime number. public boolean prime(int nbr) // assumes nbr > 0 { if (nbr < 2) // only look at positive numbers return false; for (int i=2;i<nbr;i++) if (i % nbr == 0) return false; // else // return true means there is no loop return true; } for loop leaves loop and method outside the loop

“for” structures: Exercises 1.Write a method named “factorial” that calculates the factorial of an integer parameter named “number” (where factorial is the multiplication of all numbers from 1 to number-1). The method should return an integer number with the result of the factorial, and it should work only with positive numbers (return 0 in the case of non-positive parameter numbers). 2.Write a call to factorial that prints the result of 4!

Embedded loops Write a method to draw a square box given its size as a parameter 1.Write the first line (for loop printing n stars) 2.println 3.for each internal line (n-2 of them) a.print a * b.print n-2 spaces c.println a * 4.print the last line

public void printBox(int size) { if (size < 2) return; // boxes must be 2d for (int i=0;i<size;i++) System.out.print('*'); // first line System.out.println( ); for (int i=0;i<size-2;i++) // print internal lines { System.out.print('*'); // loop within loop for (int j=0;j<size-2;j++) // print internal spaces System.out.print(' '); System.out.println('*'); } for (int i=0;i<size;i++) System.out.print('*'); // last line System.out.println( ); }

Embedded Loop Practice Write a loop that prints a times table 0-2: Write the first line 2.for each number on the left (0*, 1*, 2*) a.print the left number (the LHS of the table) b.for each number on the right (*0, *1, *2) i.print the product c.println.