all loops initialization – set up the loop

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

While loops.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
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.
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.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Chapter 4 Slide #1.
Starting Out With Java 5 (Control Structures to Objects) Chapter 3 By Tony Gaddis Copyright © 2005 Pearson Addison-Wesley. All rights reserved.
Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Chapter 4 Slide #1.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING For Loop.
Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005 Pearson Addison- Wesley. All rights reserved. Chapter 1 Slide #1.
Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied. Suppose you want to execute some code/s 10 times.
Follow up from lab See Magic8Ball.java Issues that you ran into.
Topic : While, For, Do-While Loop Guided By : Branch : Batch :
Control Flow (Python) Dr. José M. Reyes Álamo.
Loops in Java.
Chapter 3 Loops Section 3.3 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
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.
if-else-if Statements
Chapter 4: Loops and Files
Chapter 4: Loops and Files by Tony Gaddis and Godfrey Muganda
Chapter 17 Linked Lists.
Chapter 19 Binary Search Trees.
11.7 Motion in Space Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Chapter 4 Inheritance.
Chapter 7 Functions of Random Variables (Optional)
Chapter 14 Graphs and Paths.
Debugging October 3, 2007 ComS 207: Programming I (in Java)
Starting Out With Java 5 (Control Structures to Objects) Chapter 3
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
The Problem You are writing a program that accepts from the command line a number and that number tells the application how many numbers to read from standard.
Chapter 6: Repetition Structures
Chapter 5: Repetition Structures
Chapter 4: Loops and Files
Chapter 10 Datapath Subsystems.
Chapter 18 Bayesian Statistics.
Chapter 20 Hash Tables.
Copyright © 2011 Pearson Education, Inc
Chapter 8: More on the Repetition Structure
Repetition and Loop Statements
Searching for Guinea Pig B: Case Study in Online Research
Chapter 5 Algorithm Analysis.
CprE 185: Intro to Problem Solving (using C)
Debugging October 4, 2006 ComS 207: Programming I (in Java)
The Facts to Be Explained
Seating “chart” Front - Screen rows Back DOOR.
Alternate Proofs of Selected HO Theorems
Based on slides created by Bjarne Stroustrup & Tony Gaddis
PROGRAM FLOWCHART Iteration Statements.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Introduction: Some Representative Problems
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.
Circuit Characterization and Performance Estimation
Code examples MakingCircles.java Circle.java Loopy.java
The Classical Model with Many Goods
Chapter 2 Part 1 Data and Expressions.
Chapter 3 Flow of Control Loops in Java.
Chapter 6 Dynamic Programming.
CprE 185: Intro to Problem Solving (using C)
Chapter 2 Reference Types.
Chapter 4 Greedy Algorithms.
Copyright © 2011 Pearson Education, Inc
Presentation transcript:

all loops initialization – set up the loop decision – control for when to stop loop (continue while condition is true) update – you must “approach” the false condition in some way body – the group of actions that you will take while the loop is executing Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved.

The while Loop While loop is a “pre-condition” loop The decision comes first Example: WhileLoop.java Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved.

The while loop Flowchart statement(s) true boolean expression? false Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved.

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, more commonly called a do loop, takes the form: do{ statements }while(condition); Example: TestAverage1.java Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved.

The do-while Loop Flowchart statement(s) true boolean expression? false Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved.

The for Loop The for loop is a specialized form of the while loop, meaning it 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) { loop statements; } Example: Squares.java Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved.

The for Loop Flowchart statement(s) true boolean expression? false update Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved.

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 Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved.

Running Totals Loops allow the program to keep running totals while evaluating data. Imagine needing to keep a running total of user input. Example: TotalSales.java Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved.