Lecture 6: for Loops Yoni Fridman 7/6/01 7/6/01. OutlineOutline  The for statement  The for statement’s format  The for statement’s flow  Comparing.

Slides:



Advertisements
Similar presentations
Java Control Statements
Advertisements

COMP 110: Introduction to Programming Tyler Johnson Feb 11, 2009 MWF 11:00AM-12:15PM Sitterson 014.
Intro to CS – Honors I Control Flow: Loops GEORGIOS PORTOKALIDIS
Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
June 10, 2015ICS102: while & do-while1 while and do-while Statements.
Computer Science A 4 13/3. Goals To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To understand.
Introduction to Computers and Programming Lecture 9: For Loops New York University.
1 Parts of a Loop (reminder) Every loop will always contain three main elements: –Priming: initialize your variables. –Testing: test against some known.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Lecture 2: Variables and Expressions Yoni Fridman 6/29/01 6/29/01.
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.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
TODAY’S LECTURE Review Chapter 2 Go over exercises.
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 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.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
LOOPING What are the 3 structures of writing code? sequential decision repetition Def. Looping is the repetition of statements in a program. There various.
Loops and Iteration for Statements, while Statements and do-while Statements.
CIS 234: LOOPS Adapted from materials by Dr. Donald Bell, 2000 (updated April 2007)
New Tools And Workshop Mod & For Loops. Modulo Calculates the remainder (remember long division?) % Examples: 7 % 3 10 % 2 2 % 3 evaluates to 1 evaluates.
Chapter 7 LOOPING OPERATIONS: ITERATION. Chapter 7 The Flow of the while Loop.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
CS101 Computer Programming I Chapter 4 Extra Examples.
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
CMP-MX21: Lecture 5 Repetitions Steve Hordley. Overview 1. Repetition using the do-while construct 2. Repetition using the while construct 3. Repetition.
Control Structures II: Repetition.  Learn about repetition (looping) control structures  Explore how to construct and use count-controlled, sentinel-controlled,
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 9 & 10 Repetition Statements.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Overview Go over parts of quiz? Another iteration structure for loop.
CPS120 Introduction to Computer Science Iteration (Looping)
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Program Looping Why we need loops in our code –Make code concise for repetitive processes When to use loops –Run a block of code repetitively –Process.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
1 1 Additional Control Structures Chapter 9 2 New and Improved... Ways to branch Ways to write loops Understanding the break and continue statements.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Flow of Control: Loops Module 4. Objectives Design a loop Use while, do, and for in a program Use the for-each with enumerations Use assertion checks.
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
1 Fall 2009ACS-1903 Ch 4 Loops and Files while loop do-while loop for loop Other topics later on …
C Language 1 Program Looping. C Language2 Topics Program looping Program looping Relational operators / expressions Relational operators / expressions.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Loops Tonga Institute of Higher Education. Introduction Programs need to be able to execute tasks repeatedly. Use loops to repeat actions  For Loop 
Follow up from lab See Magic8Ball.java Issues that you ran into.
Lecture 6 Repetition Richard Gesick.
The switch Statement, and Introduction to Looping
Lecture 7: Repeating a Known Number of Times
Chapter 3 Loops Section 3.3 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Loop Structures.
Lecture 4A Repetition Richard Gesick.
Iteration with While You can say that again.
MSIS 655 Advanced Business Applications Programming
Debugging October 3, 2007 ComS 207: Programming I (in Java)
Outline Altering flow of control Boolean expressions
LOOPS BY: LAUREN & ROMEO.
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Debugging October 4, 2006 ComS 207: Programming I (in Java)
Decisions, decisions, decisions
Based on slides created by Bjarne Stroustrup & Tony Gaddis
PROGRAM FLOWCHART Iteration Statements.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
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.
CSC215 Lecture Control Flow.
Chapter 3 Flow of Control Loops in Java.
Chapter 4: Loops and Iteration
Presentation transcript:

Lecture 6: for Loops Yoni Fridman 7/6/01 7/6/01

OutlineOutline  The for statement  The for statement’s format  The for statement’s flow  Comparing while, do, and for ä Loop examples ä Debugging control structures  The for statement  The for statement’s format  The for statement’s flow  Comparing while, do, and for ä Loop examples ä Debugging control structures

The for Statement  Remember this while loop? ä int i = 1; while (i <= 5) { System.out.println(i); System.out.println(i); i++; i++;} ä It can be described as follows: ä initialization while (test) { statement statement update update}  The for loop condenses this: ä for (initialization; test; update) statement statement  Remember this while loop? ä int i = 1; while (i <= 5) { System.out.println(i); System.out.println(i); i++; i++;} ä It can be described as follows: ä initialization while (test) { statement statement update update}  The for loop condenses this: ä for (initialization; test; update) statement statement

The for Statement’s Format for (initialization; test; update) statement initialization is performed once before the loop starts. It initial- izes the counter. statement is any Java statement. It’s executed until test becomes false. test is the condition being tested. It’s result must be boolean. update is performed at the end of each iteration, to update the counter. Intialization, test, and update must be separated by semicolons.

for (initialization; test; update) statement The for Statement’s Flow Start test true End test false

The for Statement (Side Notes) The for Statement (Side Notes) ä The initialization, test, and update need not all operate on the counter variable. ä The initialization and update need not have one statement. ä They can be empty (have no statements), in which case they must be performed elsewhere. The semicolons must still be included. ä They can have multiple statements, separated by commas. ä The test can be empty – it defaults to true.  In this case, the loop must have a break, or it will iterate forever. ä The initialization need not declare the counter.  IMPORTANT: Variables CAN be redeclared within for statements. ä The initialization, test, and update need not all operate on the counter variable. ä The initialization and update need not have one statement. ä They can be empty (have no statements), in which case they must be performed elsewhere. The semicolons must still be included. ä They can have multiple statements, separated by commas. ä The test can be empty – it defaults to true.  In this case, the loop must have a break, or it will iterate forever. ä The initialization need not declare the counter.  IMPORTANT: Variables CAN be redeclared within for statements.

Comparing while, do, and for  Why use for instead of while ? Same old reasons: ä It’s shorter. ä It makes the program more readable – all the vital information (initialization, test, and update) can easily be found in one place. ä So when do you use which loop?  for is used in loops with counters.  while is used in loops without counters.  do is used in loops without counters, when you must guarantee at least one iteration.  Why use for instead of while ? Same old reasons: ä It’s shorter. ä It makes the program more readable – all the vital information (initialization, test, and update) can easily be found in one place. ä So when do you use which loop?  for is used in loops with counters.  while is used in loops without counters.  do is used in loops without counters, when you must guarantee at least one iteration.

Loop Examples  Powers of 2 ( while loop) ä ä  Factorials ( for loop) ä ä  Fibonacci numbers ( for loop) ä ä  Powers of 2 ( while loop) ä ä  Factorials ( for loop) ä ä  Fibonacci numbers ( for loop) ä ä

Debugging Control Structures ä Debugging becomes more complicated once control structures have been introduces. ä When testing your program, test the program on various input data. ä Make sure each statement of your code is executed at least once. ä Put println() statements within control structures. ä Common errors to watch out for:  “Off-by-one” errors, for example, using i < n instead of i <= n. ä Infinite loops.  Never-executed loops or if statements. ä Using the debugger: An example. ä Debugging becomes more complicated once control structures have been introduces. ä When testing your program, test the program on various input data. ä Make sure each statement of your code is executed at least once. ä Put println() statements within control structures. ä Common errors to watch out for:  “Off-by-one” errors, for example, using i < n instead of i <= n. ä Infinite loops.  Never-executed loops or if statements. ä Using the debugger: An example.

HomeworkHomework ä Read: 5.1, 5.2 (bottom of p. 193 to bottom of p. 194), 5.3, 5.4. ä HW3 out today, due Wednesday. ä Black Jack. ä Read: 5.1, 5.2 (bottom of p. 193 to bottom of p. 194), 5.3, 5.4. ä HW3 out today, due Wednesday. ä Black Jack.