Introduction To Repetition The for loop

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

While loops.
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Chapter 5: Control Structures II (Repetition)
Chapter 5: Loops and Files.
Loops Repeat after me …. Loops A loop is a control structure in which a statement or set of statements execute repeatedly How many times the statements.
 2007 Pearson Education, Inc. All rights reserved C Program Control.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
5.05 Apply Looping Structures
CS0007: Introduction to Computer Programming Introduction to Arrays.
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
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,
CPS120 Introduction to Computer Science Iteration (Looping)
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.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 6 Looping.
CPS120: Introduction to Computer Science Decision Making in Programs.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Chapter 5 Control Structure (Repetition). Objectives In this chapter, you will: Learn about repetition (looping) control structures Explore how to construct.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
CSIS 113A Lecture 5 Random Numbers, while, do-while.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Pascal Programming Pascal Loops and Debugging. Pascal Programming Pascal Loops In our first brush with the while do loops, simple comparisons were used.
CPS120 Introduction to Computer Science Iteration (Looping)
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
Loops and Files. 5.1 The Increment and Decrement Operators.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
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.
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Loops Tonga Institute of Higher Education. Introduction Programs need to be able to execute tasks repeatedly. Use loops to repeat actions  For Loop 
Loops ( while and for ) CSE 1310 – Introduction to Computers and Programming Alexandra Stefan 1.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Follow up from lab See Magic8Ball.java Issues that you ran into.
Introduction to Computer Programming
Chapter 4 – C Program Control
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Lecture 6 Repetition Richard Gesick.
CS161 Introduction to Computer Science
Programming Logic and Design Fourth Edition, Comprehensive
Review If you want to display a floating-point number in a particular format use The DecimalFormat Class printf A loop is… a control structure that causes.
Java Programming: Guided Learning with Early Objects
Lecture 07 More Repetition Richard Gesick.
Lecture 4B More Repetition Richard Gesick
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Control Structures - Repetition
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Lecture 4A Repetition Richard Gesick.
Arrays, For loop While loop Do while loop
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Chapter 4 - Program Control
Chapter 6: Repetition Statements
CSC115 Introduction to Computer Programming
Seating “chart” Front - Screen rows Back DOOR.
Repetition Statements (Loops) - 2
Based on slides created by Bjarne Stroustrup & Tony Gaddis
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.
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

Introduction To Repetition The for loop CSIS 113A Introduction To Repetition The for loop

What is Iteration CS term for repeating a set of actions Also called repetition or looping Statements used in iteration are called loops Iteration versus Selection Like the if statement, iteration evaluates a condition If the condition is true, a group of statements are executed If the condition is false, the statements are skipped Don't confuse iteration and selection Don't use an if statement where you should use a loop Loops repeat, “if” statements don'

Iteration Depicted

C++ And Iteration C++ has three loops: while, do-while, and for Each varies in way used and place best employed One difference is where the test is made Test at the bottom Test at the top { // Loop Body } { // Loop Body } Loop Test Loop Test

Loop Classification Where tested is not best classification Better to ask how each loop is controlled Two basic methods of loop control Simplest method relies on a counter These are called counting or counted loops "Repeat these actions 10 times" More complex loops test the occurrence of a particular event, or combination of events. These are called indefinite or indeterminate loops

Indefinite Loops Three common kinds of indefinite loops Data loops Keep going until there is no more data to process Used when reading files or sending Web pages Sentinel loops Keep going until you see a particular value Used when searching or sorting Limit loops Process until the answer is "close enough" Often used in scientific and graphics programming

How To Write Loops Loops are a common source of errors in code You must have a loop-building strategy Gives you some place to start Step-by-step approach helps avoid errors Strategy designed by Doug Cooper Professor at Berkeley who wrote "Oh Pascal" Will help you write correct loops first time

Four Sides Of A Loop Actions that occur before the loop is entered Called the precondition Test that "runs" the loop Called the bounds Actions in the body Called the operation Actions that occur when the loop is complete Called the postcondition // Before the loop The Loop Bounds { // Inside the loop } // After the loop

The Bounds And The Goal First step is to describe, understand, and separate the bounds and goal of loop Count the characters in a String until a period is encountered. Assume the String contains a period. The goal of the loop? Count the characters that precede a period [sentence] The bounds of the loop? A period was encountered Can use same bounds, but have different goal Print characters in a String until a period is encountered

Step 1: Determine the Bounds What's the first thing you need to know when using a new appliance or software package? How to turn it off !!! Same thing is true for loops. Begin by asking: "How do I make this loop quit?" Correctly stopping your loop is the single most important step in writing a correct loop Write condition using pseudo-code

Step 1: Determining the Bounds // Before the loop While C is not a period { // Inside the loop } // After the loop

Step 2: The Preconditions Next, ask yourself "How do I get into the loop?" Precondition statements usually involve: Creating variables needed by the bounds Initializing them to some reasonable value In our example we have two variables C which is a character and S which is a String Must make sure that C holds a character from S

Step 2: The Preconditions S is a string passed as an argument C is a character Assign the first character from S to C While C is not a period { // Inside the loop }

Step 3: Advance the Loop Add statements that move you closer to bounds Suppose we didn't add any statements to loop If S began with a period, loop would not be entered and program would perform correctly Otherwise, once the loop was entered, nothing would change the value of C, so the loop would repeat over and over again This is called an endless loop Solution: store next character from S in C

Step 3: Advance the Loop S is a string passed as an argument C is a character Assign the first character from S to C While C is not a period { Assign next character in S to C }

Loop-Building Part I Ensure the loop works before putting it to work Step 1: Write the loop bounds Check for unavoidable and impossible conditions Step 2: Write the loop preconditions Add and initialize any variables used in the loop's conditional test Step 3: Advance the loop Write statements in the loop body that move you toward the loop bounds

Loop Building Part II: The Goal Step 4: Start with the goal preconditions Usually involves creating and initializing variables Counters: used to count something Accumulators: used to add up something In our program we need: A counter to hold the number of characters The counter should be initialized to 0

Loop Building Part II: The Goal Step 5: Perform necessary operations in body Usually involves updating variables or making tests In our case, we need to update our counter each time we go through the loop Step 6: Make postcondition tests Loop exits because bound is reached, not because the goal is necessarily accomplished In our case [counter] no test is necessary If averaging, for instance, would need to make sure that the loop had actually been entered

The Finished Loop Plan Variable S is a String, passed as an argument Variable C is a character Variable counter is an integer, initialized to 0 Assign the first character in S to C while C is not a period { Add 1 to Counter Assign the next character in S to C } Counter holds number of characters before period in S

Loop Syntax Now that you have a "plan", let's look at syntax You'll learn how to: Write counted loops with the for statement Write counted loops with the while statement Write indefinite loops using the while statement Write loops that use the do-while statement How to use the break and continue statements

Counted Loops A counted loop is simplest kind of loop The loop bounds is controlled by a counter Use for "perform this action 10 times" C++’s for loop specializes in this for (int count = 0; count < 10; count++) { lblOut.Text =“count = “ + count.toString(); }

Counting Loops with for The loop header and body 3 condition expressions: initialize, test, and update

Counting Down The for loop is a general purpose loop Don't have to use a simple, incrementing counter You don't have to count up: for (int i = 10; i > 0; i--) { … } Works like Pascal's DOWNTO command Use this to reverse the characters in a string Note that strings are numbered starting with 0

Counting by Steps You don't have to count by one: for (int i = 0; i < 50; i += 5) { … } Works like BASIC's STEP command Not restricted to a constant step, like BASIC for (int i = 2; i < 10000 ; i * = 2 ) { Console.Writeline(i.toString() + “\n”); }

Initialization Variations The initialization and update expressions can initialize and update more than one variable Just separate each expression with a comma Only place this is allowed Multiple initializations: for (int i = 0, j=10; // notice, 1 int (i+j) < 100; i++, j++) {...}