Introduction to Computers and Programming Lecture 9: For Loops New York University.

Slides:



Advertisements
Similar presentations
Chapter 4 Loops Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Advertisements

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 3 Loops.
1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Introduction to working with Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
Introduction to Computers and Programming Lecture 7:
1 Parts of a Loop (reminder) Every loop will always contain three main elements: –Priming: initialize your variables. –Testing: test against some known.
Introduction to Computers and Programming Lecture 8: More Loops New York University.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Chapter 5: Control Structures II (Repetition)
Loops – While, Do, For Repetition Statements Introduction to Arrays
 2007 Pearson Education, Inc. All rights reserved C Program Control.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
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.
 2007 Pearson Education, Inc. All rights reserved C Program Control.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Repetition Statements.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The switch Multiple-Selection Statement switch.
C How to Program, 6/e Summary © by Pearson Education, Inc. All Rights Reserved.
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 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Chapter 6: Iteration Part 1. To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To understand.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Loops and Iteration for Statements, while Statements and do-while Statements.
Chapter 4 C Program Control. Objectives In this chapter, you will learn: –To be able to use the for and do … while repetition statements. –To understand.
Chapter 4 Loops Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.1 Chapter 5 Loops.
Loops 1. Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved while Loop Flow.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
Control Structures II (Repetition). Objectives In this chapter you will: Learn about repetition (looping) control structures Explore how to construct.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 9 & 10 Repetition Statements.
Sections 5.1 – 5.4 © Copyright by Pearson Education, Inc. All Rights Reserved.
CPS120 Introduction to Computer Science Iteration (Looping)
Repetition Statements (Loops) The do while Loop The last iteration structure in C++ is the do while loop. A do while loop repeats a statement or.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Introduction to Computers and Programming Lecture 7:
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.
REPETITION MTS3033 OBJECT ORIENTED PROGRAMMING 1.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
 2007 Pearson Education, Inc. All rights reserved C Program Control.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University.
Lecture 4b Repeating With Loops
Chapter 4 – C Program Control
Chapter 4 Repetition Statements (loops)
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Lecture 7: Repeating a Known Number of Times
Chapter 4 Control structures and Loops
MSIS 655 Advanced Business Applications Programming
Outline Altering flow of control Boolean expressions
Introduction to Object-Oriented Programming with Java--Wu
Chapter 4 - Program Control
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Chapter 6: Repetition Statements
Repetition Statements (Loops) - 2
Chapter 4 - Program Control
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.
Loops and Iteration CS 21a: Introduction to Computing I
Chapter 4: Loops and Iteration
Presentation transcript:

Introduction to Computers and Programming Lecture 9: For Loops New York University

2 Road map Introduction to for loops Good practice and style using for loops Reading: –Chapter 3: (up to nested for loops)

3 review What is the difference between a while and a do / while loop? What is a sentinel value? How do you pick a good sentinel value? Would you ever intentionally want an infinite loop? Why shouldn’t you generally use floats or doubles as control variables in loops? What are the two possible values of a Boolean variable?

For Loops

Parts of a Loop (reminder) Every loop will always contain three main elements: –Priming: initialize your variables. –Testing: test against some known condition. –Updating: update the variable that is tested.

Loop types (reminder) Indefinite Loop: –You do not know ahead of time how many times your loop will execute. –For example, you do not know how many books a person might order. Definite Loop: –You know exactly how many times you want the loop to execute. –not at compile time necessarily

For loops Another type of loop in Java is the for loop. It is very good for definite loops All the parts (priming, testing and updating) are in one place. format: for (prime expression; test expression; update expression) Since the expressions are all in one place, many people prefer for to while when the number of iterations is known.

Basic For Loop Syntax For loops are good for creating definite loops. int counter; for (counter =1;counter <= 10;counter++) System.out.println (counter); 1. Priming: Set the start value. 2. Test Condition: Set the stop value. 3. Update: Update the value. Note that each section is separated by a semicolon.

for Loop Flowchart 1. Priming Set counter=1 2. Test counter <= 10 3a. print counter 3b. Update counter++; TRUE FALSE

Infinite Loop You can still end up with an infinite loop when using for loops for (counter = 0; counter <= 10; counter--)

For Loop Variations The limit can be a variable: for ( i = 1; i <= limit; i++) –This counts from 1 to limit Update may be negative: for (i = 100; i >= 1; i--) –This counts from 100 to 1. Update may be greater than 1: for (i = 100; i >= 5; i -= 5) –This counts from 100 to 5 in steps of 5

The for Structure: Notes and Observations Arithmetic expressions –Initialization, loop-continuation, and increment can contain arithmetic expressions. If x equals 2 and y equals 10 for ( j = x; j <= 4 * x * y; j += y / x ) is equivalent to for ( j = 2; j <= 80; j += 5 ) Notes about the for structure: –If the loop continuation condition is initially false The body of the for structure is not performed Control proceeds with the next statement after the for structure –Control variable Often printed or used inside for body, but not necessary  2000 Prentice Hall, Inc. All rights reserved.

The Comma Operator Commas known here as comma operators by using commas, you can put more than one statement in priming or updating expression for (i = 100, y = 0; i >= 1; i--) or for (index; j + i <= 10; ) { code; } Commas known here as comma operators  2000 Prentice Hall, Inc. All rights reserved. Modified by Evan Korth

Warnings Do not use a float or double for the counter –May result in imprecise counter values and faulty evaluation for loop termination purposes Don’t use commas instead of semicolons to separate the components of the for loop –(very common error) As in the if and while, do not put a semicolon ; right after the parentheses – will be an empty loop!

Off-by-one error In the first example, shown here, if had written counter < 10 then loop would execute 9 times, not the desired 10 times for (counter = 1; counter <= 10; counter++) { System.out.println (counter); } /* end for counter */

Help avoid off-by-one errors Try to make your conditions in the form <= not < –Avoid code like counter < 11 or counter < 10

Good Programming Practices Do not change the value of the counter variable in your loop. –Do not attempt to manually adjust it to force an exit –Can cause subtle errors that are difficult to catch –Instead change your logic Do not put other expressions in the for control structure –Manipulations of other variables should appear before or within the body of the loop depending on whether or not you want to repeat them Put a blank line before and after each major control structure Try to limit nesting to three levels, if possible –More than three levels of indentation can get confusing Limit the for control header to one line if possible