Upload
ruby-jefferson
View
223
Download
0
Tags:
Embed Size (px)
Citation preview
ME 142Engineering Computation I
Loops
Key Concepts
Looping Basics
For… Next Statement
Do… Loop Statement
For Each… Next Statement
Looping Basics
Looping Basics
The term loop or looping refers to repeating a block of code multiple times.
Must be able to end loop – otherwise infinite loop!! Execute pre-determined number of times Execute until a condition is satisfied
For...Next Statement
For… Next Statement
The For… Next statement is used to repeatedly execute a block of statements a specified number of times.
For counter = start to end [Step stepsize]
[statements]
Next [counter]
For… Next Example
For I = 1 to 5
Cells(I,1) = I
Next I
For… Next Example
For I = 1 to 5
ActiveCell.Value = I
ActiveCell.Offset(1,0).Select
Next I
For… Next ExampleSub demo()
‘Computes average of cells A1:A5
Total = 0
For i = 1 To 5
Total = Total + Cells(i, 1)
Next I
Average = Total / 5
MsgBox "Average = " & Average
End Sub
Example Problem Create a factorial function using For Next
loops. Pass the function the number whose factorial is to be computed. After computing the factorial, the results should be displayed in the cell from which the function is called.
Use the factorial function you created to compute the factorials for the integers 1 through 10.
Example Problem SolnFunction factorial(n)
'computes the factorial of a number
Total = 1
For i = 1 To n
Total = Total * i
Next i
factorial = Total
End Function
Do...Loop Statement
Do… Loop Statement
The Do… Loop statement is used to repeatedly execute a block of statements until a specified condition is met. Conditions are tested using While and Until at either the beginning or end of the loop.
Do… Loop StatementDo [While condition]
[statements]
Loop
Do
[statements]
Loop [While condition]
Do [Until condition]
[statements]
Loop
Do
[statements]
Loop [Until condition]
Do
[statements]
Loop
Do… Loop Examplex = 1
Do
Cells(x,1) = x
x = x + 1
Loop Until x = 5
x = 1
Do Until x =5
Cells(x,1) = x
x = x + 1
Loop
Do… Loop Examplex = 10
Do
Cells(x,1) = x
x = x + 1
Loop Until x = 5
Use Ctrl+Break or Esc to stop execution of infinite loop
Do… Loop Examplex = 10
Do
Cells(x,1) = x
x = x + 1
Loop Until x> = 5
x = 10
Do Until x >=5
Cells(x,1) = x
x = x + 1
Loop
Example Problem Create a factorial function using Do loops.
Pass the function the number whose factorial is to be computed. After computing the factorial, the results should be displayed in the cell from which the function is called.
Use the factorial function you created to compute the factorials for the integers 1 through 10.
Example Problem SolnFunction factorial(n)'computes the factorial of a number
Total = 1I = 1
Do While I <= n Total = Total * I I = I + 1Loop
factorial = Total
End Function
For Each...Next Statement
For Each… Next StatementThe For Each…Next statement is used to loop through
each object in a collection of objects. An example of this would be to loop through each of the cells in a range of cells.
For Each element In collection
[statements]
Next [element]
For Each… Next ExampleFunction ForEachDemo(MyRange)
Total = 0
For Each Cell In MyRange
Total = Total + Cell.Value
Next Cell
ForEachDemo = Total
End Function
For Each… Next ExampleSub FillDemo()
‘Fill a selection with random numbers
For Each cell In Selection
cell.Value = Rnd
Next cell
End Sub
Review Questions
Review Question
What is the value of Count after the loop has finished executing:
Count=0For I = 1 to 5Count = Count + I
Next I
A.0
B.5
C.10
D.15
E.None of the above
Review Question
What is the value of Count after the loop has finished executing:
x=1Count=0DoCount = Count + xx = x + 1
Loop Until x = 5A.0B.5C.10D.15E.None of the above
Homework Help ‘n Hints