Iteration Statements Lather Rinse Repeat. Three Iteration Statements For Each For Next While.

Slides:



Advertisements
Similar presentations
Looping Structures: Do Loops
Advertisements

Introduction to Computing Science and Programming I
CS0004: Introduction to Programming Repetition – Do Loops.
 Control structures  Algorithm & flowchart  If statements  While statements.
Visual Basic.NET BASICS Lesson 10 Do Loops. 2 Objectives Explain what a loop is. Use the Do While and Do Until loops. Use the InputBox function. Use the.
Repeating Actions While and For Loops
Repetition Chapter 4: Control structures. Introduction to OOPDr. S. GANNOUNI & Dr. A. TOUIRPage 2 Loop Statements After reading and studying this Section,
Slide 1 VB Program Flow Control. Slide 2 Making Decisions v Decision Statement: control the execution of parts of the program based on conditions. v The.
Conditional Statements If these were easy then everyone would them!
Iteration (Looping Constructs in VB) Iteration: Groups of statements which are repeatedly executed until a certain test is satisfied Carrying out Iteration.
CSI 101 Elements of Computing Spring 2009 Lecture # 8 Looping and Recursion Wednesday, February 25 th, 2009.
Loops – While, Do, For Repetition Statements Introduction to Arrays
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Repetition Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
1 Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops 6.3 List Boxes and Loops.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
Lecture Set 5 Control Structures Part D - Repetition with Loops.
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.
For Loops (ProjFor1, ProjFor2, ProjFor3, ProjFor4, textbox, textbox1) Please use speaker notes for additional information!
CPS120 Introduction to Computer Science Iteration (Looping)
Repetition Chapter 7. Overview u For Loop u Do Loop  Do While Loop  Do Until Loop  Do Loop While  Do Loop Until u Nested Loops.
ME 142 Engineering Computation I Loops. Key Concepts Looping Basics For… Next Statement Do… Loop Statement For Each… Next Statement.
Compunet Corporation1 Programming with Visual Basic.NET While, Do and For – Next Loops Week 5 Tariq Ibn Aziz.
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.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 4 Looping.
6.2 For…Next Loops General Form of a For…Next Loop
1 Chapter 9. To familiarize you with  Simple PERFORM  How PERFORM statements are used for iteration  Options available with PERFORM 2.
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
CPS120 Introduction to Computer Science Iteration (Looping)
Counting Loops.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 5 Repetition.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Repetition Structures.
CSC 1010 Programming for All Lecture 4 Loops Some material based on material from Marty Stepp, Instructor, University of Washington.
Visual Basic.NET BASICS Lesson 11 List Boxes, For Next Loops, and Label Settings.
Controlling Program Flow with Looping Structures
REPETITION MTS3033 OBJECT ORIENTED PROGRAMMING 1.
Fourth Quarter.  Involves loops or cycles ◦ Loops: means that a process may be repeated as long as certain condition remains true or remains false. ◦
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.
Loops (While and For) CSE 1310 – Introduction to Computers and Programming 1.
5a – While Loops Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming.
Dani Vainstein1 VBScript Session 5. Dani Vainstein2 What we learn last session? Branching Branching using If … Then … Else statement. Branching using.
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 ( while and for ) CSE 1310 – Introduction to Computers and Programming Alexandra Stefan 1.
Chapter 5 - VB 2008 by Schneider1 Chapter 5 – Repetition 5.1 Do Loops 5.2 Processing Lists of Data with Do Loops 5.3 For...Next Loops.
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.
Computer Programming -1-
UNIT 5 Lesson 15 Looping.
Topics Introduction to Repetition Structures
3rd prep. – 2nd Term MOE Book Questions.
Chapter 5: Repetition Structures
Topics Introduction to Repetition Structures
3rd prep. – 2nd Term MOE Book Questions.
Repetition and Loop Statements
Control Structure Senior Lecturer
Outline Altering flow of control Boolean expressions
Chapter (3) - Looping Questions.
Iteration: Beyond the Basic PERFORM
Week 6 CPS125.
Using the Target Variable Inside the Loop
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
A LESSON IN LOOPING What is a loop?
Topics Introduction to Repetition Structures
Introduction to Computer Science
Repetition Statements (Loops) - 2
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

Iteration Statements Lather Rinse Repeat

Three Iteration Statements For Each For Next While

For Each The concept: go through each element in a series of elements. For example, each cell in a range.

Example of For-Each Show address of each cell in Range: For Each cell In _ W orksheets("Sheet1").Range("A1:A10") MsgBox cell.Address Next cell

Second Example of For Each Message for each negative cell For Each cell In _ Worksheets("Sheet1").Range("A1:A10") If cell.Value < 0 Then MsgBox cell.Address & " is Negative" End If Next cell

Third Example For Each cell In _ Worksheets("Sheet1").Range("A1:A10") If cell.Value < 0 Then cell.Interior.Color = RGB(255, 0, 0) End If Next cell

For Statement The For - Next statement allows us to repeat a block of statements a certain number of times Using the For statement we can perform a task repeatedly.

For cnt = start To end Statements Next cnt Syntax of the For Statement This can be added! Step X

Warning ! Never change the value of the counter variable within a For loop. This will cause unexpected (usually bad) results.

Example of a For Loop For i = 1 to 10 MsgBox(“Hello”) Next I This will present 10 message boxes with the word hello in them. An extremely annoying program.

Dim j as Integer, k as Integer, m as Integer k = 5 m = 0 For j = 1 to k m = j + m Next j Second Example of a For Loop In this example, m will have the sum of all the integers from 1 to k.

Third Example of a For Loop Dim i as Integer, j as Integer, k as Integer, m as Integer m = 2 k = 10 j = 0 For i = j To k Step m MsgBox(Cstr(i) & “ is an even number”) Next i

A.10 B.9 C.1 D.11 E. None of the Above Question: How many message boxes will appear? For i = 1 to 10 MsgBox(“Hello”) Next i

Nested For Loops As with If statements, For loops can be nested. The innermost loop will be executed outer loop * inner loop times Interactions between the iteration variables can create many effects.

Example of Nested For Loops Multiplication Table For I = 1 to 10 For J = 1 to 10 msg = msg & Str(I*J) & “ “ Next J msg = msg & Chr(13) Next I

Mathematical Notation Remember that computer languages were first invented to simplify mathematical calculations. The For loop is similar to the mathematical terms pi and sigma.

How many message boxes will appear? k = 100 For j = 1 To 100 For i = 1 To k MsgBox(“Hello”) Next i Next j A.100 B.1000 C.1 D.10,000 E.None of the above Question:

While Loop Sometimes we don’t know exactly how many times we’ll be executing a block of code. Sometimes we just want to continue executing it until we meet a condition.

Syntax of the While Loop While condition statements to execute while condition is true Wend As long as the condition is true the statements inside this expression will continue to be executed.

Example of a While Loop Increase I until its larger or equal to J While (I < J) I = I + 1 Wend

Second Example of a While Loop Asking the user for a number greater than 10 X = 0 While (X < 10) X = Val(InputBox(“Number?”) Wend

Another Example i = 1 While (i < 100 And _ Worksheets("Sheet1").Cells(i, 1).Value <> "") i = i + 1 Wend MsgBox i

What will the variable c equal when this loop is finished? a = 0 c = 10 While(a < 10) a = 2 * c c = c + 1 Wend A.10 B. 9 C.20 D.11 E.21 Question:

Conclusion We have two basic iteration (looping) statements For and While Using them we can perform repetitive operations We can accumulate values

Details on Example Properties: Top Where the top of the control is. Left Where the left of the control is. Forms have coordinate system.

Animation Moving from point A to point B in small steps animates it. Using a For loop we can animate controls. DoEvents required to redraw control between steps.