Chapter 4: Loops and Files

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming
Advertisements

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
Chapter 4: Loops and Files
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.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
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.
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.
Looping Yong Choi School of Business CSU, Bakersfield.
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.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 6: Repetition  Some additional operators increment and decrement.
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.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Looping Exercises Deciding Which Loop to Use At this.
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
Chapter 4: Looping. Resource: Starting Out with C++, Third Edition, Tony Gaddis 5.1 The Increment and Decrement Operators ++ and -- are operators that.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Repetition & Loops. One of the BIG advantages of a computer: ­It can perform tasks over and over again, without getting bored or making mistakes (assuming.
Chapter 4: Loops and Files. The Increment and Decrement Operators  There are numerous times where a variable must simply be incremented or decremented.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
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 4: Control Structures II
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 9 & 10 Repetition Statements.
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 – Part 3 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 Outline The if Statement and Conditions Other Conditional.
Copyright © 2012 Pearson Education, Inc. Chapter 5 Loops, File, and Random Numbers.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Loops and Files Starting Out with Java From Control Structures.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Using Loops. Goals Understand how to create while loops in JavaScript. Understand how to create do/while loops in JavaScript. Understand how to create.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
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.
I NTRODUCTION TO PROGRAMMING Starting Out with Java: From Control Structures through Objects.
Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Chapter 4 Slide #1.
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.
1 Fall 2009ACS-1903 Ch 4 Loops and Files while loop do-while loop for loop Other topics later on …
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING While Loop.
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.
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.
Chapter 5 – Part 3 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/19 Outline The if Statement and Conditions Other Conditional.
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.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
Loop Structures.
Lecture 4- Loops and Files
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
Java Programming: Guided Learning with Early Objects
all loops initialization – set up the loop
Chapter 4: Loops and Files
Chapter 4: Loops and Files by Tony Gaddis and Godfrey Muganda
Chapter 6: Repetition Structures
Chapter 5: Repetition Structures
Chapter 4: Loops and Files
Based on slides created by Bjarne Stroustrup & Tony Gaddis
PROGRAM FLOWCHART Iteration Statements.
Ch.4 – 5 Topics The auto Increment and Decrement Operators
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Presentation transcript:

Chapter 4: Loops and Files Starting Out with Java: From Control Structures through Objects Fifth Edition by Tony Gaddis This slide show consists of excerpts from the orginal Chapter 4 slide set.

Chapter Topics Chapter 4 discusses the following main topics: A few preliminary topics Generating Random Numbers with the Random class The Increment and Decrement Operators Loops in java The while Loop The do-while Loop The for Loop

Generating Random Numbers with the Random Class Some applications, such as games and simulations, require the use of randomly generated numbers. A simple example is the “I’m thinking of number between 1 and 10”, (or some other variation), where the program must generate a random number between 1 and 10 for the user to guess. The program reads the user’s guess and then tells them if they are right or not. For such programs, the Java API has a class named Random, that provides a method to use for generating random numbers.

Generating Random Numbers with the Random Class To use the Random class, you would first include the import statement: import java.util.Random; And secondly, instantiate a object of the Random class: Random ran = new Random(); After that is done, anytime your program needs random number, you can call a method depending on what type of number you need.

Generating Random Numbers with the Random Class For example: To obtain an integer, use: int randomInt = ran.nextInt(); To obtain an integer within a specific range, use: int randomInt = ran.nextInt(11); // returns an int from 0 to 10 Method Description nextInt() Returns the next random number as an int. The number will be within the range of an int, which is –2,147,483,648 to +2,147,483,648. nextInt(int n) This method accepts an integer argument, n. It returns a random number as an int. The number will be within the range of 0 to n-1.

Some Methods of the Random Class Description nextInt() Returns the next random number as an int. The number will be within the range of an int, which is –2,147,483,648 to +2,147,483,648. nextInt(int n) This method accepts an integer argument, n. It returns a random number as an int. The number will be within the range of 0 to n. See example: RollDice.java

The Increment and Decrement Operators In many of the programs that use loops, you will see the use of two operators that you may not have encountered yet. They are: Incrementation operator ++ Decrementation operator -- Let’s take a look at the meaning of these operators before reading the sample codes of chapter 4.

The Increment and Decrement Operators Here Increment means add 1 and decrement means subtract 1. There are numerous times where a variable must simply be incremented or decremented. number = number + 1; number = number – 1; Java provide shortened ways to increment and decrement a variable’s value. Using the ++ or -- unary operators, this task can be completed quickly. number++; or ++number; number--; or --number; There is an Example Program in the Chapter 4 Source Code folder online named: IncrementDecrement.java A version of that follows on the next page.

OUTPUT number starts out at 4 After incrementation number is 5 /** This program demonstrates the ++ and -- operators. */ public class IncrementDecrement2 { public static void main(String[] args) int number = 4; // number starts out with 4 System.out.println("number starts out at " + number); number++; // Increment number. System.out.println(" After incrementation number is " + number); number--; // Decrement number. System.out.println(" After decrementation number is " + number); } OUTPUT number starts out at 4 After incrementation number is 5 After decrementation number is 4

Loops Frequently in programming, you want certain segments of code to be repeated without having to write the same code multiple times. In programming, a loop is a statement that will cause a segment of code to be repeated (i.e. iterated). For example, think back to Project 1 where you calculated the BMI for 5 persons. Recall that the same code that worked for 1 person had to be written 4 more times. By using a looping statement, we could have written this code segment once and then placed in a looping construct and specify that it be repeated 5 times.

Loops Java provides three different looping structures. The while loop The do-while loop The for loop

The while Loop Java provides three different looping structures. The while loop has the form: while(condition) { statement1; statement2; … } While the condition is true, the statements will execute repeatedly. The while loop is a pretest loop, which means that it will test the value of the condition prior to executing the loop.

The while loop Flowchart Statement1; Statement2; … true boolean expression? false

The while Loop Care must be taken to set the condition to false somewhere in the loop so the loop will end. Loops that do not end are called infinite loops. A while loop executes 0 or more times. If the condition is false, the loop will not execute. Example: WhileLoop.java

Infinite Loops In order for a while loop to end, the condition must become false. The following loop will not end: int x = 20; while(x > 0) { System.out.println("x is greater than 0"); } The variable x never gets decremented so it will always be greater than 0. Adding the x-- above fixes the problem.

This version of the loop decrements x during each iteration: Infinite Loops This version of the loop decrements x during each iteration: int x = 20; while(x > 0) { System.out.println("x is greater than 0"); x--; }

Block Statements in Loops Curly braces are required to enclose block statement while loops. (like block if statements) while (condition) { statement; }

The do-while Loop The do-while loop is a post-test loop, which means it will execute the loop prior to testing the condition. The do-while loop (sometimes called called a do loop) takes the form: do { statement(s); }while (condition); Example: TestAverage1.java

The do-while Loop Flowchart statement(s) true boolean expression? false

The for Loop The for loop is a pre-test loop. The for loop allows the programmer to initialize a control variable, test a condition, and modify the control variable all in one line of code. The for loop takes the form: for(initialization; test; update) { statement(s); } See example: Squares.java

The for Loop Flowchart statement(s) true boolean expression? false update

The Sections of The for Loop The initialization section of the for loop allows the loop to initialize its own control variable. The test section of the for statement acts in the same manner as the condition section of a while loop. The update section of the for loop is the last thing to execute at the end of each loop. Example: UserSquares.java

The for Loop Initialization The initialization section of a for loop is optional; however, it is usually provided. Typically, for loops initialize a counter variable that will be tested by the test section of the loop and updated by the update section. The initialization section can initialize multiple variables. Variables declared in this section have scope only for the for loop.

The Update Expression The update expression is usually used to increment or decrement the counter variable(s) declared in the initialization section of the for loop. The update section of the loop executes last in the loop. The update section may update multiple variables. Each variable updated is executed as if it were on a line by itself.

When to use each type of loop… Page 233 The while loop is a pre-test loop ideal for cases where where you do not want the loop to iterate if the condition is false from the beginning. The do-while loop is a post-test loop and is used in situations where you always want the loop to iterate at least once. The for loop is a pretest loop that is used for simple counting loops, ideal when the exact number of iterations is known or can be calculated before the loop begins.