Hot-Link Menu in Right-Side Column

Last Updated 6-30-2010


 
MoreNetbooks.php
  • More Computers
  •  
    MoreSonyPSP.php
  • More Games
  •  
    Morevideo.php
  • More Electronics
  •  
    MoreGraphicsDesignBooks.php
  • More Books
  •  
    MoreGraphics.php
  • More Software
  • Visual Basic VB.Net Examples 7.7 to 7.10

    Visual Basic VB.NET - Example 7

    Assignment: Write a loop to find the smallest whole number whose square is greater than 1000.

    This problem is a good candidate for a Do-Until Loop. The logic of the solution is as follows: You have to examine the square of every number starting with 1,2,3,... until the desired number is found. Inside the loop, raise the number to the power of 2, compare the square to 1000, stop the loop if the square is greater than 1000, otherwise continue the loop checking the next number. To write the code for this problem, you need two variables: A loop variable to assume the numbers 1,2,3,... and another variable to store the square of the loop variable.

    Pseudocode for Example 7-7

    1. Declare 2 variables: A counter and SquareOfCounter.
    2. Start the loop with the Do keyword.
      1. Increment the Counter by 1.
      2. Set SquareOfCounter = Counter * Counter.
      3. Set the Loop-Until test checking for SquareOfCounter>1000.
    3. After the loop completes, display the Counter in a Label Output.


    Buy from Centurion


    Solution to 7.7

    Create a label for output changing the characteristics as indicated in the diagram below. Change the Form Text to Smallest Square > 1000 and double-click on the form to enter the text in the diagram below:

    Form at Design:

    7_7 Design

    Form at Runtime:

    7_7 Running

    lblOutput Properties

    7_7 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 SquareOfCount As Integer

        Do
          Count += 1
          SquareOfCount = Count * Count
        Loop Until SquareOfCount > 1000

        lblOutput .Text ="Smallest Number with Square > 1000: " & Count .ToString

    End Sub
    End Class


    Help Support this site - Click this ad

    7.10 Exit Do | Exit For

    There are exit doors for theaters, stores, and restaurants. There are also emergency exits in buildings that are doors used for exit under special circumstances. Exit Do and Exit For statements are like emergency doors: the are used when the logic of the problem requires an exit from the loop before its normal termination.

    • Exit For: Can be used in a For loop. It takes the execution out of the For loop.
    • Exit Do: Can be used in any of the Do loops. It takes the execution out of the loop.

    Visual Basic VB.NET - Example 8

    Assignment: Find the smallest number between 100 and 200, that is divisible by 19.

    To solve this problem, you have to examine each number starting with 100 and going up to 200. If the number is divisible by 19, then stop the search, otherwise look at the next number. Obviously, the logic requires a loop and a loop variable that will assume the values 100, 101,... The pseudocode of the solution is given below:

    Pseudocode for Example 7-8

    1. Declare a loop variable.
    2. Start a For loop with the loop assuming the values from 100 to 200.
      1. If the loop is divisible by 19 (use Mod), stop the loop.
      2. Examine the next number.
    3. Once the loop has been exited display the number.


    Help Support this site - Click this ad

    Solution to 7.8

    Create a Label for output by changing the characteristics as indicated in the Label Properties diagram to the right in the figure below, or just do what the arrows point at.

    Form at Design:

    Form 7_8 Design

    Form at Runtime:

    Form 7_8 Running

    lblOutput Properties

    7_8 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

        For Count = 100 to 200
          If Count mod 19 = 0 then
            Exit For
          End If
        Next
        lblOutput .Text = "First Number>100 divisibile by 19 = " & Count .ToString

    End Sub
    End Class


    Help Support this site - Click this ad

    Infinite Loop

    An infinite loop is a loop that goes on forever. For this to happen, the condition of the loop should remain the same. In any of the Do loops, if you forget to update the loop variable in the body of the loop, the condition remains the same, and the loop will iterate forever. Infinite loops have some applications, but generally they should be avoided.

    Do...Loop

    This is an unconditional loop. The loop continues forever, since there is no condition to control the continuation of the loop. Usually, an Exit Do statement is used to stop the loop based on some condition.

    General Syntax (keywords are in bold):

    Do
        Body of the loop
    Loop

    Visual Basic VB.NET - Example 9

    Assignment: Write a loop to find the smallest number greater than 1000 that is divisible by 17.

    To solve this problem, you have to check each number greater than 1000, one at a time, and see if it is divisible by 17. The loop should stop once such a number is found.

    Pseudocode for Example 7-9

    1. Declare a loop variable.
    2. Initialize the loop variable to 1001.
    3. Start an unconditional Do Loop
      1. If the loop variable is divisible by 17, Exit the loop.
      2. Increment the loop variable by 1.
      3. Continue the loop.
    4. Once outside the loop, display the loop variable.


    Help Support this site - Click this ad

    Solution to 7.9

    Create a Label for output changing the characteristics as indicated in the diagram below. Change the Form Text to Smallest Number>1000/17. Double-click on the form to enter the text in the diagram below:

    Form at Design:

    7_9 Design

    Form at Runtime:

    7_9Running

    lblOuput Properties

    7_9 Properties
    Help Support this site - Click this ad

    Public Class Form1

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

        Dim Num As Integer =1001

        Do
          If Num mod 17 =0 then
            Exit Do
          End If
          Num+=1
        Loop

        lblOutput .Text = "First Number >1000 Divisible by 17 is: " & Num .ToString

    End Sub
    End Class


    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