Lecture 4 Loops.

Slides:



Advertisements
Similar presentations
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Advertisements

Building Java Programs
Solving Problems with Repetition. Objectives At the end of this topic, students should be able to: Correctly use a while statement in a C# program Correctly.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 5: Program Logic and Indefinite Loops.
Computer Science 1620 Loops.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 5: Program Logic and Indefinite Loops.
Iteration and Loop Statements Horstmann Chapter 7 Loop statements control repeated execution of a block of statements Each time the statements in the block.
Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
Summary of Loops Programming. COMP102 Prog Fundamentals I: Summary of Loops /Slide 2 Which Loop to Use? l for loop n for calculations that are repeated.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
Chapter 5: Control Structures II (Repetition)
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
JAVA Control Structures: Repetition. Objectives Be able to use a loop to implement a repetitive algorithm Practice, Practice, Practice... Reinforce the.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 5: Control Structures II (Repetition)
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Chapter 6 – Repetition Statements : Objectives After you have read and studied this chapter, you should be able to Implement repetition control in a program.
1 Copyright (C) 2008 by Dennis A. Fairclough all rights reserved.
Chapter 6 Iteration.  Executes a block of code repeatedly  A condition controls how often the loop is executed while (condition) statement  Most commonly,
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.
Chapter 4 Loops Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Chapter 5 Loops.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 4 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.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Chapter 5 Control Structure (Repetition). Objectives In this chapter, you will: Learn about repetition (looping) control structures Explore how to construct.
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.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Discussion 4. Labs public MyPoint(double xInit, double yInit ) { MyPoint p = new MyPoint(0, 0); } ClassProblem.java recursive java.lang.StackOverflowError.
Chapter 4: Control Structures II
Chapter 5: Control Structures II
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Java iteration statements ● Iteration statements are statements which appear in the source code only once, but it execute many times. ● Such kind of statements.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 5: Control Structures II (Repetition)
REPETITION MTS3033 OBJECT ORIENTED PROGRAMMING 1.
Feedback  Lab2, Hw1  Groups  Group Project Requirements.
1 BUILDING JAVA PROGRAMS CHAPTER 5 PROGRAM LOGIC AND INDEFINITE LOOPS.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
INF120 Basics in JAVA Programming AUBG, COS dept Lecture 06 Title: Iterative/Repetitive Control Structures Reference: MalikFarrell, chap 1, Liang Ch 4.
Computer C programming Chapter 3. CHAPTER 3 Program Looping –The for Statement –Nested for Loops –for Loop Variants –The while Statement –The do Statement.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 6.
Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University.
Three kinds of looping structures The while loop The for loop The do (also called the do-while) loop All have equivalent power, e.g., if you can write.
Introduction to Computer Programming
Chapter 5: Control Structures II (Repetition)
Chapter 5: Control Structures II
Repetition-Sentinel,Flag Loop/Do_While
Repetition.
Selected Topics From Chapter 6 Iteration
Repetition Chapter 6 12/06/16 & 12/07/16 1 1
Chapter 4 Control structures and Loops
Building Java Programs
Building Java Programs
CSS161: Fundamentals of Computing
Control Statements Loops.
Chapter 5 Loops.
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
Chapter 5: Control Structures II (Repetition)
Control Statements Loops.
Building Java Programs
Building Java Programs
Indefinite loop variations
Building Java Programs
Presentation transcript:

Lecture 4 Loops

Looping Constructs

The while Loop

Years to Double Investment public class DoubleInvestment { public static void main(String[] args) { final double RATE = 5; final double INITIAL_BALANCE = 10000; final double TARGET = 2 * INITIAL_BALANCE; double balance = INITIAL_BALANCE; int year = 0; // Count the years required for the investment to double while (balance < TARGET) { year++; double interest = balance * RATE / 100; balance = balance + interest; } System.out.println("The investment doubled after " + year + " years."); } }

Hand-Tracing Code Execution This program computes the sum of the digits in an integer.

Example: The Number Guessing Game Algorithm Design The Problem: Write a program that guesses the user's secret number between 1 and 100. The program should be able to guess the number in seven tries or less. The Algorithm: 1. Set the initial range between lo = 0 and hi = 101 2. The program makes a guess in the middle of the range guess = (lo + hi)/2 3. If guess is too high, let hi = guess 4. If guess is too low, let lo = guess 5. If guess is correct claim victory and quit else return to Step 2.

Example: The Number Guessing Game Implementation This is the setup Let the game begin...

Example: The Number Guessing Game Analysis Starting with 100 possible answers, each wrong guess eliminates 1/2 of the possibilities. Starting with 100, how many times would be have to divide by 2 before the resulting value is <= 1? 100 2 𝑛 = 1 100= 2 𝑛 100 -> 50 -> 25 -> 12 -> 6 -> 3 -> 1 n = 𝑙𝑔 2 100 n = 6.643856...

Example: Building a StopWatch Whenever currentTimeMillis() is called it returns the current time in milliseconds. The stopwatch gets the current time when the first OK is pressed (startTime) and it gets the current time again when the second OK is pressed (finishTime). The elapsed time is computed as the difference between these two times converted to seconds.

Example: Building a StopWatch Whenever currentTimeMillis() is called it returns the current time in milliseconds. The stopwatch gets the current time when the first OK is pressed (startTime) and it gets the current time again when the second OK is pressed (finishTime). The elapsed time is computed as the difference between these two times converted to seconds.

A Delay Timer time delay in milliseconds setting the time when loop is entered stay in the loop for delMsec (Msec) This method is very useful for controlling the speed of execution of a program. Control remains in the do-while( ) loop for delMsec milliseconds.

Example: Reading Integers from a Textfile necessary when reading data from a file data.txt Blanks, carriage returns, and other white-space will be ignored by Scanner inputs for numeric data types such as integer

for Loop The for-loop is the oldest looping construct. It has been implemented in every procedural high-level programming language since FORTRAN (1957). For-loops are commonly used when you know the number of iterations to be performed before beginning the loop.

for Loop Examples

for Loop Test Program

The do while Loop

do while Test Program

Nested Loops body of outer loop executes 10 times body of inner loop executes 10 times each time it is called by the outer loop inner loop prints 10 stars on a line outer loop executes inner loop to print a line of stars and then starts a new line

more Nested Loop Examples

even more Nested Loop Examples

Multiplication Table Generator

Greatest Common Divisor

Using break & continue break - break out of the current loop x = 369 x = 531 x = 89 x = 453 x = 801 x = 777 x = 28 x = 241 x = 597 x = 989 x = 657 x = 335 Must have gotten a 42 break - break out of the current loop continue - continue immediately to the next iteration of the loop

Controlling Loops with Sentinel Values