Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.

Slides:



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

© 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.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 6, 2005.
Introduction to Computers and Programming Lecture 9: For Loops New York University.
COMP 14 Introduction to Programming Miguel A. Otaduy May 21, 2004.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
Introduction to Computers and Programming Lecture 7:
Nested For Loops It is also possible to place a for loop inside another for loop. int rows, columns; for (rows=1; rows
1 Parts of a Loop (reminder) Every loop will always contain three main elements: –Priming: initialize your variables. –Testing: test against some known.
Chapter 5: Control Structures II (Repetition)
Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University.
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.
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.
©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.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Chapter 4: Control Structures II
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: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
Loops and Iteration for Statements, while Statements and do-while Statements.
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.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
© 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)
Java iteration statements ● Iteration statements are statements which appear in the source code only once, but it execute many times. ● Such kind of statements.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
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.
Building Java Programs Chapter 2 Primitive Data and Definite Loops Copyright (c) Pearson All rights reserved.
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.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
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)
Lecture 7: Repeating a Known Number of Times
Chapter 4 Control structures and Loops
Outline Altering flow of control Boolean expressions
The for-loop and Nested loops
Repetition Statements (Loops) - 2
Chapter 4 - Program Control
Building Java Programs
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 3 Flow of Control Loops in Java.
Chapter 4: Loops and Iteration
Presentation transcript:

Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in JAVA: V

2 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 items the person will order Definite Loop: –You know exactly how many times you want the loop to execute.

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 are 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; }  2000 Prentice Hall, Inc. All rights reserved.

Warnings Do not use a float or double for the counter –This 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 – this 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

Nested for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.

Nested For Loops It is also possible to place a for loop inside of another for loop. int rows, columns; for (rows=1; rows<=5; rows++) { for (columns=1; columns<=10; columns++) { System.out.print("*"); } System.out.println (); } Outer Loop Inner Loop Output:**************************************************

Nested For Loops, Example #2 //simple loop to print triangle using the character * //Produces a right triangle public class while_numbers { public static void main(String[] args) { int rows, columns; for (rows=1; rows<=5; rows++) { for (columns=1; columns<=rows; columns++) { System.out.print ("*"); } System.out.print ("\n"); } }} Output:*************** Outer Loop Inner Loop