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

25
Iteration Statements Lather Rinse Repeat
  • date post

    19-Dec-2015
  • Category

    Documents

  • view

    218
  • download

    0

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

Iteration Statements

LatherRinse

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-EachShow address of each cell in Range:

For Each cell In _Worksheets("Sheet1").Range("A1:A10") MsgBox cell.Address Next cell

Second Example of For Each

Message for each negative cellFor Each cell In _Worksheets("Sheet1").Range("A1:A10") If cell.Value < 0 Then MsgBox cell.Address & " is Negative" End If Next cell

Third ExampleFor 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 endStatements

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 10MsgBox(“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 = 5m = 0For j = 1 to k m = j + mNext j

k

jj

1

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 = 2k = 10j = 0For i = j To k Step m MsgBox(Cstr(i) & “ is an even number”)Next i

A.A. 1010B.B. 99C.C. 11D.D. 1111E. E. None of the None of the

AboveAbove

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 TableFor I = 1 to 10 For J = 1 to 10 msg = msg & Str(I*J) & “ “ Next J msg = msg & Chr(13)Next I

mj

k

1i

i

1

5

ijji

1

10

1

10

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 = 100For j = 1 To 100

For i = 1 To kMsgBox(“Hello”)

Next iNext j

A.A. 100100B.B. 10001000C.C. 11D.D. 10,00010,000E.E. None of the None of the

aboveabove

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 conditionstatements 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 + 1Wend

Second Example of a While Loop

Asking the user for a number greater than 10

X = 0While (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 = 0c = 10While(a < 10)

a = 2 * cc = c + 1

Wend

A.A. 1010B. B. 99C.C. 2020D.D. 1111E.E. 2121

Question:

Conclusion We have two basic iteration

(looping) statements For and While Using them we can perform

repetitive operations We can accumulate values

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.