Fundamentals of Software Development IProgramming patterns involving iteration1 Overview LoopsOverview Loops We’ll also see how loops are often combined.

Slides:



Advertisements
Similar presentations
Execute Blocks of Code Multiple Times Telerik Software Academy C# Fundamentals – Part 1.
Advertisements

1 More on Arrays and Loops Reading for this Lecture: –Section 5.4, , Break and Continue in Loops Arrays and For-each Loops Arrays and Loops.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Fundamentals of Computer Science Lecture 14: Recursion Instructor: Evan Korth New York University.
ITC 240: Web Application Programming
Fundamentals of Software Development 1Slide 1 Today’s Summary Hello World concepts – –main, static, console Programming patterns involving assignment and.
Fundamentals of Software Development 1Slide 1 Programming Patterns: Motivation Many problems fit a “pattern” that experienced software developers recognizeMany.
COMP 14 Introduction to Programming Miguel A. Otaduy May 21, 2004.
Introduction to Computer Science Iteration –the while loop –the for loop Unit 8.
Fundamentals of Computer Science Lecture 14: Recursion Instructor: Evan Korth New York University.
Introducing Loop Statements Liang, pages Loop statements control repeated execution of a block of statements Each time the statements in the block.
Loops – While, Do, For Repetition Statements Introduction to Arrays
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
Java Coding 3 David Davenport Computer Eng. Dept.,
FIT Objectives By the end of this lecture, students should: understand iteration statements understand the differences of for and while understand.
Summary of Loops Programming. COMP102 Prog Fundamentals I: Summary of Loops /Slide 2 Which Loop to Use? l for loop n for calculations that are repeated.
PHYS 2020 Making Choices; Arrays. Arrays  An array is very much like a matrix.  In the C language, an array is a collection of variables, all of the.
© 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 Chapter 6 Repetition Statements.
Arrays, Conditionals & Loops in Java. Arrays in Java Arrays in Java, are a way to store collections of items into a single unit. The array has some number.
TODAY’S LECTURE Review Chapter 2 Go over exercises.
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
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”);
Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.
Recursion A method is recursive if it makes a call to itself. A method is recursive if it makes a call to itself. For example: For example: public void.
Lecture 4 Introduction to Programming. if ( grade ==‘A’ ) cout
DiagrammaticRepresentation Iteration Construct False True Condition Exit from Statement (s) loop Sequence construct Selection construct Statement 1 Statement.
Fundamentals of Software Development 1Slide 1 Loops A loop is:A loop is: –a block of code that executes repeatedly while some condition holds true. Java.
REPETITION CITS1001. Scope of this lecture Repetition for loops while loops 2.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
CSC 142 I 1 CSC 142 Iterations [Reading: chapter 6]
GAME102 - INTRO WHILE LOOPS G. MacKay. Fundamental Control Structures  STRAIGHT LINE  CONDITIONAL  LOOPS.
Overview of Java Loops By: Reid Hunter. What Is A Loop? A loop is a series of commands that will continue to repeat over and over again until a condition.
Chapter 4: Control Structures II
Chapter 5: Control Structures II
Fundamentals of Software Development 1Slide 1 Programming Patterns Many problems fit a “pattern” that experienced software developers recognizeMany problems.
CSC 212 Object-Oriented Programming and Java Part 2.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Fundamentals of Software Development 1Slide 1 Loops A loop is:A loop is: –a block of code that executes repeatedly while some condition holds true. Java.
REPETITION MTS3033 OBJECT ORIENTED PROGRAMMING 1.
Week 10 - Wednesday.  What did we talk about last time?  Method example  Roulette simulation  Types in Java.
CMSC 150 LOOPS CS 150: Fri 20 Jan Representing DNA AGTCCAGTGTCAA.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
LOOPS CHAPTER Topics  Four Types of Loops –while –do…while –for –foreach  Jump Statements in Loops –break –continue 2.
Structured Programming Structured Programming is writing a program in terms of only 3 basic control structures: sequence selection repetition We have already.
Chapter 8: Loops, Arrays, Strings Loop statements –do –while –for Arrays –declaration, allocation, initialization, access –multi-dimensional –heterogeneous.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
CONTENTS Loop Statements Parts of a loop Types of Loops Nested Loops
Loops ( while and for ) CSE 1310 – Introduction to Computers and Programming Alexandra Stefan 1.
© 2004 Pearson Addison-Wesley. All rights reserved October 5, 2007 Arrays ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
Three kinds of looping structures The while loop The for loop The do (also called the do-while) loop All have equivalent power, e.g., if you can write.
UCT Department of Computer Science Computer Science 1015F Iteration
Follow up from lab See Magic8Ball.java Issues that you ran into.
Repetition Statements
Loops A loop is: Java provides three forms for explicit loops:
Introduction to Recursion
Start reading Sec and chapter 7 on loops
Loops in Java.
Programming Patterns Many problems fit a “pattern” that experienced software developers recognize And hence can easily code, from experience Previously,
Chapter 5: Control Structures II
CiS 260: App Dev I Chapter 4: Control Structures II.
Paul Ammann & Jeff Offutt
Loops.
CHAPTER 21 LOOPS 1.
More on iterations using
Looping and Repetition
Presentation transcript:

Fundamentals of Software Development IProgramming patterns involving iteration1 Overview LoopsOverview Loops We’ll also see how loops are often combined with arrays (officially your next topic), e.g., --We’ll also see how loops are often combined with arrays (officially your next topic), e.g., --

Fundamentals of Software Development IProgramming patterns involving iteration2 We’ll review loops –We’ll review loops – –some of the important variations, and –ways you can use them toward certain ends. We’ll also see how loops are often combined with arrays (officially your next topic), e.g., --We’ll also see how loops are often combined with arrays (officially your next topic), e.g., -- –Processing every member of an array –Searching for things in an array –Loops within loops for multi-dimensional tables

Fundamentals of Software Development IProgramming patterns involving iteration3 Loops A loop is:A loop is: –a block of code that executes repeatedly while some condition holds true. Java provides three forms for explicit loops:Java provides three forms for explicit loops: –while –for –do..while The conditions in loops are written as in if statementsThe conditions in loops are written as in if statements The break statement breaks out of the loop that it is withinThe break statement breaks out of the loop that it is within Some loop examples followSome loop examples follow while (condition) { statement; … statement; statement;} for (start; condition; step) { statement; … statement; statement;} do { statement; … statement; … statement; statement; } while (condition);

Fundamentals of Software Development IProgramming patterns involving iteration4 for loops versus while loops for (int i = 0; i < 7; i++) { System.out.println (i + " " + i*i); }for (int i = 0; i < 7; i++) { System.out.println (i + " " + i*i); } is equivalent to is equivalent to int i = 0; while (i < 7) { System.out.println (i + " " + i*i); i++; }int i = 0; while (i < 7) { System.out.println (i + " " + i*i); i++; } Typically we use:Typically we use: –for when we know in advance how many times the loop will execute –while when something that happens in the loop determines when the loop exits –do..while when we want a while loop that always does at least one iteration

Fundamentals of Software Development IProgramming patterns involving iteration5 Loop patterns The next few slides present loop patterns that are often useful, including:The next few slides present loop patterns that are often useful, including: –Do N times Do N times, changing per loop variableDo N times, changing per loop variable –Count –Sum, minimum, maximum –Find-first –Do-forever –Wait-until

Fundamentals of Software Development IProgramming patterns involving iteration6 Loop pattern: “do N times” Example: print k and k 5 to the console window a given number of times: for (int k = 0; k < 5; k++) { System.out.print(k + ″ ″); System.out.print(k + ″ ″); System.out.println( Math.pow((double) k, 5.0) ); System.out.println( Math.pow((double) k, 5.0) ); String s; int start, stop; s = JOptionPane.showInputDialog(″Start at?”) start = Integer.parseInt(s); s = JOptionPane.showInputDialog(″Stop after?”) stop = Integer.parseInt(s); for (int k = start; k <= stop; k++) { System.out.print(k + ″ ″); System.out.print(k + ″ ″); System.out.println( Math.pow((double) k, 5.0)) System.out.println( Math.pow((double) k, 5.0))};

Fundamentals of Software Development IProgramming patterns involving iteration7 Loop pattern: “count” Counts how many times a given character appears in a string:Counts how many times a given character appears in a string: public static int charCount(String s, char c) { int count = 0; for (k = 0; k < s.length(); k++) { if (s.charAt(k) == c) { ++ count; }} return count; }

Fundamentals of Software Development IProgramming patterns involving iteration8 Loop pattern: “sum” Sums the digits in a string of digits:Sums the digits in a string of digits: public static int digitSum(String s) { int digit; int digit; int sum = 0; int sum = 0; for (k = 0; k < s.length(); k++) { for (k = 0; k < s.length(); k++) { digit = Integer.parseInt(s.substring(k, k+1)); digit = Integer.parseInt(s.substring(k, k+1)); sum = sum + digit; sum = sum + digit; } return sum; return sum;}

Fundamentals of Software Development IProgramming patterns involving iteration9 Loop pattern: “maximum” Finds the largest digit in a string of digits:Finds the largest digit in a string of digits: public static int digitMax(String s) { int digit; int digit; int max = Integer.parseInt(s.substring(0, 1)); int max = Integer.parseInt(s.substring(0, 1)); for (k = 1; k < s.length(); k++) { for (k = 1; k < s.length(); k++) { digit = Integer.parseInt(s.substring(k, k+1)); digit = Integer.parseInt(s.substring(k, k+1)); if (digit > max) { if (digit > max) { max = digit; max = digit; } } return max; return max;}

Fundamentals of Software Development IProgramming patterns involving iteration10 Loop Pattern: “find-first” Find the first place where a given character appears in a string. Return -1 if the character does not appear.Find the first place where a given character appears in a string. Return -1 if the character does not appear. public int findChar(String s, char c) {public int findChar(String s, char c) { int i = 0; int i = 0; while (i < s.length()) { while (i < s.length()) { if (s.charAt(i) == c) { if (s.charAt(i) == c) { return i; return i; } i++; i++; } return -1; // Not found return -1; // Not found} for (i=0; i < s.length(); i++) { if (s.charAt(i) == c) { return i; }}

Fundamentals of Software Development IProgramming patterns involving iteration11 Loop Pattern: “do-forever” Our instruction-followers often go “forever”:Our instruction-followers often go “forever”: while (true) { System.out.println("hi");... } while (true) { System.out.println("hi");... } for (; true;) { System.out.println( " hi "); }

Fundamentals of Software Development IProgramming patterns involving iteration12 Loop pattern: “break in middle” while (true) { if (...) { if (...) { break; break; }......}

Fundamentals of Software Development IProgramming patterns involving iteration13 How loops are combined with arrays This is a preview of what you’ll really be studying next!This is a preview of what you’ll really be studying next! Example: Processing every member of an array:Example: Processing every member of an array: for (int k = 0; k < maxDepth; k++) { for (int k = 0; k < maxDepth; k++) { myArray[k] = 0; // Clear out whole array! myArray[k] = 0; // Clear out whole array! }

Fundamentals of Software Development IProgramming patterns involving iteration14 How loops are combined with arrays Preview, cntd:Preview, cntd: Searching for things in an arraySearching for things in an array int k; for (k = 0; k < maxDepth; k++) { if (myArray[k] = 0) { if (myArray[k] = 0) { break; // Find an empty slot in array! break; // Find an empty slot in array! } }

Fundamentals of Software Development IProgramming patterns involving iteration15 How loops are combined with arrays Preview, cntd:Preview, cntd: Loops within loops for multi-dimensional tablesLoops within loops for multi-dimensional tables for (int k = 0; k < maxDepth; k++) { for (int m=0; m<maxWidth; m++) { myTable[k][m] = 0; // Clear out 2-dim table! myTable[k][m] = 0; // Clear out 2-dim table! }}