Hot-Link Menu in Right-Side Column

Last Updated 6-30-2010

  • More Computers
  • More Games
  • More Electronics
  • More Books
  • More Software
  • 7.12 Nested Loops

    A loop can be coded within another loop thus forming nested loops. There can be many levels of nesting. Needless to say, there can be a selection structure coded inside a loop, a loop coded in a selection structure, and so on. Such nesting is possible as long as the inner structure is completely enclosed in the outer structure. Nested loops do have application in programming. For example, nested loops are needed to display a table of values, to process the elements of a two-dimensional array, or to sort array elements using the Bubble Sort algorithm. Let us look at an example:

    Off-By-One Error

    This error means that the loop is iterated one extra, or one fewer times than expected. To avoid an Off-By-One error, be careful when forming the loop condition. For example, the following loop condition will process all the numbers from 1 to 100, excluding 100. Assume that N as Int = 1.

    Do While N<100

    Visual Basic VB.NET - Example 10

    Assignment: Design a VB.NET project to display a multiplication table for numbers 1 to 10 on the screen. To do this, you need to display 10 rows, and in each row you have to display 10 values, which is the product of the row # by the column #. The outer loop will go through the rows, and the inner loop will go through the columns. Notice that for each cycle of the outer loop, i.e. for each row, the inner loop has to through all the cycles, i.e., all the columns. Let us design a simple GUI for this project with one Button and one Label, and write the code in the click event procedure of the Button.

    Buy from Centurion

    Solution to 7.10

    Modify the properties of the Label, Button and Form as indicated in the diagrams below. Double-Click on the Display button to open the code box, and enter the code supplied below.

    Form at Design:

    7_10 Design

    Help Support this site - Click this ad

    Form at Runtime:

    7_10 Running
    Help Support this site - Click this ad

    lblShow Properties

    7_10 Properties
    Help Support this site - Click this ad

    Public Class Form1

    Private Sub btnDisplay_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
        Dim Row As Integer
        Dim Col As Integer
        Dim Length As Integer
        Dim Product As Integer
        Dim strProduct As String

        lblShow .Text =""

        For Row = 1 to 10
          For Col=1 to 10
            strProduct = Product .ToString
            For Length=4 to strProduct .Length step -1
              strProduct = " " & strProduct
            lblShow .Text &= strProduct
          lblShow .Text &= vbCrLf

    End Sub
    End Class

    Help Support this site - Click this ad


    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


    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


    Nullable Types

    Loop Constructs

    For Next Loop

    Do While Loop

    Do Until Loop



    Exit Do | Exit For


    Nested Loops


    Array Declaration

    Rnd( ) Function

    Listbox Control


    Parallel Arrays

    Key Event Args

    Dynamic Arrays

    Redimension Array

    Set Breakpoint

    Start Debugger

    ReDim Preserve

    MultiDimensional Arrays

    DataGridView Control

    Length and Sort Methods


    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