Introduction to Visual BASIC - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Introduction to Visual BASIC

Description:

Example: Adding Sequences. iCnt = 0. iTot = 0. Do While iCnt ... A better solution is to re-write the routine using a loop instead of recursion. Any ideas? ... – PowerPoint PPT presentation

Number of Views:32
Avg rating:3.0/5.0
Slides: 19
Provided by: joe9
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Visual BASIC


1
Introduction to Visual BASIC
  • Loops

2
Q What is a Loop?
  • A control structure that allows for a sequence of
    steps to be repeated a certain number of times
  • This sequence of steps is called the body of the
    loop

3
Q What is a Loop?
  • There are three basic loop structures in
    programming
  • For
  • While
  • Repeat

4
While loop
  • A while loop is a control structure where the
    body is repeated as long as the condition is true

Condition
F
T
Body
5
While loop
  • When the condition is false, the body is
    bypassed, and flow continues with the next part
    of the algorithm

Condition
F
T
Body
6
Example Adding Sequences
  • k ? 0
  • total ? 0
  • while (kltsize)
  • k ? k 1
  • total ? total k

(kltsize)
F
T
k ? k 1 total ? total k
7
Example Adding Sequences
  • iCnt 0
  • iTot 0
  • Do While iCntltiSize
  • iCnt iCnt 1
  • iTot iTot iCnt
  • Loop

iCnt lt iSize
F
T
iCnt iCnt 1 iTot iTot iCnt
8
For Loop
  • iCnt 0
  • Do While iCnt lt iSize
  • Do something
  • iCnt iCnt 1Loop
  • Wait!
  • This isnt a For loop?

iCnt lt iSize
F
T
Do something
9
For Loop
  • For iCnt 0 to 4 Do somethingNext
  • Ahhhhhh!
  • Thats better!

iCnt lt iSize
F
T
Do something
10
Exercise
  • The function factorial (n!) is defined as the
    product of the integers 1 through n.
  • Create two functions to compute n!, the first
    version using a for loop and the second using a
    while loop.

11
Repeat loop
  • A repeat loop is a control structure where the
    body is repeated until the condition is true

Body
Condition
F
T
12
Repeat loop
  • When the condition is true, the body is bypassed,
    and flow continues with the next part of the
    algorithm

Body
Condition
F
T
13
Example Adding Sequences
  • iCnt 0
  • iTot 0
  • Do
  • iCnt iCnt 1
  • iTot iTot iCnt
  • Until iCnt iSize

Body
Condition
F
T
14
Recursion
  • When a subroutine or function calls itself
  • Private Function sumIt (N As Integer) As Integer
  • Dim iTotal as Integer
  • If N gt 0 Then
  • sumIt sumIt(N 1) N
  • Else
  • sumIt N
  • End Function

15
Recursion
Example
sumIt(4)
16
Recursion
  • But theres only so far down we can go
  • Question How many recursive calls would it
    take to evaluate sumIt(10000)?

17
Recursion
  • Each time you make a recursive call, the state of
    the calling routine is saved on the program stack
    (part of memory)
  • But the stack is only so big
  • You can run out of space!

18
Recursion
  • A better solution is to re-write the routine
    using a loop instead of recursion
  • Any ideas?
Write a Comment
User Comments (0)
About PowerShow.com