Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Iteration Statements Lather Rinse Repeat. Three Iteration Statements For Each For Next While."— Presentation transcript:

1 Iteration Statements Lather Rinse Repeat

2 Three Iteration Statements For Each For Next While

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

4 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

5 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

6 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

7 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.

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

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

10 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.

11 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.

12 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

13 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

14 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.

15 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

16 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.

17 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:

18 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.

19 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.

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

21 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

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

23 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:

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

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

26 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.


Download ppt "Iteration Statements Lather Rinse Repeat. Three Iteration Statements For Each For Next While."

Similar presentations


Ads by Google