Definite Loops Instructor Rob Nash. What is Definite Looping? A loop: a block of code that is repeated a number of times A definite loop: a block of code.

Slides:



Advertisements
Similar presentations
Control Structures. Decision Making Structures The if and if…else are all selection control structures that introduce decision-making ability into a program.
Advertisements

Repetition Chapter 4: Control structures. Introduction to OOPDr. S. GANNOUNI & Dr. A. TOUIRPage 2 Loop Statements After reading and studying this Section,
 Instructor Nash  Readings: Savitch, Chapter 3 (Section 3)  CSS 161 V2.0.
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 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos:
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Chapter 5: Loops and Files.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Looping Yong Choi School of Business CSU, Bakersfield.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Repetition Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
While Loops and Do Loops. Suppose you wanted to repeat the same code over and over again? System.out.println(“text”); System.out.println(“text”); System.out.println(“text”);
Lecture Set 5 Control Structures Part D - Repetition with Loops.
Jaeki Song ISQS6337 JAVA Lecture 04 Control Structure - Selection, and Repetition -
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
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.
Loops and Files. 5.1 The Increment and Decrement Operators.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
For Loop Tips And Tricks
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Application development with Java Lecture 6 Rina Zviel-Girshin.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Iteration & Loop Statements 1 Iteration or Loop Statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
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.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
REPETITION MTS3033 OBJECT ORIENTED PROGRAMMING 1.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Lesson 7 Iteration Structures. Iteration is the third control structure we will explore. Iteration simply means to do something repeatedly. All iteration.
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.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Chapter 9 Repetition.
Lecture 4b Repeating With Loops
Chapter 4 Repetition Statements (loops)
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Chapter 6 More Conditionals and Loops
CHAPTER 5A Loop Structure
CiS 260: App Dev I Chapter 4: Control Structures II.
Repetition Structures (Loops)
JavaScript: Control Statements.
JavaScript: Control Statements I
Chapter 5 Repetition.
Programming Fundamentals Lecture #6 Program Control
Outline Altering flow of control Boolean expressions
Structured Program Development in C
© A+ Computer Science - What is a LOOP? © A+ Computer Science -
Lab5 PROGRAMMING 1 Loop chapter4.
Alternate Version of STARTING OUT WITH C++ 4th Edition
Building Java Programs
Based on slides created by Bjarne Stroustrup & Tony Gaddis
PROGRAM FLOWCHART Iteration Statements.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Building Java Programs
Learning Plan 4 Looping.
Loops CGS3416 Spring 2019 Lecture 7.
Programming Fundamental
Looping and Repetition
Presentation transcript:

Definite Loops Instructor Rob Nash

What is Definite Looping? A loop: a block of code that is repeated a number of times A definite loop: a block of code that is repeated a finite number of times, known in advance LoopFor x = 1 to 10 in increments of 1 { … }

The While Loop while( condition ) { //increment counter or change condition } //this loop continues while condition is true int x = 0; while( x < 10 ) { //do stuff x = x + 1 }

The For Loop For( expression1 ; expression2; expression 3) exp1: initialization of loop counter exp2: loop continuation test exp3: increment that occurs at the end of a loop, but before the loop continuation test is evaluated for( int i = 0; i < 10; i = i + 1) for( int x = 0; x < 10; x++) for( int x = 10; x > 0; x--)

For What? Any time you need to repeat work! Need to read in a directory of users? Or, iterate over websites to assign pagerank? Doing a login screen? What if the username and password are incorrect? Loop!

Definite Looping Involves… A loop control variable An initial value for the loop control variable A final or terminal value for the loop control variable An increment or decrement that brings us closer to the terminal value for the control variable. If your loop isn’t converging on the terminal value, you could be stuck in an infinite loop!

For Examples for( int i = 11; i < 77; i = i + 11) for( int i = 100; i > 0; i = i -1) for( int i = -200; i < 150; i += 1) for( int i = 100; i > 0; i = i + 1) Uh, hmmmmm… for( z = 0; z < 10; z = z + a) But, what if z and a are doubles?

Loops and Loop Variables for( int loopCount = 0; loopCount < 3; loopCount++) In the above example, loopCount is our loop variable Often called a control variable, as its value controls or determines when our loop will terminate

Control Structures Defn: a syntactic structure that controls or alters the flow of control. Loops are Repetition Control Structures They alter the flow of control so as to iterate over a block of code many times The exact number of times is known in advance with definite looping Ifs are Selection Control Structures These alter the flow of control so as to select one of multiple code paths while executing This is known as conditional branching If some condition is true, go this way, otherwise branch off that way

Need to Traverse A String? for(int r = 0; r < string.length(); r++) { System.out.println( string.charAt(r)); } Or, backwards… for(int r = string.length() – 1; r>=0; r--) { System.out.println( string.charAt(r)); }

A Running Sum for( int j=0; j <=20; j++ ) { sum += j; //same as sum = sum + j } for( int k =0; k <=30; k++ ) { if( k % 2 == 0 ) { sum = sum + k; }

Nested Loops Need to loop over a loop? All the time! Need to clear a monitor of resolution X by Y. Use a loop within a loop to accomplish this! Or, need to walk over an entire database? One loop would walk over tables The next (inner) loop would step through each row of the database While the final (innermost) loop would iterate over each column in the current row Foreach( table ) { Foreach( row ) { Foreach( column ) {…} } }

Simple Nesting Example for( int y = 0; y < 10; y++ ) { for( int x = 0; x < 5; x++ ) { System.out.print( “*” ); } System.out.println(); }

A Not So Simple Example for( int y = 1; y < 10; y++ ) { for( int x = 1; x < y; x++ ) { System.out.print( “*” ); } System.out.println(); }