COM S 207 While-Loop Statement Instructor: Ying Cai Department of Computer Science Iowa State University

Slides:



Advertisements
Similar presentations
CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
Advertisements

Basic Control Structures Control order of execution of statements sequential selection iteration - Repeat some action while a certain condition is true.
June 10, 2015ICS102: while & do-while1 while and do-while Statements.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
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.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
CS150 Introduction to Computer Science 1
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.
Loops Repeat after me …. Loops A loop is a control structure in which a statement or set of statements execute repeatedly How many times the statements.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
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 Repetition Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
For Loops Programming. COMP102 Prog Fundamentals I: for Loops/Slide 2 The for Statement condition action true false initialization update.
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.
Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.
Building Java Programs Chapter 5 Program Logic and Indefinite Loops Copyright (c) Pearson All rights reserved.
COM S 207 IF Statement Instructor: Ying Cai Department of Computer Science Iowa State University
Java Programming: From the Ground Up
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Computer Science 12 Mr. Jean May 2 nd, The plan: Video clip of the day Review of common errors in programs 2D Arrays.
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,
More While Loop Examples CS303E: Elements of Computers and Programming.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
 Wednesday, 9/18/02, Slide #1 CS106 Introduction to CS1 Wednesday, 9/18/02  QUESTIONS?? HW #1 due today at 5!!  Today: Loops, and two new data types.
1 while loops. 2 Definite loops definite loop: A loop that executes a known number of times.  The for loops we have seen so far are definite loops. We.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
CS101 Computer Programming I Chapter 4 Extra Examples.
Logic Our programs will have to make decisions on what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if and if-else.
Chapter 5: Control Structures II
Advanced Computer Science Lesson 4: Reviewing Loops and Arrays Reading User Input.
COM S 207 Method Instructor: Ying Cai Department of Computer Science Iowa State University
Programming with Loops. When to Use a Loop  Whenever you have a repeated set of actions, you should consider using a loop.  For example, if you have.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
CSE 201 – Elementary Computer Programming 1 Extra Exercises Sourceshttp://
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
REPETITION MTS3033 OBJECT ORIENTED PROGRAMMING 1.
CMSC 150 LOOPS CS 150: Fri 20 Jan Representing DNA AGTCCAGTGTCAA.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
General Condition Loop A general condition loop just loops while some condition remains true. Note that the body of the loop should (eventually) change.
Exercise 2 : Using for loop Repetition (loop) (1)control variable initialization (2)Test Conditon (3)Modification of control variable value order : (1)
Slides by Evan Gallagher
Loops A loop is: Java provides three forms for explicit loops:
Chapter 4 Repetition Statements (loops)
Department of Computer Science
Department of Computer Science
Chapter 5: Control Structures II
Repetition-Counter control Loop
TK1114 Computer Programming
LOOPS.
Building Java Programs Chapter 4
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Outline Altering flow of control Boolean expressions
The for-loop and Nested loops
Loops/Iteration Used to repeat an action Must have a STOP condition
Repetition Control Structure
CIS 110: Introduction to Computer Programming
Repetition Statements
Building Java Programs
Announcements Lab 3 was due today Assignment 2 due next Wednesday
Presentation transcript:

COM S 207 While-Loop Statement Instructor: Ying Cai Department of Computer Science Iowa State University

Used to implement looping Two key elements test condition body of loop Test Condition Execute Statements (body of loop) true false while (condition) { Statement1; Statement2; ::: } // end while While loop body of loop As long as the condition is true, you execute all statements in the body of the loop. When condition is false, you exit the loop. Key Point: Some statement(s) in the body of the loop must eventually make the condition false.

Example int K = 0; while (K < 4) { System.out.print(K + “ “); K++; } output:

More Examples int i = 0; int sum = 0; while (i < 4) { sum = sum + i; i++; } System.out.println(sum);

More Examples int i = 2; int p = 1; int total = 1; while (p < 4) { total = i * total; p++; } System.out.println(total);

More Examples i = 0; sum = 0; while (sum < 10) { i++; sum = sum + i; Print i and sum; } i = 0; sum = 0; while (sum < 10) { i++; sum = sum - i; Print i and sum; } i = 0; sum = 0; while (sum < 0) { i++; sum = sum + i; Print i and sum; } i = 0; sum = 0; while (sum >= 10) { i++; sum = sum + i; Print i and sum; }

Hand-Tracing Loops int n = 1729; int sum = 0; while (n > 0) { int digit = n % 10; sum = sum + digit; n = n / 10; } NSumdigit 17290

Common Error 1: Infinite Loop int year = 1; while (year <= 20) { double interest = balance * rate /100.0; balance = balance + interest; } // end while Forgetting to update the variable that eventually makes the loop condition false

Common Error 1: Infinite Loop Forgetting to update the variable that eventually makes the loop condition false int year = 1; while (year > 0) { double interest = balance * rate /100.0; balance = balance + interest; year++; } // end while

Common Error 2: Off-by-One Error or Boundary Condition Error Questions: 1)Should year start at 0 or 1? 2)Should you test for (balance <TARGET ) or (balance <= TARGET) int year = 0; while (balance < TARGET) { year++; double interest = balance * rate /100.0; } // end while System.out.println(“the interest doubled after ” + year + “years.”);

Exercise Write a Java program that prompts the user for a positive integers value and outputs the numbers ( …) up to the number that is inputted. Scanner in = new Scanner(System.in); System.out.println(“Enter in a positive number: “); int lastNumber = in.nextInt(); int cntr = 1; System.out.println(“ “); while (cntr <= lastNumber) { System.out.print(cntr + “ “); cntr++; }

Write Loops that computes 1)the sum of all integers between 2 and 100 (inclusive) 2)the sum of all EVEN integers between 2 and 100 (exclusive) 3)the sum of all SQUARES of integers between -300 and +300 (inclusive) 4)the average of all ODD integers between 1 and 100 (inclusive)