1 CS 106 Computing Fundamentals II Chapter 61 “Loops” Herbert G. Mayer, PSU CS Status 7/29/2013 Initial content copied verbatim from CS 106 material developed.

Slides:



Advertisements
Similar presentations
CS0004: Introduction to Programming Repetition – Do Loops.
Advertisements

CHAPTER 5: LOOP STRUCTURES Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
Recursion. Recursion is a powerful technique for thinking about a process It can be used to simulate a loop, or for many other kinds of applications In.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
Programming Logic and Design, Third Edition Comprehensive
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
1 CS 106, Winter 2009 Class 14, Section 4 Slides by: Dr. Cynthia A. Brown, Instructor section 4: Dr. Herbert G. Mayer,
1 CS 106, Winter 2009 Class 17, Section 4 Slides by: Dr. Cynthia A. Brown, Instructor section 4: Dr. Herbert G. Mayer,
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Loops – While, Do, For Repetition Statements Introduction to Arrays
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.
Chapter 6 - Visual Basic Schneider
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Adding Automated Functionality to Office Applications.
1 CS 106 Computing Fundamentals II Chapter 25 “Variables, Assignment Statement” Herbert G. Mayer, PSU CS Status 7/4/2013 Initial content copied verbatim.
1 CS 106 Computing Fundamentals II Chapter 7 “Showing Developer Tab” Herbert G. Mayer, PSU CS status 6/17/2013 Initial content copied verbatim from CS.
Copyright 2009 by Pearson Education Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos: Ch.
Lecture Set 5 Control Structures Part D - Repetition with Loops.
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
Chapter 6 - VB 2005 by Schneider1 Chapter 6 – Repetition 6.1 Do While and Do Until Loops 6.2 Processing Lists of Data with Do Loops 6.3 For...Next Loops.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice.
 Wednesday, 9/18/02, Slide #1 CS106 Introduction to CS1 Wednesday, 9/18/02  QUESTIONS?? HW #1 due today at 5!!  Today: Loops, and two new data types.
Visual Basic Programming
PROBLEM SOLVING WITH LOOPS Chapter 7. Concept of Repetition Structure Logic It is a computer task, that is used for Repeating a series of instructions.
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.
1 CS 106 Computing Fundamentals II Chapter 32 “Boolean Expressions” Herbert G. Mayer, PSU CS Status 7/14/2013 Initial content copied verbatim from CS 106.
1 CS 106 Computing Fundamentals II Chapter 84 “Array Formulae” Herbert G. Mayer, PSU CS status 6/14/2013 Initial content copied verbatim from CS 106 material.
6.2 For…Next Loops General Form of a For…Next Loop
1 CS 106 Computing Fundamentals II Chapter 67 “Working With Files” Herbert G. Mayer, PSU CS status 6/24/2013 Initial content copied verbatim from CS 106.
1 Chapter 9. To familiarize you with  Simple PERFORM  How PERFORM statements are used for iteration  Options available with PERFORM 2.
CPS120 Introduction to Computer Science Iteration (Looping)
1 CS 106 Computing Fundamentals II Chapter 79 “Recursion” Herbert G. Mayer, PSU CS status 6/24/2013 Initial content copied verbatim from CS 106 material.
Chapter 7 Problem Solving with Loops
Fourth Quarter.  Involves loops or cycles ◦ Loops: means that a process may be repeated as long as certain condition remains true or remains false. ◦
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
CS 161 Introduction to Programming and Problem Solving Chapter 17 Nested Loops Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim from.
1 CS 106 Computing Fundamentals II Chapter 28 “Scope” Herbert G. Mayer, PSU CS Status 7/14/2013 Initial content copied verbatim from CS 106 material developed.
BO65: PROGRAMMING ITERATION 1. Starter  Write down what you think the code below outputs on screen: Dim intCounter as integer intCounter = 0 Do while.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
1 CS 106 Computing Fundamentals II Chapter 42 “Sub Procedures And Functions” Herbert G. Mayer, PSU CS Status 8/5/2013 Initial content copied verbatim from.
Lesson #5 Repetition and Loops.
Lesson #5 Repetition and Loops.
CS 106 Computing Fundamentals II Chapter 35 “Controls For Choices”
CS 106 Computing Fundamentals II Chapter 77 “Algorithm”
CS 106 Computing Fundamentals II Chapter 33 “Conditional Statements”
( Iteration / Repetition / Looping )
Arrays, For loop While loop Do while loop
Outline Altering flow of control Boolean expressions
Lesson #5 Repetition and Loops.
CS 106 Computing Fundamentals II Chapter 71 “Indexing”
Understanding the Three Basic Structures
CS 106 Computing Fundamentals II Chapter 73 “Ranges”
Iteration: Beyond the Basic PERFORM
CS 106 Computing Fundamentals II Chapter 66 “Working With Strings”
Coding Concepts (Basics)
Chapter 6: Repetition Statements
Herbert G. Mayer, PSU CS Status 8/2/2013
CS 106 Computing Fundamentals II Chapter 64 “For Loop Variations”
ECE 103 Engineering Programming Chapter 19 Nested Loops
A LESSON IN LOOPING What is a loop?
CS 106 Computing Fundamentals II Chapter 69 “Event Loop”
Lesson #5 Repetition and Loops.
The structure of programming
Thinking procedurally
Presentation transcript:

1 CS 106 Computing Fundamentals II Chapter 61 “Loops” Herbert G. Mayer, PSU CS Status 7/29/2013 Initial content copied verbatim from CS 106 material developed by CS professors: Cynthia Brown & Robert Martin

2 Syllabus Repetition Repetition Real-Life Examples Real-Life Examples Multiplication Table Example Multiplication Table Example For Next Loop For Next Loop Do While Do While Do Until Do Until Nested Loops Nested Loops

3 Repetition Repetition of the same or similar actions is often needed in process designRepetition of the same or similar actions is often needed in process design Three styles of repetition:Three styles of repetition: Repeat for a fixed number of times Repeat as long as or while a certain condition is true Repeat until a condition becomes true Repeat for every element of a group or set We’ll look at the first two types in this sessionWe’ll look at the first two types in this session 3

4 Real-Life Examples Keep ringing up items while the customer has moreKeep ringing up items while the customer has more Keep adding numbers until you get to the known end of the listKeep adding numbers until you get to the known end of the list Cut a paycheck for each employee in the rosterCut a paycheck for each employee in the roster 4

5 Multiplication Table Example 5

6 Printing a Multiplication Table Our job: input a number between 1 and 12 in a text box (txtM)Our job: input a number between 1 and 12 in a text box (txtM) Print a multiplication table for this number in a list box (lstAnswer)Print a multiplication table for this number in a list box (lstAnswer) This is possible but painful with our current set of toolsThis is possible but painful with our current set of tools 6

7 Possible code… strM = txtM.Text 'text form of M numM = CInt(strM) 'numeric form of M lstAnswer.Clear‘nothing in the list box lstAnswer.AddItem( strM & " X 1 = " & CStr(numM * 1 ) ) lstAnswer.AddItem( strM & " X 2 = " & CStr(numM * 2 ) ) lstAnswer.AddItem( strM & " X 3 = " & CStr(numM * 3 ) )… lstAnswer.AddItem( strM & " x 12 = " & CStr(numM * 12 ) ) 7

8 Ugh! This is clumsy and unbearably repetitiveThis is clumsy and unbearably repetitive If we wanted to change the upper limit in some way (say do up to 8 * 8, or 10 * 10, instead of going to 12 each time), we would need even uglier codeIf we wanted to change the upper limit in some way (say do up to 8 * 8, or 10 * 10, instead of going to 12 each time), we would need even uglier code Doing a large number of these would be awfulDoing a large number of these would be awful Luckily, VBA has some nice repetition constructsLuckily, VBA has some nice repetition constructs Now for some samples with upper bounds, 4 or 10...Now for some samples with upper bounds, 4 or

9 For Next loop Repetitions are called loops in VBARepetitions are called loops in VBA Loops go through, what we call, iterations; or we say: loops iterateLoops go through, what we call, iterations; or we say: loops iterate A For Next loop is used when we can determine the number of repetitions before starting the loopA For Next loop is used when we can determine the number of repetitions before starting the loop Such determinations can be computable trivially, if the so called upper bound is a constantSuch determinations can be computable trivially, if the so called upper bound is a constant Or else, if the bound is computable at the latest by the start of the first loop iterationOr else, if the bound is computable at the latest by the start of the first loop iteration 9

10 Simple For Next Loop strM = txtM.Text‘ strM = “5” lstAnswer.Clear‘ list box is cleared ’ print the same thing 4 times, i.e. strM For j = 1 To 4‘ iterate 4 times, print same: lstAnswer.AddItem( strM ) Next Prints, for strM = “5”

11 Another Simple For Next Loop lstAnswer.Clear ‘ prints sequence 1..4 ‘ with implied carriage-return after each: For j = 1 To 4 lstAnswer.AddItem( CStr( j ) ) ‘ convert int Next Prints 4 lines:

12 For Next Multiplication Table ‘ see line continuation _ in samples: ‘ other line continations after, ( and before ) numM = CInt( txtM.Text ) strM = txtM.Text lstAnswer.Clear For j = 1 To 10 lstAnswer.AddItem(strM & “ x “ & CStr(j) & “ = “ & _ CStr( numM * j ) ) Next Prints, for numM = 5 5 x 1 = 5 5 x 2 = x 10 = 50 12

13 Do While Concept Sometimes we can’t tell in advance how many times a loop iteratesSometimes we can’t tell in advance how many times a loop iterates Or, it might just be clearer to use a logic that checks as we go alongOr, it might just be clearer to use a logic that checks as we go along In that case we can use a Do While loop instead of a For Next loopIn that case we can use a Do While loop instead of a For Next loop 13

14 Do While Example Use For Next loop program, but this time with a Do While loop numM = CInt(txtM.Text) strM = txtM.Text lstAnswer.Clear j = 1 ‘ set j to initial value 1 at loop start Do While j <= 12‘ upper bund is constant lstAnswer.AddItem( strM & “ x “ & CStr(j) & “ = “ & _ CStr( numM * j ) ) j = j + 1‘ this line makes the loop stop Loop ‘ end with Loop instead of Next 14

15 Do While Example Here’s the numM by numM version numM = CInt(txtM.Text)‘ numM is an integer type strM = txtM.Text‘ strM ix a string type lstAnswer.Clear j = 1 Do While j <= numM‘ upper bound is variable lstAnswer.AddItem( strM & “ x “ & CStr( j ) & “ = “ _ & CStr( numM * j ) ) j = j + 1 Loop What happens if numM = 0? 15

16 Do While Loop Flowchart Do While condition statements including nested Do While Loop Loop statements can execute forever Loop statements can execute forever Condition true? Execute statements within the loop. Execute statements that follow the loop. No Yes 16

17 The Do Until Variation Instead of repeating While a condition is true, we can repeat Until the condition becomes true For example: Do While varA <= 5 Is almost equivalent to: Do Until varA > 5 17

18 Test at the End Variation Instead of testing at the beginning of the loop, we can test at the endInstead of testing at the beginning of the loop, we can test at the end This is useful because, many times, we want to do the loop code at least once, no matter what!This is useful because, many times, we want to do the loop code at least once, no matter what! 18

19 Do Until Example Multiplication example using Do Until loop with test at the end numM = CInt( txtM.Text ) strM = txtM.Text lstAnswer.Clear j = 1 Do lstAnswer.AddItem(strM & “ x “ & CStr(j) & “ = “ & CStr(numM * j)) j = j + 1 Loop Until j > 12 19

20 Do Loop Flowchart (Until, test at end) Dostatement(s) Loop Until condition Do Loop statements always run at least once Do Loop statements always run at least once Condition true? Execute statements within the loop Execute statements that follow the loop No Ye s 20

21 Nested Loops Remember how If statements can be “nested”: you can have an If inside another IfRemember how If statements can be “nested”: you can have an If inside another If We can also put whatever we want inside a loop, including another loopWe can also put whatever we want inside a loop, including another loop If we do this, the inner loop is executed completely, for all of its repetitions, each time the outer loop is executed onceIf we do this, the inner loop is executed completely, for all of its repetitions, each time the outer loop is executed once 21

22 Nested Loops Dim j, k As Integer lstAnswer.Clear For j = 1 To 12 For k = 1 To 4 lstAnswer.AddItem(“j = “ & CStr( j ) & “ k= “ & CStr( k ) ) Next ‘ k Next ‘ j ‘ how many lines of output?? 22

23 Output of Nested Loop j = 1 k = 1 j = 1 k = 2 j = 1 k = 3 j = 1 k = 4 j = 2 k = 1 j = 2 k = 2 j = 2 k = 3 j = 2 k = 4 j = 3 k = 1 Etc. 23

24 Another Nested Loop Dim j, k As Integer lstAnswer.Clear For j = 1 To 12 For k = 1 To 4 lstAnswer.AddItem(“j = “ & CStr( j ) & “ k= “ & CStr( k ) ) Next ‘k lstAnswer.AddItem(“Finished j = “ & CStr( j ) ) lstAnswer.AddItem(“Finished j = “ & CStr( j ) ) Next ‘j 24

25 Results of Another Nested Loop j = 1 k = 1 j = 1 k = 2 j = 1 k = 3 j = 1 k = 4 Finished j = 1 j = 2 k = 1 j = 2 k = 2 j = 2 k = 3 j = 2 k = 4 Finished j = 2 j = 3 k = 1 Etc. 25