Lecture 6 Repetition Richard Gesick.

Slides:



Advertisements
Similar presentations
While loops.
Advertisements

Week 5: Loops 1.  Repetition is the ability to do something over and over again  With repetition in the mix, we can solve practically any problem that.
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Computer Science 1620 Loops.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
Chapter 5: Control Structures II (Repetition)
Loops – While, Do, For Repetition Statements Introduction to Arrays
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
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.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
Chapter 4: Control Structures II
Week 5 - Wednesday.  What did we talk about last time?  Exam 1!  And before that?  Review!  And before that?  if and switch statements.
CPS120 Introduction to Computer Science Iteration (Looping)
CIS 234: LOOPS Adapted from materials by Dr. Donald Bell, 2000 (updated April 2007)
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
Chapter 5 Loops. Overview u Loop Statement Syntax  Loop Statement Structure: while, for, do-while u Count-Controlled Loops u Nested Loops u Loop Testing.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
Control Structures II (Repetition). Objectives In this chapter you will: Learn about repetition (looping) control structures Explore how to construct.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Week 6 - Monday.  What did we talk about last time?  while loop examples  Lab 5.
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.
COMP Loop Statements Yi Hong May 21, 2015.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
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 
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Topic : While, For, Do-While Loop Guided By : Branch : Batch :
Lecture 4b Repeating With Loops
REPETITION CONTROL STRUCTURE
EKT120 COMPUTER PROGRAMMING
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Introduction To Repetition The for loop
Repetition (While-Loop) version]
Chapter 5: Control Structures II
Chapter 5: Control Structures II
Loop Structures.
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Repetition-Counter control Loop
CiS 260: App Dev I Chapter 4: Control Structures II.
Lecture 07 More Repetition Richard Gesick.
Lecture 4B More Repetition Richard Gesick
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Introduction to Functions
Lecture 4A Repetition Richard Gesick.
Iteration with While You can say that again.
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Outline Altering flow of control Boolean expressions
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
Computing Fundamentals
Repetition Statements (Loops) - 2
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.
CprE 185: Intro to Problem Solving (using C)
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Looping and Repetition
Presentation transcript:

Lecture 6 Repetition Richard Gesick

Overview Iteration 4 kinds of good loops Infinite Loops

Looping In computing, we often need to perform the same operations on multiple items. Typically, these tasks follow this pattern: initialize values (set total to 0) process items one at a time (add price to total) report results (report total)   The flow of control that programmers use to complete jobs with this pattern is called looping, or repetition.

Iteration One thing that computers do well is repeat commands Programmers use loops to accomplish this 4 kinds of loops in C# for loop while loop do while loop foreach loop

Criteria for loops Usually have some initial condition Starting a counter Beginning in a certain state Must have a test to continue Must make progress towards finishing

“I will not pour Clorox in the fish tank” Loops in Everyday Life Bad children are told to write sentences on the board “I will not pour Clorox in the fish tank” Have to write this sentence either A certain number of times Until the teacher is happy As many as you can during break

The for Loop Ideal when you know the number of iterations to perform before the loop begins Examples: Find the sum of 5 numbers Find the maximum of 20 numbers Print the odd numbers from 1 to 10

The for loop format: for (<initialization>; <test to continue>; <increment>) { // everything in here is what is repeated // over and over again } Initialization is where the counter is given a starting value The test determines whether or not to continue The increment can be any amount, including negative, and occurs after the loop statements execute

More for Loop Syntax Notes: for ( initialization; loop condition; loop update ) { // loop body } Notes: semicolons separate terms in the loop header no semicolon follows the loop header curly braces are required only if more than one statement is in the loop body

for Loop Flow of Control The initialization statement is executed (once only). The loop condition is evaluated. If the condition is true, the loop body is executed. The loop update statement is executed, and the loop condition is reevaluated (#2). And so on, until the condition is false.

Satisfying the Teacher Example: 1000 sentences? No problem… int counter; for (counter = 1; counter <= 1000; counter++) { Console.WriteLine (“I will not pour Clorox…”); } // Remember, counter++ is the same as // counter = counter + 1

“But I want them numbered!” No problem… int counter; for (counter = 1; counter <= 1000; counter++) { Console.WriteLine (counter + “I will not pour…”); }

Why this works 1 counter int counter; for (counter = 1; counter <= 1000; counter++) { Console.WriteLine (counter + “I will not pour…”); } counter 1

Why this works 1 counter true int counter; for (counter = 1; counter <= 1000; counter++) { Console.WriteLine (counter + “I will not pour…”); } counter true 1

Why this works counter int counter; for (counter = 1; counter <= 1000; counter++) { Console.WriteLine (counter + “I will not pour…”); } Output: 1 I will not pour Clorox in the Fish Tank 1

Why this works 2 counter int counter; for (counter = 1; counter <= 1000; counter++) { Console.WriteLine (counter + “I will not pour…”); } 2

Why this works 2 counter true int counter; for (counter = 1; counter <= 1000; counter++) { Console.WriteLine (counter + “I will not pour…”); } true 2

Why this works counter int counter; for (counter = 1; counter <= 1000; counter++) { Console.WriteLine (counter + “I will not pour…”); } Output: 2 I will not pour Clorox in the Fish Tank 2

Why this works 3 counter int counter; for (counter = 1; counter <= 1000; counter++) { Console.WriteLine (counter + “I will not pour…”); } 3

Why this works 3 counter true int counter; for (counter = 1; counter <= 1000; counter++) { Console.WriteLine (counter + “I will not pour…”); } true 3

Why this works counter int counter; for (counter = 1; counter <= 1000; counter++) { Console.WriteLine (counter + “I will not pour…”); } Output: 3 I will not pour Clorox in the Fish Tank 3

Why this works 4 counter int counter; for (counter = 1; counter <= 1000; counter++) { Console.WriteLine (counter + “I will not pour…”); } 4

When will it end? We see that this will go on for a while It’s a little more interesting later around 1000

Why this works 999 counter true int counter; for (counter = 1; counter <= 1000; counter++) { Console.WriteLine (counter + “I will not pour…”); } true 999

Why this works counter int counter; for (counter = 1; counter <= 1000; counter++) { Console.WriteLine (counter + “I will not pour…”); } Output: 999 I will not pour Clorox in the Fish Tank 999

Why this works 1000 counter int counter; for (counter = 1; counter <= 1000; counter++) { Console.WriteLine (counter + “I will not pour…”); } 1000

Why this works 1000 counter true for last time int counter; for (counter = 1; counter <= 1000; counter++) { Console.WriteLine (counter + “I will not pour…”); } true for last time 1000

Why this works (are we finished?) counter int counter; for (counter = 1; counter <= 1000; counter++) { Console.WriteLine (counter + “I will not pour…”); } Output: 1000 I will not pour Clorox in the Fish Tank 1000

Why this works 1001 counter int counter; for (counter = 1; counter <= 1000; counter++) { Console.WriteLine (counter + “I will not pour…”); } 1001

Why this works 1001 … counter false int counter; for (counter = 1; counter <= 1000; counter++) { Console.WriteLine (counter + “I will not pour…”); } // Jump down here and continue … false 1001

Final Output 1 I will not pour Clorox in the fish tank. 2 I will not pour Clorox in the fish tank. 3 I will not pour Clorox in the fish tank. 4 I will not pour Clorox in the fish tank. . 999 I will not pour Clorox in the fish tank. 1000 I will not pour Clorox in the fish tank.

Another Example of Repetition int num; for ( num = 1 ; num <= 3 ; num++ ) { C.Wln(num + “Potato”); }

num ? int num; for ( num = 1 ; num <= 3 ; num++ ) C.Wln(num+"Potato"); OUTPUT

num 1 int num; for ( num = 1 ; num <= 3 ; num++ ) C.Wln(num+"Potato"); OUTPUT

1 num int num; for ( num = 1 ; num <= 3 ; num++ ) C.O.Wln(num+"Potato"); true OUTPUT

num 1 int num; for ( num = 1 ; num <= 3 ; num++ ) C.O.Wln(num+"Potato"); OUTPUT 1Potato

num 2 int num; for ( num = 1 ; num <= 3 ; num++ ) C.Wln(num+"Potato"); OUTPUT 1Potato

2 num int num; for ( num = 1 ; num <= 3 ; num++ ) C.Wln(num+"Potato"); true OUTPUT 1Potato

num 2 int num; for ( num = 1 ; num <= 3 ; num++ ) C.O.Wln(num+"Potato"); OUTPUT 1Potato 2Potato

num 3 int num; for ( num = 1 ; num <= 3 ; num++ ) C.Wln(num+"Potato"); OUTPUT 1Potato 2Potato

3 num int num; for ( num = 1 ; num <= 3 ; num++ ) C.Wln(num+"Potato"); true OUTPUT 1Potato 2Potato

num 3 int num; for ( num = 1 ; num <= 3 ; num++ ) C.Wln(num+"Potato"); OUTPUT 1Potato 2Potato 3Potato

num 4 int num; for ( num = 1 ; num <= 3 ; num++ ) C.Wln(num+"Potato"); OUTPUT 1Potato 2Potato 3Potato

4 num int num; for ( num = 1 ; num <= 3 ; num++ ) C.Wln(num+"Potato"); false OUTPUT 1Potato 2Potato 3Potato

4 int num; for ( num = 1 ; num <= 3 ; num++ ) C.Wln(num+"Potato"); num false When the loop control condition is evaluated and has value false, the loop is said to be “satisfied” and control passes to the statement following the For statement.

The output was: 1Potato 2Potato 3Potato

int count ; for ( count = 4 ; count > 0 ; count-- ) { C int count ; for ( count = 4 ; count > 0 ; count-- ) { C.Wln(count); } C.Wln(“Done”); OUTPUT: 4 3 2 1 Done

The while Loop The while loop is designed for repeating a set of operations on data items when we don't know how many data items there will be. We will get some signal when we have reached the end of the items to process. The end of data items could be indicated by a special input value called a sentinel value or by reaching the end of a file Receiving the signal is an event; we call this event-controlled looping

The while loop Good for when you don’t know how many times to repeat Teacher says “Write until I’m happy” Has format: while (<boolean value>) { // stuff to repeat over and over }

Operation of the while Loop If the condition evaluates to true, the loop body is executed, then the condition is re-evaluated. As long as the condition evaluates to true, we continue to repeat the loop body. The loop body must "update the loop condition"; that is, it must perform some operation that eventually will cause the loop condition to evaluate to false Typically, the loop update will be an attempt to read the next input value, in order to detect the sentinel value or the end of the file.

Loop Characteristics All loops have the following three characteristics: initialization test update

Count-controlled Loop int count ; count = 4; // initialize loop variable while (count > 0) // test expression { C.Wln(count); // repeated action count -- ; // update loop variable } C.Wln(“Done”);

Count-controlled Loop int count ; count = 4; while (count > 0) { C.Wln(count); count -- ; } C.Wln("Done"); count OUTPUT

Count-controlled Loop 4 int count ; count = 4; while (count > 0) { C.Wln(count); count -- ; } C.Wln("Done"); OUTPUT

Count-controlled Loop 4 int count ; count = 4; while (count > 0) { C.Wln(count); count -- ; } C.Wln("Done"); TRUE OUTPUT

Count-controlled Loop 4 int count ; count = 4; while (count > 0) { C.O.Wln(count); count -- ; } C.O.Wln("Done"); OUTPUT 4

Count-controlled Loop 3 int count ; count = 4; while (count > 0) { C.Wln(count); count -- ; } C.Wln("Done"); OUTPUT 4

Count-controlled Loop 3 int count ; count = 4; while (count > 0) { C.Wln(count); count -- ; } C.Wln("Done"); TRUE OUTPUT 4

Count-controlled Loop 3 int count ; count = 4; while (count > 0) { C.Wln(count); count -- ; } C.Wln("Done"); OUTPUT 4 3

Count-controlled Loop 2 int count ; count = 4; while (count > 0) { C.Wln(count); count -- ; } C.Wln("Done"); OUTPUT 4 3

Count-controlled Loop 2 int count ; count = 4; while (count > 0) { C.Wln(count); count -- ; } C.Wln("Done"); TRUE OUTPUT 4 3

Count-controlled Loop 2 int count ; count = 4; while (count > 0) { C.Wln(count); count -- ; } C.Wln("Done"); OUTPUT 4 3 2

Count-controlled Loop 1 int count ; count = 4; while (count > 0) { C.Wln(count); count -- ; } C.Wln("Done"); OUTPUT 4 3 2

Count-controlled Loop 1 int count ; count = 4; while (count > 0) { C.Wln(count); count -- ; } C.Wln("Done"); TRUE OUTPUT 4 3 2

Count-controlled Loop 1 int count ; count = 4; while (count > 0) { C.Wln(count); count -- ; } C.Wln("Done"); OUTPUT 4 3 2 1

Count-controlled Loop int count ; count = 4; while (count > 0) { C.Wln(count); count -- ; } C.Wln("Done"); OUTPUT 4 3 2 1

Count-controlled Loop int count ; count = 4; while (count > 0) { C.Wln(count); count -- ; } C.Wln("Done"); FALSE OUTPUT 4 3 2 1

Count-controlled Loop int count ; count = 4; while (count > 0) { C.Wln(count); count -- ; } C.Wln("Done"); OUTPUT 4 3 2 1 Done

Example: Re-Writing 1-1000 (using a while loop) counter 1 int counter = 1; while (counter < 1000) { Console.WriteLine (counter + “I will not…”); }

Example: Re-Writing 1-1000 (using a while loop) counter 1 int counter = 1; while (counter < 1000) { Console.WriteLine (counter + “I will not…”); }

Example: Re-Writing 1-1000 (using a while loop) counter 1 int counter = 1; while (counter < 1000) { Console.WriteLine (counter + “I will not…”); } Output: 1 I will not pour Clorox in the fish tank

Example: Re-Writing 1-1000 (using a while loop) counter 1 int counter = 1; while (counter < 1000) { Console.WriteLine (counter + “I will not…”); }

Example: Re-Writing 1-1000 (using a while loop) counter 1 int counter = 1; while (counter < 1000) { Console.WriteLine (counter + “I will not…”); }

Example: Re-Writing 1-1000 (using a while loop) counter 1 int counter = 1; while (counter < 1000) { Console.WriteLine (counter + “I will not…”); } Output: 1 I will not pour Clorox in the fish tank

Example: Re-Writing 1-1000 (using a while loop) counter 1 int counter = 1; while (counter < 1000) { Console.WriteLine (counter + “I will not…”); }

Example: Re-Writing 1-1000 (using a while loop) counter 1 int counter = 1; while (counter < 1000) { Console.WriteLine (counter + “I will not…”); }

Example: Re-Writing 1-1000 (using a while loop) int counter = 1; while (counter < 1000) { Console.WriteLine (counter + “I will not…”); } Output: 1 I will not pour Clorox in the fish tank counter 1

} Infinite Loops This loop isn’t making a lot of progress! Loops that repeat forever are called infinite loops Apparently “lock up” Output: 1 I will not pour Clorox in the fish tank . } Continue forever

Problem Solved… 1 counter int counter = 1; while (counter < 1000) { Console.WriteLine (counter + “I will not…”); counter++; }

Problem Solved… 1 counter int counter = 1; while (counter < 1000) { Console.WriteLine (counter + “I will not…”); counter++; }

Problem Solved… 1 counter int counter = 1; while (counter < 1000) { Console.WriteLine (counter + “I will not…”); counter++; } Output: 1 I will not pour Clorox in the fish tank

Problem Solved… 2 counter int counter = 1; while (counter < 1000) { Console.WriteLine (counter + “I will not…”); counter++; } // Remember, counter++ is the same as // counter = counter + 1

Example: Re-Writing 1-1000 (using a while loop) counter 2 int counter = 1; while (counter < 1000) { Console.WriteLine (counter + “I will not…”); counter++; }

Problem Solved… 2 counter int counter = 1; while (counter < 1000) { Console.WriteLine (counter + “I will not…”); counter++; } 2

Problem Solved… 2 counter int counter = 1; while (counter < 1000) { Console.WriteLine (counter + “I will not…”); counter++; } Output: 2 I will not pour Clorox in the fish tank 2

Problem Solved… 3 counter int counter = 1; while (counter < 1000) { Console.WriteLine (counter + “I will not…”); counter++; } 3

How does it end? counter int counter = 1; while (counter < 1000) { Console.WriteLine (counter + “I will not…”); counter++; } 999

How does it end? counter int counter = 1; while (counter < 1000) { Console.WriteLine (counter + “I will not…”); counter++; } 999

How does it end? counter int counter = 1; while (counter < 1000) { Console.WriteLine (counter + “I will not…”); counter++; } 999

Problem Solved… 999 counter int counter = 1; while (counter < 1000) { Console.WriteLine (counter + “I will not…”); counter++; } Output: 999 I will not pour Clorox in the fish tank 999

How does it end? counter int counter = 1; while (counter < 1000) { Console.WriteLine (counter + “I will not…”); counter++; } 1000

How does it end? counter int counter = 1; while (counter < 1000) { Console.WriteLine (counter + “I will not…”); counter++; } 1000

How does it end? 1000 counter now false int counter = 1; while (counter < 1000) { Console.WriteLine (counter + “I will not…”); counter++; } // So we never print out // 1000 I will not pour Clorox in the fishtank now false 1000

Another Problem Solved counter int counter = 1; while (counter <= 1000) { Console.WriteLine (counter + “I will not…”); counter++; } now true 1000

Example bool teacherHappy = false; int lineNumber = 1; while (!teacherHappy) { Console.WriteLine (lineNumber + “I will not…”); lineNumber++; teacherHappy = attitudeFunction ( ); } // assume attitudeFunction can change // teacherHappy

The do-while loop Similar to while loop Must execute at least one time (test is at bottom) Has format: do { }while (<boolean value>);

Example (count from 1 to 3) counter int counter = 0; do { counter++; Console.WriteLine (counter); } while (counter < 3);

Example (count from 1 to 3) counter int counter = 0; do { counter++; Console.WriteLine (counter); } while (counter < 3);

Example (count from 1 to 3) counter 1 int counter = 0; do { counter++; Console.WriteLine (counter); } while (counter < 3);

Example (count from 1 to 3) counter 1 int counter = 0; do { counter++; Console.WriteLine (counter); } while (counter < 3); Output: 1

Example (count from 1 to 3) counter 1 int counter = 0; do { counter++; Console.WriteLine (counter); } while (counter < 3);

Example (count from 1 to 3) counter 2 int counter = 0; do { counter++; Console.WriteLine (counter); } while (counter < 3);

Example (count from 1 to 3) counter 2 int counter = 0; do { counter++; Console.WriteLine (counter); } while (counter < 3); Output: 2

Example (count from 1 to 3) counter 2 int counter = 0; do { counter++; Console.WriteLine (counter); } while (counter < 3);

Example (count from 1 to 3) int counter = 0; do { counter++; Console.WriteLine (counter); } while (counter < 3); // Note: counter is now 3, but we still have // to finish out the loop – it doesn’t skip counter 3

Example (count from 1 to 3) counter 3 int counter = 0; do { counter++; Console.WriteLine (counter); } while (counter < 3); Output: 3

Example (count from 1 to 3) counter 3 int counter = 0; do { counter++; Console.WriteLine (counter); } while (counter < 3); now false, so loop is finished

Summary for loops good for when you know how many times you want to repeat while and do-while good for when you don’t foreach loop useful for processing collections All loops must finish, or they become infinite loops All loops must have a test to continue, or they become infinite loops