Slides by Evan Gallagher

Slides:



Advertisements
Similar presentations
Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
Advertisements

Loops (Part 1) Computer Science Erwin High School Fall 2014.
Random Number Generation public class Hand { private int card1; private int card2; private int card3; private Random rand; public static final int NO_CARD.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
Iteration and Loop Statements Horstmann Chapter 7 Loop statements control repeated execution of a block of statements Each time the statements in the block.
Loop variations do-while and for loops. Do-while loops Slight variation of while loops Instead of testing condition, then performing loop body, the loop.
Copyright 2006 by Pearson Education 1 reading: 4.1 Cumulative sum.
Loops –For For Reading for this Lecture, L&L, Part of 5.8.
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.
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
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.
Lecture 10 Instructor: Craig Duckett. Assignment 2 Revision TONIGHT DUE TONIGHT Wednesday, August 5 th Assignment 3 NEXT DUE NEXT Monday, August 10 th.
CSC 1051 M.A. Papalaskari, Villanova University Repetition CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing.
Introduction to Computer Programming Counting Loops.
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.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Chapter 5 Loops.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Overview of Java Loops By: Reid Hunter. What Is A Loop? A loop is a series of commands that will continue to repeat over and over again until a condition.
Chapter 4: Control Structures II
Chapter 5: Control Structures II
Loops Wrap Up 10/21/13. Topics *Sentinel Loops *Nested Loops *Random Numbers.
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 © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Chapter 9 Control Structures.
Chapter 7 – Arrays and Array Lists Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 1.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
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.
CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING For Loop.
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.
Sophomore Scholars Java
Slides by Evan Gallagher
Chapter 9 Repetition.
Chapter 4 Repetition Statements (loops)
Loop Structures.
Repetition-Counter control Loop
Repetition.
TK1114 Computer Programming
The switch Statement The switch statement provides another way to decide which statement to execute next The switch statement evaluates an expression,
LOOPS.
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
Chapter 5 Repetition.
Chapter Three - Implementing Classes
Decision statements. - They can use logic to arrive at desired results
Chapter 6 More Conditionals and Loops
Outline Altering flow of control Boolean expressions
Introduction to Computer Programming Counting Loops 2
Chapter 9 Control Structures.
Java Language Basics.
Control Statements Loops.
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
Lecture Notes – Week 4 Chapter 5 (Loops).
Suggested self-checks:
Control Statements Loops.
Repetition Statements
Announcements Lab 3 was due today Assignment 2 due next Wednesday
Chapter 5 – Decisions Big Java by Cay Horstmann
6.2 for Loops Example: for ( int i = 1; i
Loops and Iteration CS 21a: Introduction to Computing I
Repetition CSC 1051 – Data Structures and Algorithms I Course website:
Presentation transcript:

Slides by Evan Gallagher For Loops 12/08/15 & 12/09/15 C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Slides by Evan Gallagher

Know the syntax of a for loop and how it works. Goals To implement for loop Know the syntax of a for loop and how it works. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

The for Loop To execute statements a certain number of times “You “simply” take 4,522 steps!!! C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

The for Loop Often you will need to execute a sequence of statements a given number of times. You could use a while loop for this. counter = 1; // Initialize the counter while (counter <= 10) // Check the counter { System.out.println(counter); counter++; // Update the counter } C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Java has a statement custom made for this sort of processing: The for Loop Java has a statement custom made for this sort of processing: the for loop. for (counter = 1; counter <= 10; counter++) { System.out.println(counter); } C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

initialization condition statements update The for Loop Is Better than while for Doing Certain Things Doing something a certain number of times or causing a variable to take on a sequence of values is so common, Java has a statement just for that: for (int count = 1; count <= 10; count++) { System.out.println(count); } initialization condition statements update C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

for (initialization; condition; update) { statements } The for Loop for (initialization; condition; update) { statements } The initialization is code that happens once, before the check is made, in order to set up for counting how many times the statements will happen. The loop variable is created here. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

(covered before break.) The for Loop (covered before break.) for (initialization; condition; update) { statements } The condition is code that tests to see if the loop is done. When this test is false, the for statement is over and we go on to the next statement. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

(covered before break.) The for Loop (covered before break.) for (initialization; condition; update) { statements } The statements are repeatedly executed - until the condition is false. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

(covered before break.) The for Loop (covered before break.) for (initialization; condition; update) { statements } The update is code that causes the condition to eventually become false. Usually it’s incrementing or decrementing the loop variable. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

The for Statement The variable i is defined only in this for loop. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Solving a Problem with a for Statement Now let’s see the interest in action: We want to print the balance of our savings account over a five-year period. The “…over a five-year period” indicates that a for loop should be used. Because we know how many times the statements must be executed, we choose a for loop. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

The output should look something like this: Solving a Problem with a for Statement The output should look something like this: C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Solving a Problem with a for Statement The pseudocode: for (int year = 1; year <= nyears; year++) { Update balance. Print year and balance. } The algorithm C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

They are: Solving a Problem with a for Statement Two statements should happen five times. So use a for statement. They are: update balance print year and balance for (int year = 1; year <= nyears; year++) { } // update balance // print year and balance C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

The Modified Investment Program Using a for Loop public static void main(String [] args) { final double RATE = 5; final double INITIAL_BALANCE = 10000; double balance = INITIAL_BALANCE; Scanner scan = new Scanner(System.in); System.out.println(“Number of Years?”); int nyears =scan.nextInt(); DecimalFormat f = new DecimalFormat(“$0.00”); for (int year = 1; year <= nyears; year++) balance = balance * (1 + RATE / 100); System.out.println(year + “ “ + f.format(balance)); } C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

The Modified Investment Program Using a for Loop A run of the program: Enter number of years: 10 1 10500.00 2 11025.00 3 11576.25 4 12155.06 5 12762.82 6 13400.96 7 14071.00 8 14774.55 9 15513.28 10 16288.95 C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Example of Normal Execution – Taking Bigger Steps for loop to hand- trace What is the output? for (int i = 0; i < 9; i += 2) cout << i << " "; C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

for(int n = 1; n<= 5; n++) { s = s + n; System.out.println(s); } R4.14a Trace this. int s = 1; for(int n = 1; n<= 5; n++) { s = s + n; System.out.println(s); }

Write a Program Write a program to output “Winter Wonderland." seven times. Use a for loop to repeat the output of the line, “Winter Wonderland.”