1 CS 106, Winter 2009 Class 14, Section 4 Slides by: Dr. Cynthia A. Brown, Instructor section 4: Dr. Herbert G. Mayer,

Slides:



Advertisements
Similar presentations
Introduction to Computing Science and Programming I
Advertisements

CHAPTER 5: LOOP STRUCTURES Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Repetition Chapter 4: Control structures. Introduction to OOPDr. S. GANNOUNI & Dr. A. TOUIRPage 2 Loop Statements After reading and studying this Section,
11 CS 106, Winter 2009 Class 20, Section 4 Slides by: Dr. Cynthia A. Brown, Instructor section 4: Dr. Herbert G. Mayer,
1 CS 106, Winter 2009 Class 10, Section 4 Slides by: Dr. Cynthia A. Brown, Instructor section 4: Dr. Herbert G. Mayer,
1 CS 106, Winter 2009 Class 19, Section 4 Slides by: Dr. Cynthia A. Brown, Instructor section 4: Dr. Herbert G. Mayer,
1 CS 106, Winter 2009 Class 11, Section 4 Slides by: Dr. Cynthia A. Brown, Instructor section 4: Dr. Herbert G. Mayer,
1 CS 106, Winter 2009 Class 8, 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,
Computer Science 1620 Loops.
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:
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Objectives You should be able to describe:
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 Chapter 6 Repetition Statements.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
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.
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.
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.
© 2006 Pearson Education 1 More Operators  To round out our knowledge of Java operators, let's examine a few more  In particular, we will examine the.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Chapter 4 Loops Write code that prints out the numbers Very often, we want to repeat a (group of) statement(s). In C++, we have 3 major ways of.
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.
L OO P S While writing a program, there may be a situation when you need to perform some action over and over again. In such situation you would need.
Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled.
6.2 For…Next Loops General Form of a For…Next Loop
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
JavaScript, Fourth Edition
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
CMSC 104, Version 9/011 More Loops Topics Counter-Controlled (Definite) Repetition Event-Controlled (Indefinite) Repetition for Loops do-while Loops Choosing.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos:
CPS120 Introduction to Computer Science Iteration (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:
Repetition Statements (Loops) The do while Loop The last iteration structure in C++ is the do while loop. A do while loop repeats a statement or.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Building Java Programs Chapter 2 Primitive Data and Definite Loops Copyright (c) Pearson All rights reserved.
1 b Boolean expressions b truth tables b conditional operator b switch statement b repetition statements: whilewhile do/whiledo/while forfor Lecture 3.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
REPETITION MTS3033 OBJECT ORIENTED PROGRAMMING 1.
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Loops Tonga Institute of Higher Education. Introduction Programs need to be able to execute tasks repeatedly. Use loops to repeat actions  For Loop 
Programming in Java (COP 2250) Lecture 12 & 13 Chengyong Yang Fall, 2005.
Repetition In today’s lesson we will look at: why you would want to repeat things in a program different ways of repeating things creating loops in Just.
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.
Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied. Suppose you want to execute some code/s 10 times.
Loop Structures.
Arrays, For loop While loop Do while loop
Outline Altering flow of control Boolean expressions
Computing Fundamentals
Building Java Programs
Building Java Programs
Presentation transcript:

1 CS 106, Winter 2009 Class 14, Section 4 Slides by: Dr. Cynthia A. Brown, Instructor section 4: Dr. Herbert G. Mayer,

2 Assignment 5 In Assignment 5 you will practice with procedures and functions The assignment statement and examples are on the website. If you compare the code for Ice Cream Store version 2 and 3, you will be changing the Plane Ticket application in the same way, so that the end result does the same thing as the original but uses general procedures and functions.

3 Chapter 6: Repetition Repetition of the same or similar actions is often needed in process design Three styles of repetition: – Repeat for a fixed number of times – Repeat as long as (while) a certain condition is true – Repeat until a certain condition is true

4 Examples Keep ringing up items as long as (while) the customer has more Keep adding numbers until you get to the end of the list Cut a paycheck for each employee in the roster

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

6 Possible code… num = Cint(txtInput.Text) strNum = txtInput.Text lstMult.Items.Clear() lstMult.Items.Add(strNum&“ x 1 = “ &CStr(num * 1) ) lstMult.Items.Add(strNum&“ x 2 = “ &CStr(num * 2) ) lstMult.Items.Add(strNum&“ x 3 = “ &CStr(num * 3) ) … lstMult.Items.Add(strNum&“ x 12 = “ &CStr(num * 12) )

7 Ugh! This 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 code We couldn’t even do some very natural tasks Luckily, VB has some nice repetition constructs

8 For loop Repetitions are called loops in VB A For loop is used when we can determine the number of repetitions before starting the loop num = Cint(txtInput.Text) strNum = txtInput.Text lstMult.Items.Clear() Forj = I To 12 lstMult.Items.Add(strNum&“ x “ &CStr(j) &“ = “ & _ CStr(num * j)) Next

9 For loop version 2 Let’s modify the previous example so it prints a multiplication table up to N * N, instead of going to 12. num = Cint(txtInput.Text) strNum = txtInput.Text lstMult.Items.Clear() Forj = I To num ‘the limits can be variables or expressions lstMult.Items.Add(strNum&“ x “ &CStr(j) &“ = “ & _ CStr(num * j)) Next

10 For Loop Syntax ForcVar = sValToeVal statements Next Here cVar is the control variable, sVal is the start value for cVar, and eVal is the end value for cVar cVar>eVal? Execute loop statements Increment cVar yes no cVar = sVal

11 Let’s step through this with an example…

12 The Do-While Concept Sometimes we can’t tell in advance how many times a loop will need to execute Or, it might just be clearer to use a logic that checks as we go along In that case we can use a Do loop instead of a For loop

13 Do-While Example Let’s try the same program we did with the For loop, but this time with a Do-While loop num = Cint(txtInput.Text) strNum = txtInput.Text lstMult.Items.Clear() j = 1 Do While j<= 12 lstMult.Items.Add(strNum&“ x “ &CStr(j) &“ = “ & _ CStr(num * j)) j = j + 1 ‘what would happen if I forgot this line? Loop

14 Do-While Example Here’s the n by n version num = Cint(txtInput.Text) strNum = txtInput.Text lstMult.Items.Clear() j = 1 Do While j<= num ‘note the upper bound is now a variable lstMult.Items.Add(strNum&“ x “ &CStr(j) &“ = “ & _ CStr(num * j)) j = j + 1 Loop What happens if num = 0?

15 Do Loop Syntax (While) Do While condition statement(s) Loop (Loop statements may never be done.) Condition true? Execute statements within the loop. Execute statements that follow the loop. No Yes

16 The Do-Until Variation Instead 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. We’ll mostly focus on the Do-While and For loops.

17 Do-Until Example Here’s the multiplication example using a Do-until loop num = Cint(txtInput.Text) strNum = txtInput.Text lstMult.Items.Clear() j = 1 Do lstMult.Items.Add(strNum&“ x “ &CStr(j) &“ = “ & _ CStr(num * j)) j = j + 1 Loop Untilj> 12

18 Do Loop Syntax (Until) Do statement(s) Loop Until condition (Loop statements are always done at least once.) Condition true? Execute statements within the loop. Execute statements that follow the loop. No Yes

19 BREAK 10 minutes

20 Nested Loops Remember 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 loop If we do this, the inner loop is executed completely, for all of its repetitions, each time the outer loop is executed once.

21 Example: Complete Multiplication Table Dimj,kAs Integer DimtableLineAs String Forj= 1 To 12 tableLine = “” Fork= 1 To 12 tableLine = tableLine&CStr(j) &“ x “ _ &CStr(k) &“ = “ &CStr(j*k) &“ “ Next‘k lstMult.Items.Add(tableLine) Next‘j