27
ME 142 Engineering Computation I Loops

ME 142 Engineering Computation I Loops. Key Concepts Looping Basics For… Next Statement Do… Loop Statement For Each… Next Statement

Embed Size (px)

Citation preview

Page 1: ME 142 Engineering Computation I Loops. Key Concepts Looping Basics For… Next Statement Do… Loop Statement For Each… Next Statement

ME 142Engineering Computation I

Loops

Page 2: ME 142 Engineering Computation I Loops. Key Concepts Looping Basics For… Next Statement Do… Loop Statement For Each… Next Statement

Key Concepts

Looping Basics

For… Next Statement

Do… Loop Statement

For Each… Next Statement

Page 3: ME 142 Engineering Computation I Loops. Key Concepts Looping Basics For… Next Statement Do… Loop Statement For Each… Next Statement

Looping Basics

Page 4: ME 142 Engineering Computation I Loops. Key Concepts Looping Basics For… Next Statement Do… Loop Statement For Each… Next Statement

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

Page 5: ME 142 Engineering Computation I Loops. Key Concepts Looping Basics For… Next Statement Do… Loop Statement For Each… Next Statement

For...Next Statement

Page 6: ME 142 Engineering Computation I Loops. Key Concepts Looping Basics For… Next Statement Do… Loop Statement For Each… 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]

Page 7: ME 142 Engineering Computation I Loops. Key Concepts Looping Basics For… Next Statement Do… Loop Statement For Each… Next Statement

For… Next Example

For I = 1 to 5

Cells(I,1) = I

Next I

Page 8: ME 142 Engineering Computation I Loops. Key Concepts Looping Basics For… Next Statement Do… Loop Statement For Each… Next Statement

For… Next Example

For I = 1 to 5

ActiveCell.Value = I

ActiveCell.Offset(1,0).Select

Next I

Page 9: ME 142 Engineering Computation I Loops. Key Concepts Looping Basics For… Next Statement Do… Loop Statement For Each… Next Statement

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

Page 10: ME 142 Engineering Computation I Loops. Key Concepts Looping Basics For… Next Statement Do… Loop Statement For Each… Next Statement

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.

Page 11: ME 142 Engineering Computation I Loops. Key Concepts Looping Basics For… Next Statement Do… Loop Statement For Each… Next Statement

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

Page 12: ME 142 Engineering Computation I Loops. Key Concepts Looping Basics For… Next Statement Do… Loop Statement For Each… Next Statement

Do...Loop Statement

Page 13: ME 142 Engineering Computation I Loops. Key Concepts Looping Basics For… Next Statement Do… Loop Statement For Each… Next 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.

Page 14: ME 142 Engineering Computation I Loops. Key Concepts Looping Basics For… Next Statement Do… Loop Statement For Each… Next Statement

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

Page 15: ME 142 Engineering Computation I Loops. Key Concepts Looping Basics For… Next Statement Do… Loop Statement For Each… Next Statement

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

Page 16: ME 142 Engineering Computation I Loops. Key Concepts Looping Basics For… Next Statement Do… Loop Statement For Each… Next Statement

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

Page 17: ME 142 Engineering Computation I Loops. Key Concepts Looping Basics For… Next Statement Do… Loop Statement For Each… Next Statement

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

Page 18: ME 142 Engineering Computation I Loops. Key Concepts Looping Basics For… Next Statement Do… Loop Statement For Each… Next Statement

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.

Page 19: ME 142 Engineering Computation I Loops. Key Concepts Looping Basics For… Next Statement Do… Loop Statement For Each… Next Statement

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

Page 20: ME 142 Engineering Computation I Loops. Key Concepts Looping Basics For… Next Statement Do… Loop Statement For Each… Next Statement

For Each...Next Statement

Page 21: ME 142 Engineering Computation I Loops. Key Concepts Looping Basics For… Next Statement Do… Loop Statement 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]

Page 22: ME 142 Engineering Computation I Loops. Key Concepts Looping Basics For… Next Statement Do… Loop Statement For Each… Next Statement

For Each… Next ExampleFunction ForEachDemo(MyRange)

Total = 0

For Each Cell In MyRange

Total = Total + Cell.Value

Next Cell

ForEachDemo = Total

End Function

Page 23: ME 142 Engineering Computation I Loops. Key Concepts Looping Basics For… Next Statement Do… Loop Statement For Each… Next Statement

For Each… Next ExampleSub FillDemo()

‘Fill a selection with random numbers

For Each cell In Selection

cell.Value = Rnd

Next cell

End Sub

Page 24: ME 142 Engineering Computation I Loops. Key Concepts Looping Basics For… Next Statement Do… Loop Statement For Each… Next Statement

Review Questions

Page 25: ME 142 Engineering Computation I Loops. Key Concepts Looping Basics For… Next Statement Do… Loop Statement For Each… Next Statement

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

Page 26: ME 142 Engineering Computation I Loops. Key Concepts Looping Basics For… Next Statement Do… Loop Statement For Each… Next Statement

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

Page 27: ME 142 Engineering Computation I Loops. Key Concepts Looping Basics For… Next Statement Do… Loop Statement For Each… Next Statement

Homework Help ‘n Hints