Download presentation
Presentation is loading. Please wait.
Published byRuby Jefferson Modified over 9 years ago
1
ME 142 Engineering Computation I Loops
2
Key Concepts Looping Basics For… Next Statement Do… Loop Statement For Each… Next Statement
3
Looping Basics
4
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
5
For...Next Statement
6
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]
7
For… Next Example For I = 1 to 5 Cells(I,1) = I Next I
8
For… Next Example For I = 1 to 5 ActiveCell.Value = I ActiveCell.Offset(1,0).Select Next I
9
For… Next Example Sub 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
10
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.
11
Example Problem Soln Function factorial(n) 'computes the factorial of a number Total = 1 For i = 1 To n Total = Total * i Next i factorial = Total End Function
12
Do...Loop Statement
13
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.
14
Do… Loop Statement Do [While condition] [statements] Loop Do [statements] Loop [While condition] Do [Until condition] [statements] Loop Do [statements] Loop [Until condition] Do [statements] Loop
15
Do… Loop Example x = 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
16
Do… Loop Example x = 10 Do Cells(x,1) = x x = x + 1 Loop Until x = 5 Use Ctrl+Break or Esc to stop execution of infinite loop
17
Do… Loop Example x = 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
18
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.
19
Example Problem Soln Function factorial(n) 'computes the factorial of a number Total = 1 I = 1 Do While I <= n Total = Total * I I = I + 1 Loop factorial = Total End Function
20
For Each...Next Statement
21
For Each… Next Statement The 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]
22
For Each… Next Example Function ForEachDemo(MyRange) Total = 0 For Each Cell In MyRange Total = Total + Cell.Value Next Cell ForEachDemo = Total End Function
23
For Each… Next Example Sub FillDemo() ‘Fill a selection with random numbers For Each cell In Selection cell.Value = Rnd Next cell End Sub
24
Review Questions
25
Review Question What is the value of Count after the loop has finished executing: Count=0 For I = 1 to 5 Count = Count + I Next I A.0 B.5 C.10 D.15 E.None of the above
26
Review Question What is the value of Count after the loop has finished executing: x=1 Count=0 Do Count = Count + x x = x + 1 Loop Until x = 5 A.0 B.5 C.10 D.15 E.None of the above
27
Homework Help ‘n Hints
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.