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.

Slides:



Advertisements
Similar presentations
Chapter 7 - Iteration. Chapter Goals Program repitiation statements – or loops – with the for, while, and do-while statements Program repitiation statements.
Advertisements

Week 5: Loops 1.  Repetition is the ability to do something over and over again  With repetition in the mix, we can solve practically any problem that.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 1 Chapter 6: Iteration 1 Chapter 6 Iteration.
June 10, 2015ICS102: while & do-while1 while and do-while Statements.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
COMP 14 Introduction to Programming Miguel A. Otaduy May 21, 2004.
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.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Chapter 5: Control Structures II (Repetition)
Loops – While, Do, For Repetition Statements Introduction to Arrays
Datalogi A 5: 6/10. while Loops Executes a block of code repeatedly A condition controls how often the loop is executed while (condition) statement; Most.
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.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
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.
Flow of Control Loops – Chapter 3.2. Java Loop Statements: Outline the while Statement the do-while Statement the for Statement.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
CSM-Java Programming-I Spring,2005 Control Flow Lesson - 3.
The switch Statement, DecimalFormat, and Introduction to Looping
COM S 207 While-Loop Statement Instructor: Ying Cai Department of Computer Science Iowa State University
Chapter 6 Iteration.  Executes a block of code repeatedly  A condition controls how often the loop is executed while (condition) statement  Most commonly,
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
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.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Chapter 5 Loops.
Copyright © 2013 by John Wiley & Sons. All rights reserved. LOOPS CHAPTER Slides by Donald W. Smith TechNeTrain.com Final Draft Oct 30,
 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.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
If Statement if (amount
Repetition Statements (Loops) The do while Loop The last iteration structure in C++ is the do while loop. A do while loop repeats a statement or.
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.
REPETITION MTS3033 OBJECT ORIENTED PROGRAMMING 1.
Flow of Control: Loops Module 4. Objectives Design a loop Use while, do, and for in a program Use the for-each with enumerations Use assertion checks.
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
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.
Lecture 4b Repeating With Loops
REPETITION CONTROL STRUCTURE
The switch Statement, and Introduction to Looping
Selected Topics From Chapter 6 Iteration
LOOPS.
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Outline Altering flow of control Boolean expressions
Introduction to Object-Oriented Programming with Java--Wu
Chapter 7 Iteration.
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Repetition Control Structure
Lab5 PROGRAMMING 1 Loop chapter4.
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
Repetition Statements
Based on slides created by Bjarne Stroustrup & Tony Gaddis
PROGRAM FLOWCHART Iteration Statements.
Announcements Lab 3 was due today Assignment 2 due next Wednesday
CSC215 Lecture Control Flow.
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Looping and Repetition
Presentation transcript:

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 nested loops To implement simulations

Blocks Group statements together { } Variables declared in a block disappear when you leave a block int x=3; int y=4; { int temp=x; x=y; y=temp; } // now x is 4, y is 3 and temp is unknown

Blocks if(x == 0) System.out.println(”it’s zero”); Or if(x == 0){ System.out.println(”it’s zero”); System.out.println(”try again”); } But watch out for: if(x == 0) System.out.println(”it’s zero”); System.out.println(”try again”);

while Loops Executes a block of code repeatedly A condition controls how often the loop is executed while (condition) statement; Most commonly, the statement is a block statement

Calculating the Growth of an Investment Invest $10,000, 5% interest, compounded annually YearBalance 0$10,000 1$10,500 2$11,025 3$11, $12, $12,762.82

While loop When has the bank account reached a particular balance? while (balance < targetBalance) { year++; double interest = balance * rate / 100 balance = balance + interest; }

Flow of control

Common Error: Infinite Loops int years = 0; while (years < 20) { double interest = balance * rate / 100; balance = balance + interest; } int years = 20; while (years > 0) { years++; // Oops, should have been years-- double interest = balance * rate / 100; balance = balance + interest; } Loops run forever–must kill program

Java int years = 0; while (balance < 2 * initialBalance) { years++; double interest = balance * rate / 100; balance = balance + interest; } System.out.println("The investment reached the target after " + years + " years."); Should years start at 0 or 1? Should the test be < or <=?

do Loops Executes loop body at least once: do statement while (condition); Example: Validate input double value; do { System.out.print( "Please enter a positive number: "); value = in.nextDouble(); } while (value <= 0);

do loop

Spaghetti code – not in java years=1; goto a: b: years++; a: balance+=interest; if(balance<targetBalance) goto b;

for Loops for (initialization; condition; update) statement Example: for (int i = 1; i <= n; i++){ double interest = balance * rate / 100; balance = balance + interest; }

for loops for (initialization; condition; update) statement Equivalent to initialization; while (condition) { statement; update; } Other examples: for(years = n; years > 0; years--)... for(x = -10; x <= 10; x = x + 0.5)...

for loops

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);

Nested loops Create triangle pattern [] [][] [][][] [][][][] Loop through rows for (int i = 1; i <= n; i++) { // make triangle row } Make triangle row is another loop for (int j = 1; j <= i; j++) r = r + "[]"; r = r + "\n"; Put loops together → Nested loops

Loop and a half Sometimes termination condition of a loop can only be evaluated in the middle of the loop Then, introduce a boolean variable to control the loop: boolean done = false; while (!done){ Print prompt String input = read input ; if ( end of input indicated ) done = true; else{ // Process input }

Break and continue break; jump out of loop continue; jump to start of loop while(true){ Print prompt String input = read input ; if( end of input indicated )break; // Process input }

Dangling else if ( x > 0 ) if (y > 0) System.out.println("First case"); else System.out.println("Second case");

switch switch(x){ case 1:.. break; case 2:.. break; case 3:.. break; }