Hot-Link Menu in Right-Side Column

Last Updated 6-30-2010


 
MoreNotebooks.php
  • More Computers
  •  
    MoreplayStation3.php
  • More Games
  •  
    MoreofficeProducts.php
  • More Electronics
  •  
    MoreGeneralComputerBooks.php
  • More Books
  •  
    MoreBusinessSoftware.php
  • More Software
  • 7.7 Do-Until...Loop

    Do-Until is another pretest loop. the logic of Do-Until is the opposite of the Do-While loop. The condition is for termination of the loop. In other words, the loop stops when the condition evaluates to True. To better understand the logic of Do-Until, let us consider drinking a glass of iced water on a hot summer day. You might say "I will drink the water, until the glass is empty." You might consider a Do-Until loop, whenthe number of iterations is not known and the logic of the problem suggests that the loop should continue until the condition becomes True. There is usually a loop variable used to form the condition. Like a Do-While loop, the initialization and updating of the loop variable should be done explicitly. Below is the general syntax fo a Do-Until loop:

    General Syntax (keywords are in bold):

    Do Until condition
        Body of the loop
    Loop

    Key Points to Remember

    • Do-Until is a pretest loop.
    • The body of the loop is executed until the condition evaluates to True.
    • Usually there is a loop variable that is used in the condition.
    • The loop variable should be explicitly updated within the loop.


    Buy from Centurion


    Visual Basic VB.NET - EXAMPLE 4

    Assignment: Write a Do-Until loop to add up all the numbers from 0 to 100, i.e. 0+1+2+3...+100.

    Although this is a good problem to code using a For loop, you can use a Do-Until loop as well. To write a computer program for this problem, you need two variables: one variable to add up the numbers and a loop variable to assume the values 0,1,2...100. The variables should be initialized to 0 before the loop. The condition of the loop should be for stopping the loop. Hence, the condition should be until the loop variable is greater than 100. In the body of the loop, the loop variable should be added to the sum of the numbers, and then incremented by 1 to assume the next value. Below is the pseudocode for solving this problem:

    Pseudocode for Example 7-4

    1. Declare two variables: a loop variable and a variable to sum the number.
    2. Initialize both variables to 0.
    3. Start a Do-Until loop, with condition: LoopVariable>100.
      1. Add the loop variable to the sum of the numbers.
      2. Increment the loop variable by 1.
      3. Loop back to Step 3, as long as the condition evaluates to False.
    4. After the loop completes, display the sum of the numbers in the Label.

    Create a Label for output changing the characteristics as indicated in the diagram below. Change the Form Text to Do-Until Sum 0 to 100 and double-click on the form to enter the text in the diagram below:

    Solution to 7.4

    Form at Design:

    Form 7_4

    Form at Runtime:

    7_4 Running

    lblShow Properties

    7_4 Properties
    Public Class Form1

    Private Sub Form1_Load( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim Sum As Integer
        Dim Num As Integer

        Do Until Num > 100
          Sum += Num
          Num += 1
        Loop

        lblShow .Text = "Sum Total: " & Sum .ToString

    End Sub
    End Class


    Help Support this site - Click this ad

    A Loop That Does Not Execute

    A pretest loop will not execute at all if the loop condition does not hold at the very beginning of the loop. In a For loop with a positive step value, the loop will not execute if the initial value is greater than the end value, and vice versa.

    Posttest Loops

    In a posttest loop, the condition of the loop is checked at the end of each cycle. The body of a posttest loop gets executed at least once, regardless of the condition. Sometimes the logic of the problem's solution fits well into a posttest loop. However, every solution that requires a loop can be coded using one of the pretest loops covered in Sections 7.5 to 7.7.

    7.8 Do...Loop-While

    The statements surrounded with Do and Loop get executed at least once. The condition is examined at the end of each cycle. The loop continues while the condition evaluates to True.

    General Syntax (keywords are in bold):

    Do
        Body of the loop
    Loop While condition

    Key Points to Remember

    • This is a posttest loop.
    • The body of the loop will be executed at least once, regardless of the condition.
    • The loop goes through one more cycle if the condition evaluates to True.
    • The loop variable should be explicitly updated within the body of the loop.


    Help Support this site - Click this ad

    Visual Basic VB.NET - EXAMPLE 5

    Assignment: Write a Do..Loop-While to add up all the numbers from 1 to 100, i.e. 1+2+3...+100.

    As discussed in previous sections, a computer solution for this problem requires two variables: one variable to add up the numbers and another to be used as a loop variable, which will assume the values 1,2,3....100. Below is the pseudocode for this problem using a Do...Loop-While.

    Pseudocode for Example 7-5

    1. Declare two variables: A loop variable and a variable to the sum the numbers.
    2. Initialize the loop variable to 1.
    3. Initialize the sum variable to 0.
    4. Start the Do...Loop-While with the Do keyword. In each cycle:
      1. Add the loop variable to the sum of the numbers.
      2. Increment the loop variable/Counter by 1.
      3. Finish the loop with the Loop-While keyword and test for Counter<=100.
    5. After the loop completes, display the sum of the number in the Label.

    Solution to 7.5

    Create a Label for output changing the characteristics as indicated in the diagram below. Change the Form Text to: Sum 1 to 100 with Do...Loop-While and double click on the form to enter the text in the diagram below:

    Form at Design:

    Form 7_5

    Form at Runtime:

    7_5 Running

    lblShow Properties

    7_5 Properties
    Public Class Form1

    Private Sub Form1_Load( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim Count As Integer
        Dim Sum As Integer

        Count=1
        Do
            Sum+=Count
          Count +=1
        Loop While Count <=100

        lblOutput .Text = "Sum of Numbers: " & Sum .ToString

    End Sub
    End Class




    Help Support this site - Click this ad

    7.9 Do...Loop-Until

    The statements surrounded with the Do and Loop get executed at least once. The loop stops when the condition at the end of a cycle evaluates to True.

    General Syntax (keywords are in bold):

    Do
        Body of the loop
    Loop Until condition

    Key Points to Remember

    • This is a posttest loop.
    • Body of the loop will be executed at least once, regardless of the condition.
    • The loop continues until the condition evaluates to True.
    • The loop variable should be explicitly updated within the body of the loop.


    Help Support this site - Click this ad

    Visual Basic VB.NET - EXAMPLE 6

    Assignment: Write a Do...Loop-Until to add up all the numbers from 1 to 100, i.e. 1+2+3...+100.

    Pseudocode for Example 7-6

    1. Declare two variables: a Counter variable and a sum to add the numbers.
    2. Initialize Count to 1.
    3. Initialize Sum to 0.
    4. Start a Do...Loop-Until loop, with conditions:
      1. Add the Counter variable to the sum.
      2. Increment the Counter by 1.
      3. put test condition after loop keyword checking for Counter>100.
    5. After the loop completes, display the sum of the numbers in the Label.

    Solution to 7-6

    Create a Label output for changing the characteristics as indicated in the diagram below. Change the Form Text to Sum 1 to 100. Double-click on the form to enter the text as in the diagram below:

    Form at Design:

    Form 7_6 Design

    Form at Runtime:

    7_6 Running

    lblShow Properties

    7_6 Properties
    Public Class Form1

    Private Sub Form1_Load( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim Count As Integer =1
        Dim Sum As Integer
        Do
          Sum+=Count
          Count+=1
        Loop Until Count > 100

        lblOutput .Text = "Sum of Numbers = " & Sum .ToString

    End Sub
    End Class


    Help Support this site - Click this ad

    Home


    Visual Basic

    Introduction to VB.NET

    .NET Framework

    VS2008 IDE

    How VB is Compiled

    Start Visual Studio


    Windows Form App


    Save Your Work

    VB OOP Programming

    Visual Basic Code


    Exit Code


    Button Event Code

    Coding Recommendations

    If/Then/Else

    Error List Window

    Comment Syntax

    Help Window

    Language Essentianl

    Built-In Data Types


    Declare Variables

    Declare Constants

    Code Arithmetic Expressions

    Assignment Statements

    Operator Precedence

    Type Casting


    Math Class

    String Declaration

    Conversion Functions

    Conversion Methods

    Formatting Functions

    String Formatting


    Variable Scope

    Enumerations

    Nullable Types


    Loop Constructs

    For Next Loop

    Do While Loop


    Do Until Loop

    Do...Loop-While

    Do...Loop-Until


    Exit Do | Exit For

    Do...Loop


    Nested Loops


    Arrays

    Array Declaration


    Rnd( ) Function

    Listbox Control


    KeyPressEventArgs


    Parallel Arrays

    Key Event Args


    Dynamic Arrays

    Redimension Array

    Set Breakpoint

    Start Debugger


    ReDim Preserve


    MultiDimensional Arrays

    DataGridView Control


    Length and Sort Methods


    Structures

    Pad Right

    Split Method

    IsNumeric Function


    Multiform Projects


    Add Form To Project

    Form Object Methods

    Form Show Method

    ShowDialog Method

    Form Close Method

    Form Accept Button

    Multiform Project Example


    ASP.NET Web Programming

    Create Data Source


    Configure Access Data Source


    Add Product Class


    Extract Local Database Data

    Order PageLoad VB Code

    Add New Web Page


    Set Start Page

    Display Cart Aspx Code

    Display Cart Design View

    Sorted List Definition

    VB.NET Session State


    Create CartItem Class

    GetCartContents Function


    Add To Cart Event Handler

    Remove Cart Item Event


    Clear Cart Event Handler