Chapter 7 loops
This presentation is the property of its rightful owner.
Sponsored Links
1 / 19

Chapter 7 Loops PowerPoint PPT Presentation


  • 101 Views
  • Uploaded on
  • Presentation posted in: General

Chapter 7 Loops. Programming In Visual Basic.NET Prepared by Johnny Tsui, [email protected] Loops. Repeating a series of instructions Types of Loops Do Use when the number of iterations is unknown For Next Use when the number of iterations known. Do Loops.

Download Presentation

Chapter 7 Loops

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Chapter 7 loops

Chapter 7Loops

Programming In

Visual Basic.NET

Prepared by Johnny Tsui, [email protected]


Loops

Loops

  • Repeating a series of instructions

  • Types of Loops

    • Do

      • Use when the number of iterations is unknown

    • For Next

      • Use when the number of iterations known


Do loops

Do Loops

  • Ends based on a condition you specify, either

    • Loop While a condition is True

    • Loop Until a condition becomes True

  • Condition can be located at

    • Top of Loop, Pretest

    • Bottom of Loop, Posttest


Do loop general form

Do Loop General Form

Do {While |Until} condition

Statements to execute

Loop

OR

Do

Statements to execute

Loop {While | Until} condition

Top of Loop Condition,

Pretest

Bottom of Loop Condition,

Posttest


Do s when evaluated

Do's - When Evaluated

  • Top Evaluation, not guaranteed to run once since evaluated BEFORE running

    Do While … Loop

    Do Until … Loop


Chapter 7 loops

  • Example:

    intA = 0

    Do Until intA = 10

    intA = intA + 1

    Loop


Chapter 7 loops

  • Example:

    intA = 0

    Do While intA < 10

    intA = intA + 1

    Loop


Do s when evaluated1

Do's - When Evaluated

  • Bottom Evaluation, will always run at least one time

    Do … Loop While

    Do … Loop Until


Chapter 7 loops

  • Example:

    intA = 0

    Do

    intA = intA + 1

    Loop Until intA = 10


Chapter 7 loops

  • Example:

    intA = 0

    Do

    intA = intA + 1

    Loop While intA < 10


Do loops for next loops deciding which to use

Is the

number of

repetitions

known?

Must

the loop

execute

once?

NO

NO

Top Eval Do Loop

YES

YES

For Next Loop

Bottom Eval Do Loop

Is the

expression

initially

true?

Do Until Loop

Do While Loop

NO

YES

Do Loops / For Next Loops:Deciding Which To Use


For next loops

For Next Loops

  • Use when you know the number of iterations

  • Uses a numeric counter variable, called Loop Index, to control number of iterations

  • Loop Index is incremented at the bottom of the loop on each iteration

  • Step value can be included to specify the incrementing amount to increment Loop Index, step can be a negative number


For next loop general form

For Next Loop General Form

For LoopIndex = InitialValue To TestValue [Step Increment]

Statements

Next [LoopIndex]


Chapter 7 loops

  • Example:

    intA = 0

    intB = 0

    For intA = 1 To 10

    intB = intB + 1

    Next


Chapter 7 loops

  • Example:

    intA = 0

    intB = 0

    For intA = 1 To 10 Step 2

    intB = intB + 1

    Next


Manually exiting for next loops

Manually Exiting For Next Loops

  • In some situations you may need to exit the loop prematurely

  • Use theExit For statement inside the loop structure


Chapter 7 loops

  • Example:

    intA = 0

    intB = 0

    For intA = 1 To 10

    intB = intB + 1

    If intB = 3

    Exit For

    End If

    Next


Exercises

Exercises

Name: Class:

  • Idenifty the statements that are correctly formed and those that have errors. For those with errors, state what is wrong and how to correct it.

    (a)For decIndex = 3.5 to 6.0, Step 0.5

    Next

    (b)For 4 = 1 to 10 Step 2

    Next

    (c)For intIndex = 100 to 0 Step -25

    Next

    (d)For intIndex = 0 to -10 Step -1

    Next

    (e)For intIndex = 10 to 1

    Next


Exercises1

Exercises

Name: Class:

  • How many times will the body of the loop be executed for each of these examples?

    (a)For intIndex = 1 to 3

    (b)For intIndex = 2 to 11 Step 3

    (c)For intIndex = 10 to -1 Step -1

    (d)For decIndex = 3.0 to 6.0 Step 0.5

    (e)For intIndex = 5 to 1


  • Login