Learning About the Loop Structure (continued)

Slides:



Advertisements
Similar presentations
Repetition Statements Perform the same task repeatedly Allow the computer to do the tedious, boring things.
Advertisements

Programming Logic and Design Eighth Edition
Chapter 6 – Lots of Loops! September 28, Boolean expression Loop body.
CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
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.
Objectives In this chapter, you will learn about:
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 (!)
Looping Yong Choi School of Business CSU, Bakersfield.
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.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 6: Repetition  Some additional operators increment and decrement.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 6 – Car Payment Calculator Application: Introducing.
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.
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
Programming Logic and Design Fifth Edition, Comprehensive
Using Shortcut Arithmetic Operators Accumulator: A variable that is used to total. Its value is repeatedly increased by some amount. Java provides shortcuts.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 6 – Car Payment Calculator Application: Introducing.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Programming Logic and Design Sixth Edition Chapter 5 Looping.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 6 Looping.
AEEE 195 – Repetition Structures: Part B Spring semester 2011.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 4 Looping.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 5A Repetition (Concepts)
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.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
Chapter 6: Looping. Objectives Learn about the loop structure Create while loops Use shortcut arithmetic operators Create for loops Create do…while loops.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Loops Tonga Institute of Higher Education. Introduction Programs need to be able to execute tasks repeatedly. Use loops to repeat actions  For Loop 
General Condition Loop A general condition loop just loops while some condition remains true. Note that the body of the loop should (eventually) change.
Chapter 5: Looping. Using the while Loop Loop – A structure that allows repeated execution of a block of statements Loop body – A block of statements.
Chapter 7 JavaScript: Control Statements, Part 1
Chapter 4 – C Program Control
Chapter 4 Repetition Statements (loops)
Control Statements: Part 1
Programming Logic and Design Fourth Edition, Comprehensive
JavaScript: Control Statements I
CHAPTER 5A Loop Structure
Chapter 5: Loops and Files.
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Chapter 6 – Lots of Loops! September 28, 2010.
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.
MSIS 655 Advanced Business Applications Programming
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Outline Altering flow of control Boolean expressions
Unary Operators ++ and --
3 Control Statements:.
Chapter 8: More on the Repetition Structure
3.5- The while Statement The while statement has the following syntax:
Chapter 6: Repetition Statements
Repetition Statements (Loops) - 2
PROGRAM FLOWCHART Iteration Statements.
Chap 7. Advanced Control Statements in Java
Using C++ Arithmetic Operators and Control Structures
Learning Plan 4 Looping.
‘do’ and ‘for’ loops October 1, 2007 ComS 207: Programming I (in Java)
‘do’ and ‘for’ loops October 2, 2006 ComS 207: Programming I (in Java)
Control Statements:.
Presentation transcript:

Learning About the Loop Structure (continued) Three types of loops while Loop-controlling Boolean expression is the first statement for A concise format in which to execute loops do...while Loop-controlling Boolean expression is the last statement Java Programming, Fifth Edition

Learning About the Loop Structure (continued) Java Programming, Fifth Edition

Using a while Loop To Create a Definite Loop (continued) Java Programming, Fifth Edition

Using a while Loop To Create a Definite Loop (continued) Prevent while loop from executing infinitely Named loop control variable initialized to starting value Loop control variable tested in while statement If test expression true Body of while statement takes action Alters value of loop control variable Test of while statement must eventually evaluate to false Java Programming, Fifth Edition

Using a while Loop To Create a Definite Loop (continued) Java Programming, Fifth Edition

Java Programming, Fifth Edition

Using a while Loop to Create an Indefinite Loop Event controlled Controlled by user Executed any number of times Validating data Ensures value falls within specified range Use indefinite loops to validate input data If user enters incorrect data Loop repeats Java Programming, Fifth Edition

Java Programming, Fifth Edition

Using Shortcut Arithmetic Operators Accumulating Repeatedly increasing value by some amount Java provides shortcuts for incrementing and accumulating: += add and assign -= subtract and assign *= multiply and assign /= divide and assign %= remainder and assign Java Programming, Fifth Edition

Using Shortcut Arithmetic Operators (continued) Prefix and postfix increment operators ++someValue, someValue++ Use only with variables Unary operators Use with one value Increase variable’s value by 1 No difference between operators (unless other operations in same expression) Java Programming, Fifth Edition

Using Shortcut Arithmetic Operators (continued) Java Programming, Fifth Edition

Using Shortcut Arithmetic Operators (continued) Prefix and postfix increment operators (continued) Prefix ++ Result calculated and stored Then variable used Postfix ++ Variable used Then result calculated and stored Prefix and postfix decrement operators --someValue someValue-- Similar logic to increment operators Java Programming, Fifth Edition

Using a for Loop (continued) Java Programming, Fifth Edition

Learning How and When to Use a do...while Loop (continued) Java Programming, Fifth Edition

Learning About Nested Loops Inner and outer loops Inner loop must be entirely contained in outer loop Loops can never overlap To print three mailing labels for each of 20 customers for(customer = 1; customer <= 20; ++customer) for(label = 1; label <= 3; ++label) printLabelMethod(); Java Programming, Fifth Edition

Learning About Nested Loops (continued) Java Programming, Fifth Edition

Considering the Order of Evaluation for Short-Circuit Operators Short-circuit evaluation Each part of an AND or an OR expression is evaluated only as much as necessary to determine the value of expression Important to consider number of evaluations that take place When loop might execute many times Java Programming, Fifth Edition