6.2 for Loops Example: for ( int i = 1; i <= 10; i++ ) { double interest = balance * rate / 100; balance = balance + interest; } Use a for loop.

Slides:



Advertisements
Similar presentations
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. It is common to use two nested loops when filling or searching: for.
Advertisements

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 1 Chapter 6: Iteration 1 Chapter 6 Iteration.
Chapter 3 Implementing Classes. Instance Variables Instance variables store the data of an object; the fields of an object. Instance of a class: an object.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Chapter 9 – Inheritance Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Chapter 2 – An Introduction to Objects and Classes Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Random Number Generation public class Hand { private int card1; private int card2; private int card3; private Random rand; public static final int NO_CARD.
Computer Science A 4 13/3. Goals To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To understand.
Chapter 6 Iteration Goals To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To understand.
Iteration and Loop Statements Horstmann Chapter 7 Loop statements control repeated execution of a block of statements Each time the statements in the block.
Chapter 6  Iteration 1 Chapter 6 Iteration. Chapter 6  Iteration 2 Chapter Goals  To be able to program loops with while and for (sometimes do ) statements.
The for-statement. Different loop-statements in Java Java provides 3 types of loop-statements: 1. The for-statement 2. The while-statement 3. The do-while-statement.
Chapter 6 Iteration.  Executes a block of code repeatedly  A condition controls how often the loop is executed while (condition) statement  Most commonly,
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved For Loops October 16, 2013 Slides by Evan Gallagher.
Chapter 6: Iteration Part 1. To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To understand.
Chapter 6 Iteration. Chapter Goals To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
ICOM 4015 Fall 2008 Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. ICOM 4015: Advanced Programming Lecture 6 Chapter.
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 1 – Introduction ( ปรับปรุง )
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 6 - Loops.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 6 - Loops.
 Executes a block of code repeatedly  A condition controls how often the loop is executed  Most commonly, the statement is a block statement (set of.
Chapter 6: Iteration Part 1. To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To understand.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 6 – Iteration.
Chapter 6 Iteration. Chapter Goals To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To.
Decisions Bush decision making.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Array length = maximum number of elements in array Usually, array is partially filled Need companion variable to keep track of current size Uniform naming.
Chapter 7 – Arrays and Array Lists Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 1.
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING While Loop.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Lesson 7 Iteration Structures. Iteration is the third control structure we will explore. Iteration simply means to do something repeatedly. All iteration.
Chapter 7 Iteration. Chapter Goals To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To.
Slides by Evan Gallagher
Slides by Evan Gallagher
A final variable is a constant
John Woodward A Simple Program – Hello world
Lecture 3 John Woodward.
Data Structures and Algorithms revision
Department of Computer Science
Chapter Six: Iteration
Chapter 4 – Fundamental Data Types
Chapter Goals To implement while, for, and do loops
5.3 Multiple Alternatives (Part 1)
7.6 Common Array Algorithm: Filling
Chapter 1 – Introduction
Repetition-Counter control Loop
Selected Topics From Chapter 6 Iteration
LOOPS.
Something about Java Introduction to Problem Solving and Programming 1.
Chapter Three - Implementing Classes
Chapter Six - Iteration
Chapter 7 Iteration.
class PrintOnetoTen { public static void main(String args[]) {
Chapter 5 – Decisions Big Java by Cay Horstmann
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
while while (condition) { statements }
Use a variable to store a value that you want to use at a later time
4.3 Arithmetic Operators and Mathematical Functions
Chapter 5 – Decisions Big Java by Cay Horstmann
Each object belongs to a class
1.7 Errors Compile-time error: A violation of the programming language rules that is detected by the compiler Example: System.ou.println("Hello, World!);
Parameter: an input to a method
Presentation transcript:

6.2 for Loops Example: for ( int i = 1; i <= 10; i++ ) { double interest = balance * rate / 100; balance = balance + interest; } Use a for loop when a variable runs from a starting value to an ending value with a constant increment or decrement Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

Syntax 6.2 The for Statement Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

for Loop Flowchart Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

Execution of a for Loop Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

for Loops /** * A method to monitor the growth of an investment that * accumulates interest at a fixed annual rate for a given number of years * @param startBalance the starting balance * @param rate the interest rate in percent * @param years the number of years it grows * @return the balance */ public static double calcBalance (double startBalance, double rate, int years){ double balance = startBalance; for (int i = 1; i <= years; i++) { double interest = balance * rate / 100; balance = balance + interest; } return balance } Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

for Loops Program Run: The balance after 20 years is 26532.98 1 /** 1 /** 2 This program computes how much an investment grows in 3 a given number of years. 4 */ 5 public class InvestmentRunner { 6 public static void main(String[] args) { 7 final double INITIAL_BALANCE = 10000; 8 final double RATE = 5; 9 final int YEARS = 20; 10 11 double balance = caluBalance(INITIAL_BALANCE, RATE, YEARS); 12 System.out.printf("The balance after %d years is %.2f\n", 13 YEARS, balance); 14 } 15 } Program Run: The balance after 20 years is 26532.98 Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

Rewrite the for loop in the calcBalance method as a while loop. Self Check 6.3 Rewrite the for loop in the calcBalance method as a while loop. Answer: int i = 1; while ( i <= years ) { double interest = balance * rate / 100; balance = balance + interest; i++; } for (int i = 1; i <= years; i++) { double interest = balance * rate / 100; balance = balance + interest; } Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

How many times does the following for loop execute? Self Check 6.4 How many times does the following for loop execute? for (i = 0; i <= 10; i++) System.out.println(i * i); Answer: 11 times. Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

for Loop Examples Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

Common Errors: Semicolons A semicolon that shouldn’t be there: sum = 0; for (i = 1; i <= 10; i++); sum = sum + i; System.out.println(sum); Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.