Chapter 4 Ch 1 – Introduction to Computers and Java Flow of Control Loops 1.

Slides:



Advertisements
Similar presentations
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 6 Repetition Statements.
Advertisements

Control Structures.
Chapter 2: Basic Elements of C++
Repetition Statements Perform the same task repeatedly Allow the computer to do the tedious, boring things.
Iterations for loop. Outcome Introduction to for loop The use of for loop instead of while loop Nested for loops.
February 12, 2013 COMP Introduction to Programming For Statement and More Loops Haohan Li TR 11:00 – 12:15, SN 011 Spring 2013.
Looping Structures: Do Loops
Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
CSC 221: Computer Programming I Fall 2001  conditional repetition  repetition for: simulations, input verification, input sequences, …  while loops.
Repeating Structures For Loops. Repeating Structures Tasks we need to complete are often very repetitive. Once a task has been mastered, repeating it.
CS0004: Introduction to Programming Repetition – Do Loops.
WARM-UP: MON, APR 7 What are loops used for in programming? What are at least 2 different kinds of loops?
Repetition Chapter 4: Control structures. Introduction to OOPDr. S. GANNOUNI & Dr. A. TOUIRPage 2 Loop Statements After reading and studying this Section,
June 10, 2015ICS102: while & do-while1 while and do-while Statements.
1 Lecture 11:Control Structures II (Repetition) (cont.) Introduction to Computer Science Spring 2006.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
An Introduction to Programming with C++ Fifth Edition Chapter 1 An Introduction to Programming.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
Chapter 5 Control Structures: Loops 5.1 The while Loop The while loop is probably the most frequently used loop construct. The while loop is a conditional.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.1 Chapter 5 Loops.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Counting Loops.
Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition.
Chapter 6: Repetition Continued. 2 Validity Checks What’s weak about the following code ? do { s1 = JOptionPane.showInputDialog (“Enter a number: ”);
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
Introduction to Computing Concepts Note Set 14. What if… You had to print “I love Java” to the screen 125 times. How? 125 lines of ▫ System.out.println(“I.
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
CPSC 233 Tutorial 5 February 2 th /3 th, Java Loop Statements A portion of a program that repeats a statement or a group of statements is called.
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
The Repetition control structure using while loop.
Chapter 9 Repetition.
Unit-1 Introduction to Java
Java for Beginners.
Java's for Statement.
Week 4 – Chapter 3 Repetition.
CS150 Introduction to Computer Science 1
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
Chapter 5 Repetition.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Determinate Loops with the
Java for Beginners University Greenwich Computing At School DASCO
Alice in Action with Java
Review for Test1.
Recursion Chapter 11.
LRobot Game.
An Introduction to Control Structures
Chapter 8 The Loops By: Mr. Baha Hanene.
Chapter 1: Introduction
Computers & Programming Languages
Repetition Control Structure
Introduction to Repetition Structures
Alternate Version of STARTING OUT WITH C++ 4th Edition
Review for Test1.
An Introduction to Control Structures
Java Programming: Chapter 9: Recursion Second Edition
ICT Gaming Lesson 2.
An Introduction to Programming with C++ Fifth Edition
do/while Selection Structure
Introduction to Computer Science
Introduction to Computer Science
Repetition Statements (Loops) - 2
Looping Structures.
Presentation transcript:

Chapter 4 Ch 1 – Introduction to Computers and Java Flow of Control Loops 1

Chapter Java Loop Statements 4.2 Programming with Loops 4.3 Graphics Supplement 2

4.1 Java Loop Statements 3 repeat...repeat...repeat

Repetition simplifies coding 4

How would you sum up 1 through 100? 5 sum = sum = 1 sum += 2... sum += 100

How about 1 through 100,00? 6 We need a more efficient way to accomplish this task. One that can scale up easily!

The for Statement 7 sum = 0 num = 1 sum += num num <= 100num > 100 num++ end

for Deconstructed int sum = 0; for (int num = 1; num <= 100; num++) { sum += num; } 8 Is this solution scalable? int sum = 0; for (int num = 1; num <= 10000; num++) { sum += num; } initial step condition step update step

Application Deconstructed package sumnumbers; import java.util.Scanner; public class SumNumbers { public static void main(String[] args) { int upperBound; int sumOfNumbers = 0; Scanner keyboard = new Scanner(System.in); System.out.println("I will add numbers from 1 to n."); System.out.print("Please enter n: "); upperBound = keyboard.nextInt(); 9

Application Deconstructed for (int num = 1; num <= upperBound; num++) { sumOfNumbers += num; } //end for System.out.println(""); System.out.println("The sum of 1 to " + upperBound + " is " + sumOfNumbers); } //end main() } //end SumNumbers 10

Recap: for Use to repeat a fixed number of times Excellent choice for counting problems Very concise – all steps on one line 11

Sometimes you may not want to repeat an action at all 12

Summing a list of numbers 13 First decide what ends the list Let's say that -1 will signal the end of the input Now, could the list be empty? It could, if user enters -1 as the first number

The while Statement 14 get num sum += num num != -1num == -1 get num end

while Deconstructed int num; int sumOfNumericList = 0; System.out.print("Enter number of -1 to quit: "); num = keyboard.nextInt(); while(num != -1) { sumOfNumericList += num; System.out.print("Enter number of -1 to quit: "); num = keyboard.nextInt(); } 15 initial step condition step update step

Application Deconstructed package sumnumericlist; import java.util.Scanner; public class SumNumericList { public static void main(String[] args) { int num; int sumOfNumericList = 0; Scanner keyboard = new Scanner(System.in); 16 System.out.print("Enter a number or -1 to quit: "); num = keyboard.nextInt(); while (num != -1) { sumOfNumericList += num; System.out.print("Enter a number or -1 to quit: "); num = keyboard.nextInt(); } //end while

Application Deconstructed System.out.println("The sum of the numbers is " + sumOfNumericList); } //end main() } //end SumNumericList 17

Recap: while Use to repeat 0 or more times Number of iterations is unknown before hand Test before execution 18

And sometimes you may want to repeat an action at least once 19

Sum numbers until you reach Add next number if sum is less than 100 Stop when sum becomes greater than or equal to 100

The do-while Statement 21 sum = 0 get num sum < 100 sum >= 100 sum += num end

do-while Deconstructed int num; int sumToReach = 0; do { System.out.print("Enter number: "); num = keyboard.nextInt(); sumToReach += num; } while(sumToReach < 100); 22 initial step condition step update step

Application Deconstructed package sumtoreach; import java.util.Scanner; public class SumToReach { public static void main(String[] args) { int num; int sumToReach = 0; Scanner keyboard = new Scanner(System.in); do { System.out.print("Enter a number: "); num = keyboard.nextInt(); sumToReach += num; } while(sumToReach < 100); 23

Application Deconstructed System.out.println("The sum is " + sumToReach); } //end main() } //end SumToReach 24

Recap: do-while Use to repeat 1 or more times Number of iterations is unknown before hand Test after execution 25

The for-each Statement 26 Allows you to cycle through an enumeration or collection, in its entirety

for-each Deconstructed enum Suit { CLUBS, DIAMONDS, HEARTS, SPADES } 27 for ( Suit nextSuit : Suit.values() ) { System.out.print(nextSuit + " "); }

Application Deconstructed package foreachenum; public class ForEachEnum { enum Suit { CLUBS, DIAMONDS, HEARTS, SPADES } public static void main(String[] args) { for ( Suit nextSuit : Suit.values() ) { System.out.print(nextSuit + " "); } //end for System.out.println(""); } //end main() } //end ForEachEnum 28

4.2 Programming with Loops 29

In Class Examples 30 for: Count down User enters upper bound, lower bound and step down. Program outputs list. while: Number reversal User enters integer to reverse. Program displays reversed number.

In Class Examples 31 while: Structure violation (implement in class) int num = 0; int count = 0; while (num != -1) { read number count number }

Recap: Repetition Strive to always choose the most appropriate loop for the task Each loop has a purpose and a structure Do not violate either one 32

4.3 Graphic Supplement 33

Applet Deconstructed package fallingletters; import java.awt.Color; import javax.swing.JApplet; import java.awt.Graphics; public class FallingLetters extends JApplet { public static final int LETTER_COUNT = 100; int windowWidth; int public void paint(Graphics canvas) { windowWidth = this.getWidth(); windowHeight = this.getHeight(); 34

Applet public void paint(Graphics canvas) { windowWidth = this.getWidth(); windowHeight = this.getHeight(); int x; int y; char c; int red; int green; int blue; 35

Applet Deconstructed for (int letter = 1; letter <= LETTER_COUNT; letter++) { x = (int)(Math.random() * windowWidth); y = (int) (Math.random() * windowHeight); c = (char)( (int) (Math.random() * 26) + 65); red = (int)(Math.random() * 256); green = (int)(Math.random() * 256); blue = (int)(Math.random() * 256); canvas.setColor(new Color(red, green, blue)); canvas.drawString(String.valueOf(c), x, y); }// end for }// end paint() }// end FallingLetters 36

Applet Deconstructed 37