1 Fall 2009ACS-1903 Ch 4 Loops and Files while loop do-while loop for loop Other topics later on …

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming
Advertisements

LOOP / REPETITION while loop. for loop do/while loop We assume that loops are not meant to be infinite. That is, there should always be a way out of the.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
Repeating Actions While and For Loops
Computer Science 1620 Loops.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Chapter 5: Loops and Files.
Loops – While, Do, For Repetition Statements Introduction to Arrays
1 Fall 2007ACS-1903 for Loop Writing to a file String conversions Random class.
1 Fall 2008ACS-1903 for Loop Reading files String conversions Random class.
©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.
1 Fall 2008ACS-1903 Ch 4 Loops and Files while loop do-while loop Other topics later on …
1 Fall 2007ACS-1903 Ch 4 Loops and Files Increment & decrement statements while loop do-while loop Other topics later on …
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
The switch Statement, DecimalFormat, and Introduction to Looping
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
Chapter 4: Loops and Files
Chapter 4: Loops and Files. The Increment and Decrement Operators  There are numerous times where a variable must simply be incremented or decremented.
CONTROLLING PROGRAM FLOW
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
Lecture 3 Looping and FIiling. 5-2 Topics – The Increment and Decrement Operators – The while Loop – Using the while Loop for Input Validation – The do.
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Loops and Files. 5.1 The Increment and Decrement Operators.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Chapter 4 Slide #1.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Repetition Repetition allows you to repeat an operation or a series of operations many times. This is called looping and is one of the basic structured.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Iteration & Loop Statements 1 Iteration or Loop Statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
Chapter 6 - Repetition. while Loop u Simplest loop u Two parts: test expression and loop body u Pre-tested loop –Execute loop body if test true –Bypass.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
While ( number
CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING For Loop.
Lesson 7 Iteration Structures. Iteration is the third control structure we will explore. Iteration simply means to do something repeatedly. All iteration.
Topic : While, For, Do-While Loop Guided By : Branch : Batch :
The switch Statement, and Introduction to Looping
Loop Structures.
Review If you want to display a floating-point number in a particular format use The DecimalFormat Class printf A loop is… a control structure that causes.
Chapter 5: Repetition Structures
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Outline Altering flow of control Boolean expressions
Chapter 6: Repetition Structures
Chapter 5: Repetition Structures
Chapter 4: Loops and Files
Chapter 6: Repetition Statements
More Loops Topics Counter-Controlled (Definite) Repetition
Based on slides created by Bjarne Stroustrup & Tony Gaddis
PROGRAM FLOWCHART Iteration Statements.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Presentation transcript:

1 Fall 2009ACS-1903 Ch 4 Loops and Files while loop do-while loop for loop Other topics later on …

2 Fall 2009ACS-1903 Incrementing and Decrementing Variables by 1 number = number + 1 ; number += 1; number = number - 1 ; number -= 1 ; number++; ++number; number --; --number;

3 Fall 2009ACS-1903 while loops while Pre-test (expression tested before the iteration) Loop could be executed zero times do-while Post-test (expression tested after the iteration) Loop executed at least once

4 Fall 2009ACS-1903 Flowchart for while Boolean Expression Loop statement(s) false true The statements comprising the loop body are executed while the expression is true. pre-test

5 Fall 2009ACS-1903 Reading keyboard input until a sentinel is reached studentNumber=keyboard.nextInt(); countStudents = 0; while (studentNumber != 0) { countStudents++; studentNumber=keyboard.nextInt(); } Sometimes systems are designed to process some data until a “special” value is encountered. ReadUntilSentinel.java

6 Fall 2009ACS-1903 Example studentNumber=keyboard.nextInt(); // get student numbers until the sentinel of 0 is encountered while (studentNumber != 0) { mark = keyboard.nextDouble(); if (mark < 50) grade = "F"; else if (mark < 60) grade = "D"; else if (mark < 65) grade = "C"; else if (mark < 70) grade = "C+"; else grade = "B"; // display the grade System.out.println(studentNumber+" "+mark+" "+grade); studentNumber=keyboard.nextInt(); } Read student numbers and marks -until sentinel of 0 for student number -if there is a student number, there will be a mark too -assign grades based on marks What is the flowchart for this? marksAndGrades.java

7 Fall 2009ACS-1903 Flowchart for do-while Boolean Expression Loop statement(s) false true The statements comprising the loop body are executed repeatedly until the expression becomes false. post-test

8 Fall 2009ACS-1903 Summing the digits of a number int number = 373 ; do { sum +=(number % 10); number=number/10; } while (number!=0); Sometimes a process can be used that can or must execute at least once and so it can be structured with a loop with a post-test. SumDigitsOfInteger.java

9 Fall 2009ACS-1903 for Loop The initialization section … initialize its control variable. The test section of the for statement is similar to the test in a while loop. The update section of the for loop is the last thing to execute in the loop. Example: UserSquares.javaUserSquares.java

10 Fall 2009ACS-1903 The for Loop Initialization Typically, for loops initialize a counting variable Can initialize multiple variables. Variables declared in this section have scope only for the for loop.

11 Fall 2009ACS-1903 The Update Expression usually used to increment or decrement the counting variable(s) last section to execute in the loop. may update multiple variables. Each variable updated is executed as if it were on a line by itself.

12 Fall 2009ACS-1903 Modifying The Control Variable bad programming style to update the control variable of a for loop within the body of the loop leads to hard to maintain code and difficult debugging. update section should be used to update the control variable.

13 Fall 2009ACS-1903 Multiple Initializations and Updates The for loop may initialize and update multiple variables. for(int i = 5, int j = 0; i < 10 || j < 20; i++, j+=2){ … loop statements } only the semicolons are mandatory. for(;;){ … loop statements }//infinite loop. We will not consider using for’s like the above We do not cover the use of the break or continue statements If left out, the test section defaults to true.

14 Fall 2009ACS-1903 Nested Loops Like if statements, loops can be nested. If a loop is nested, the inner loop will execute all of its iterations each time the outer loop executes once. for(int i = 0; i < 10; i++) for(int j = 0; j < 10; j++) { … loop statements } The loop statements in this example execute 100 times. Example: Clock.javaClock.java