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
Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
Advertisements

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.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 4.1, 5.1.
Loops Chapter 4. It repeats a set of statements while a condition is true. while (condition) { execute these statements; } “while” structures.
Arrays, Loops weeks 4-6 (change from syllabus for week 6) Chapter 4.
LAB 10.
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
Building Java Programs
CSC 1051 M.A. Papalaskari, Villanova University Repetition CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing.
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
1 Fencepost loops “How do you build a fence?”. 2 The fencepost problem Problem: Write a class named PrintNumbers that reads in an integer called max and.
Boolean expressions, part 2: Logical operators. Previously discussed Recall that there are 2 types of operators that return a boolean result (true or.
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:
1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops; Procedural Design reading: 5.1 – 5.2; 4.5.
Miscellaneous topics Chapter 2 Savitch. Miscellaneous topics Standard output using System.out Input using Scanner class.
1 while loops. 2 Definite loops definite loop: A loop that executes a known number of times.  The for loops we have seen so far are definite loops. We.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
1 BUILDING JAVA PROGRAMS CHAPTER 2 Pseudocode and Scope.
1 Building Java Programs Chapter 3: Introduction to Parameters and Objects These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I 12/11/20151.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 4: Loop Figures and Constants reading:
Java iteration statements ● Iteration statements are statements which appear in the source code only once, but it execute many times. ● Such kind of statements.
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 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.
Building Java Programs Program Logic and Indefinite Loops.
The for loop.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I 2/4/20161.
Boolean expressions, part 1: Compare operators. Compare operators Compare operators compare 2 numerical values and return a Boolean (logical) value A.
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.
Copyright 2008 by Pearson Education 1 Nested loops reading: 2.3 self-check: exercises: videos: Ch. 2 #4.
1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 5.1 – 5.2.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 5: Control Structures II
Repetition-Counter control Loop
TK1114 Computer Programming
Building Java Programs
Building Java Programs
Building Java Programs
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
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
Suggested self-checks:
Building Java Programs
Repetition Statements
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 Lecture 2-3: Loop Figures and Constants reading:
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)

Computer Work 1.Write a program that reads an integer, and displays (using System.out.println ) all numbers from the number down to 0. For example, if 8 is entered, the program should display numbers 7, 6, 5, 4, 3, 2, 1, 0, in this order and with each number in one line. 2.Write a program that reads an integer, and displays (using System.out.println ) all even numbers between 0 and the number received, and finally the number of 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 program that prints “yes” or “no” to indicate whether an integer parameter named number is a prime number. Use a for loop. 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.

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

Prime Number Program import java.util.Scanner; public class Prime { public static void main(String[ ] args) { Scanner kbd = new Scanner(System.in); System.out.print("Enter a number: "); int nbr = kbd.nextInt( ); if (nbr < 2) // only look at positive numbers { System.out.println("no"); System.exit(0); // means leave the program right now } for (int i=2;i<nbr;i++) { if (nbr % i == 0) { System.out.println("no"); System.exit(0); } System.out.println("yes"); } for loop Leaves loop and program outside the loop

Embedded loops Write a program to draw a square box after reading in its size 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

import java.util.Scanner; public class DrawStars { public static void main(String[ ] args) { Scanner kbd = new Scanner(System.in); System.out.print("Enter a number: "); int nbr = kbd.nextInt( ); if (nbr < 2) // only look at positive numbers System.exit(0); // means leave the program right now // do first line for (int i=0;i<nbr;i++) System.out.print("*"); // add enter System.out.println( ); // nothing in parentheses // continued on next page

// print n-2 internal lines for (int i=0;i<nbr-2;i++) // i is never used. it only counts { // nbr of times through loop // print first character on line System.out.print("*"); // print number of spaces minus one * on each end for (int j=0;j<nbr-2;j++) // embedded loop for one internal line System.out.print(" "); // single space in between quotes // print last * on line -- also put enter in there System.out.println("*"); } // and outer for loop that does all internal lines // print last line. same as first line for (int i=0;i<nbr;i++) System.out.print("*"); System.out.println( ); // nothing in parentheses } // end main } // end class

Write a loop that prints a times table 0-2: a Embedded Loop Practice 1.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.