Department of Computer CS 1408 and Intro to...

13
Unit 2: Visual Basic .NET, pages 1 of 13 Department of Computer and Mathematical Sciences CS 1408 Intro to Computer Science with Visual Basic .NET 8 Lab 8: Loops Structures Objectives: The main objective of this lab is to understand the concepts of and how to use Repetition Structures -- loop, loop, and Loop in VB .NET. Do While Do Until For-Next Task 1: Do loop While The purpose of this task is to learn how to implement Do loop statement. While A loop is a repetition control structure. It causes a single statement or block to be executed repeatedly. A repeated procedural section of code is commonly called a loop, because after the last statement in the code is executed, the program branches, or loops back to the first statement and starts another repetition. Each repetition is also referred to as iteration or a pass through the loop. Constructing repetitive sections of code requires the following four elements: 1. A repetition statement that both defines the boundaries containing the repeating section of code and controls whether the code will be executed or not. 2. A condition that needs to be evaluated. 3. A statement that initially sets the condition. 4. A statement within the repeating section of code that modifies the condition in such a way that the repetitions, at some point, stop. There are three different forms of Visual Basic repetition structures: Do structures While Do structures Until structures For-Next The syntax of the Do loop is as follows: While Do Boolean expression While statement(s) Loop A Do loop consists of While A Boolean expression: the loop condition that will control the repetitions of the body of the loop A body of the loop is the set of statements that will be repeated The body of the loop will be repeated if the Boolean expression is true. Otherwise, the loop will stop.

Transcript of Department of Computer CS 1408 and Intro to...

Page 1: Department of Computer CS 1408 and Intro to …cms.dt.uh.edu/Faculty/Ongards/cs1408/Labs/Lab8_loops.pdfUnit 2: Visual Basic .NET, pages 1 of 13 Department of Computer and Mathematical

Unit 2: Visual Basic .NET, pages 1 of 13

Department of Computer and

Mathematical Sciences

CS 1408 Intro to Computer

Science with Visual Basic .NET

8

Lab 8: Loops Structures

Objectives: The main objective of this lab is to understand the concepts of and how to use Repetition Structures -- loop, loop, and Loop in VB .NET. Do While Do Until For-Next Task 1: Do loop WhileThe purpose of this task is to learn how to implement Do loop statement. While

A loop is a repetition control structure. It causes a single statement or block to be executed repeatedly. A repeated procedural section of code is commonly called a loop, because after the last statement in the code is executed, the program branches, or loops back to the first statement and starts another repetition. Each repetition is also referred to as iteration or a pass through the loop. Constructing repetitive sections of code requires the following four elements:

1. A repetition statement that both defines the boundaries containing the repeating section of code and controls whether the code will be executed or not.

2. A condition that needs to be evaluated. 3. A statement that initially sets the condition. 4. A statement within the repeating section of code that modifies the condition in such a

way that the repetitions, at some point, stop.

There are three different forms of Visual Basic repetition structures:

– Do structures While– Do structures Until– structures For-Next

The syntax of the Do loop is as follows: While

Do Boolean expression Whilestatement(s)

Loop

A Do loop consists of While– A Boolean expression: the loop condition that will control the repetitions of the body of

the loop – A body of the loop is the set of statements that will be repeated – The body of the loop will be repeated if the Boolean expression is true. Otherwise, the

loop will stop.

Page 2: Department of Computer CS 1408 and Intro to …cms.dt.uh.edu/Faculty/Ongards/cs1408/Labs/Lab8_loops.pdfUnit 2: Visual Basic .NET, pages 1 of 13 Department of Computer and Mathematical

Unit 2: Visual Basic .NET, pages 2 of 13

The Do is an entrance-controlled loop since it checks the loop condition before it enters Whilethe loop. In creating loops, it is the programmer’s responsibility to ensure an exit from the loop. If this is not done, then an infinite loop, a loop that never ends, will occur.

Statement of the Problem: Create a VB.NET application called Average of Numbers that will accept a list of numbers through a textbox and displayed in a list box. Then the application will compute the sum and average of the numbers in the list box when Compute button is clicked. Clear button is used to clear the list of numbers in the list box and the text in the labels. The design/GUI of application should look similar to the following figure:

Activity 1.1: Create a new project for VB application called Lab11Tsk1 by first create a GUI with the following controls and property settings.

Control Name Text ForeColor Font Size Others From1 Default Average of Numbers Default Default Size: 490, 350 Label1 Default List of Numbers Blue 12 Size: 155, 30 Label2 Default Enter a Number: Blue 12 Size: 165, 30 Label3 Default Sum: Blue 12 Size: 70, 30 Label4 Default Average: Blue 12 Size: 110, 30

Label5 SumLabel None Red 12 Size: 150, 30, BackColor: Yellow

Label6 AverageLabel None Red 12 Size: 150, 30, BackColor: Yellow

ListBox DisplayListBox None Blue 12 Size: 190, 204, BackColor: Yellow

TextBox InputTextBox None Default 12 Size: 150, 30 Button1 EnterButton Enter Blue 12 Size: 80, 30 Button2 ComputeButton Compute Blue 12 Size: 100, 30 Button3 ClearButton Clear Blue 12 Size: 80, 30 Your GUI of Lab11Tsk1 VB application should look similar to Figure 1.

Figure 1: Lab11Tsk1 Application at Design Time

Now we are ready to add code to the Lab11Tsk1 VB application according to statement of the problem. From analyzing the problem, the Lab11Tsk1 VB application has three events and each event has the following actions to be performed:

1. Enter button when clicked will take the number in InputTextBox and append it to the list of in DisplayListBox, then set focus and highlight the text in InputTextBox.

2. Compute button when clicked will sum all the numbers in DisplayListBox and computes the average. Then it will display the sum and the average in SumLabel and AverageLabel.

Page 3: Department of Computer CS 1408 and Intro to …cms.dt.uh.edu/Faculty/Ongards/cs1408/Labs/Lab8_loops.pdfUnit 2: Visual Basic .NET, pages 1 of 13 Department of Computer and Mathematical

Lab 8: Loops, page 3 of 13

3. Clear button when clicked will clear DisplayListBox, SumLabel, and AverageLabel.

We can organize a chart describes actions to be performed by these three events as follows:

Activity 1.2: The Enter Button Click event will perform the following actions: 1) Append the number in InputTextBox to DisplayListBox. The code to perform the above action is as follows:

REM Display the number from the InputTextBox REM in the DisplayListBox DisplayListBox.Items.Add(InputTextBox.Text)

2) Set focus and highlight the text in InputTextBox. The code to perform the above action is as follows:

REM Set focus and highlight the text in the InputTextBox InputTextBox.Focus() InputTextBox.SelectAll()

Test your application by checking on the control that related to the event procedure. Activity 1.3: The Compute Button Click Event will perform the following actions: 1) Find the number of numbers in DisplayListBox. The code to perform the above action is as follows:

Dim Sum, Average As Double Dim MaxCount, Count As Byte

REM Assign the MaxCount to the number of the numbers in the DisplayListBox REM MaxCount = DisplayListBox.Items.Count 2) Find the sum of the numbers in DisplayListBox. The code to perform the above action is as follows:

REM Initialize the variable Sum and Count variable. Sum = 0 Count = 0

Lab11Tsk1 Application

Enter Button Click Event Append the number in InputTextBox to DisplayListBox. Set focus and highlight the text in InputTextBox.

Compute Button Click Event Find the number of numbers in DisplayListBox. Find the sum of the numbers in DisplayListBox. Find the average of the numbers in DisplayListBox. Display the value of sum in SumLabel and average in Averagelabel.

Clear Button Click Event Clear DisplayListBox. Clear SumLabel. Clear AverageLabel.

This statement appends the value from the InputTextBox.Text to DisplayListBox.

These statements set focus and highlight the text in it InputTextBox.

This statement assigns the number of items in the DisplayListBox to MaxCount.

These statements initialize Sum and Count to 0 before the loop.

Page 4: Department of Computer CS 1408 and Intro to …cms.dt.uh.edu/Faculty/Ongards/cs1408/Labs/Lab8_loops.pdfUnit 2: Visual Basic .NET, pages 1 of 13 Department of Computer and Mathematical

Unit 2: Visual Basic .NET, pages 4 of 13

REM The Do While loop will sum all the numbers in DisplayListBox Do While (Count < MaxCount) Sum = Sum + Val(DisplayListBox.Items(Count)) Count = Count + 1 Loop 3) Find the average of the numbers in DisplayListBox. The code to perform the above action is as follows:

REM Find the Average Average = Sum / MaxCount 4) Display the value of sum in SumLabel and average in AverageLabel. The code to perform the above action is as follows:

REM Display the Sum and the Average SumLabel.Text = Sum AverageLabel.Text = FormatNumber(Sum / MaxCount)

Test your application by checking on the control that related to the event procedure. Activity 1.4: The Clear Button Click Event will perform the following actions: 1) Clear DisplayListBox. 2) Clear SumLabel. 3) Clear Averagelabel. The code to perform the above actions is as follows:

REM Clear the DisplayListBox, SumLabel and Average Label DisplayListBox.Items.Clear() SumLabel.Text = "" AverageLabel.Text = ""

Test your application for correctness.

Task 2: loop Do-Until The purpose of this task is to learn how to implement loop statement. Do-Until

The syntax of a loop is as follows: Do-Until

Do statement(s)

Boolean expression Loop Until The loop executes the statement(s) within the loop as long as the condition is False. Do-UntilThe loop is an Exit-controlled loop. Do-Until Statement of the Problem: Create a VB.NET application called Max-Min that will accept a list of numbers through a textbox and displayed in a list box. Then the application will determine the maximum and minimum number of the list numbers in the list box

This is an algorithm to find the sum of a set of numbers using a loop control structure.

Page 5: Department of Computer CS 1408 and Intro to …cms.dt.uh.edu/Faculty/Ongards/cs1408/Labs/Lab8_loops.pdfUnit 2: Visual Basic .NET, pages 1 of 13 Department of Computer and Mathematical

Lab 8: Loops, page 5 of 13

when Find button is clicked. Clear button is used to clear the list of numbers in the list box and the text in the labels. The design/GUI of application should look similar to the following figure: Activity 2.1: Create a new project for VB application called Lab11Tsk2 by first create a GUI with the following controls and property settings.

Control Name Text ForeColor Font Size Others From1 Default Max-Min Default Default Size: 490, 350 Label1 Default List of Numbers Blue 12 Size: 155, 30 Label2 Default Enter a Number: Blue 12 Size: 165, 30 Label3 Default Maximum: Blue 12 Size: 110, 30 Label4 Default Maximum: Blue 12 Size: 110, 30

Label5 MaxiLabel None Red 12 Size: 150, 30, BackColor: Yellow

Label6 MiniLabel None Red 12 Size: 150, 30, BackColor: Yellow

ListBox DisplayListBox None Blue 12 Size: 190, 204, BackColor: Yellow

TextBox InputTextBox None Default 12 Size: 150, 30 Button1 EnterButton Enter Blue 12 Size: 80, 30 Button2 FindButton Find Blue 12 Size: 100, 30 Button3 ClearButton Clear Blue 12 Size: 80, 30 Your GUI of Lab11Tsk2 VB application should look similar to Figure 2.

Figure 2: Lab11Tsk2 Application at Design Time

Now we are ready to add code to the Lab11Tsk2 VB application according to statement of the problem. From analyzing the problem, the Lab11Tsk2 VB application has three events and each event has the following actions to be performed:

1. Enter button when clicked will take the number in InputTextBox and append it to the list of in DisplayListBox, then set focus and highlight the text in InputTextBox.

2. Find button when clicked will determine the maximum and minimum numbers from the numbers in the DisplayListBox and display the maximum and minimum numbers in the MaxLabel and MinLabel.

3. Clear button when clicked will clear DisplayListBox, SumLabel, and AverageLabel.

We can organize a chart describes actions to be performed by these three events as follows:

Page 6: Department of Computer CS 1408 and Intro to …cms.dt.uh.edu/Faculty/Ongards/cs1408/Labs/Lab8_loops.pdfUnit 2: Visual Basic .NET, pages 1 of 13 Department of Computer and Mathematical

Unit 2: Visual Basic .NET, pages 6 of 13

Activity 2.2: The Enter Button Click event will perform the following actions: 1) Append the number in InputTextBox to DisplayListBox. The code to perform the above action is as follows:

REM Display the number from the InputTextBox REM in the DisplayListBox DisplayListBox.Items.Add(InputTextBox.Text)

2) Set focus and highlight the text in InputTextBox. The code to perform the above action is as follows:

REM Set focus and highlight the text in the InputTextBox InputTextBox.Focus() InputTextBox.SelectAll()

Test your application by checking on the control that related to the event procedure. Activity 2.3: The Compute Button Click Event will perform the following actions: 1) Find the minimum and maximum number of numbers in DisplayListBox. The code to perform the above action is as follows: Dim Maximum, Minimum As Double Dim MaxCount, Count As Byte

REM Assign the MaxCount to the number of the numbers in the DisplayListBox MaxCount = DisplayListBox.Items.Count

REM Initialize the variables Maximum and Minimum to the first number in the DisplayListBox Maximum = Val(DisplayListBox.Items(0)) Minimum = Val(DisplayListBox.Items(0))

REM Initialize the variable Count to be second Count = 1 REM The Do Until loop will repeat until Count > MaxCount - 1 Do REM Reset the Maximum number to the larger number If (Maximum < DisplayListBox.Items(Count)) Then Maximum = DisplayListBox.Items(Count) End If REM Reset the Miniimum number to the smaller number If (Minimum > DisplayListBox.Items(Count)) Then Minimum = DisplayListBox.Items(Count) End If

Lab11Tsk2 Application

Enter Button Click Event Append the number in InputTextBox to DisplayListBox. Set focus and highlight the text in InputTextBox.

Find Button Click Event Find the minimum and maximum number of numbers in DisplayListBox. Display the minimum value in MinLabel and maximum value in MaxLabel.

Clear Button Click Event Clear DisplayListBox. Clear MinLabel. Clear MaxLabel.

These statements initialize Maximum and Minimum to be the first number in the list.

This If_Then statement will

replace the current Maximum value with the next larger value.

This If_Then statement will

replace the current Minimum value with the next smaller value.

These statements initialize Sum and Count to 0 before the loop.

This statement appends the value from the InputTextBox.Text to DisplayListBox.

These statements set focus and highlight the text in it InputTextBox.

Page 7: Department of Computer CS 1408 and Intro to …cms.dt.uh.edu/Faculty/Ongards/cs1408/Labs/Lab8_loops.pdfUnit 2: Visual Basic .NET, pages 1 of 13 Department of Computer and Mathematical

Lab 8: Loops, page 7 of 13

REM Increment the Count Count = Count + 1 Loop Until (Count > MaxCount - 1) 'check the loop condition

2) Display the minimum value in MinLabel and maximum value in MaxLabel. The code to perform the above actions is as follows: REM Display the Maximum and Minimum numbers in the MaxLabel and MinLabel MaxLabel.Text = Maximum MinLabel.Text = Minimum

Test your application by checking on the control that related to the event procedure. Activity 2.4: The Clear Button Click Event will perform the following actions: 1) Clear DisplayListBox. 2) Clear SumLabel. 3) Clear Averagelabel. The code to perform the above actions is as follows:

REM Clear the DisplayListBox, SumLabel and Average Label DisplayListBox.Items.Clear() SumLabel.Text = "" AverageLabel.Text = ""

Test your application for correctness. Print out the source code to be turned in later.

Task 3: loop For-Next

The purpose of this task is to learn how to implement loop statement. For-Next The creation of fixed-count loops always requires initializing, testing, and modifying the counter variable. The loop groups all three of these operations on a single line which make it For-Nextmore convenient and compact. The general form of the loop is as follows: For-Next Loop variable = startingValue endingValue [ increment] For To Step statement(s) [Loop variable] Next

loops must satisfy the following rules: For-Next• The first statement in a loop must be a statement For-Next For• The last statement in a loop must be a statement. For-Next Next• The loop counter variable may be either a real or integer variable. For-Next• The initial, final, and increment values may all be replaced by variables or expressions, as

long as each variable has a value previously assigned to it and the expressions can be evaluated to yield a number.

Page 8: Department of Computer CS 1408 and Intro to …cms.dt.uh.edu/Faculty/Ongards/cs1408/Labs/Lab8_loops.pdfUnit 2: Visual Basic .NET, pages 1 of 13 Department of Computer and Mathematical

Unit 2: Visual Basic .NET, pages 8 of 13

• The initial, final, and increment values may be positive or negative, but the loop will not be executed if any one of the following is true:

– The initial value is greater than the final value and the increment is positive. – The initial value is less than the final value and the increment is negative.

• An infinite loop is created if the increment is zero. • An Exit statement may be embedded within a loop to cause a transfer For For-Next

out of the loop. Statement of the Problem: Create a VB.NET application called Mortgage Table that will retrieve the loan amount, interest rate, and number of years from the textboxes, then computes monthly payment, monthly interest, monthly principle paid, loan balance and displays these values as a table in the provided list box. The design/GUI of application should look similar to the following figure: Activity 3.1: Create a new project for VB application called Lab11Tsk3 by first create a GUI with the following controls and property settings.

Control Name Text ForeColor Font Size Others From1 Default Mortgage Table Default Default Size: 715, 570 Label1 Default Mortgage Table Blue 10 Size: 300, 30 Label2 Default Enter the loan amount: Blue 10 Size: 220, 30 Label3 Default Enter the interest rate: Blue 10 Size: 220, 30 Label4 Default Enter the number of years to payoff the loan: Blue 10 Size: 350, 25

Label5 DisplayTitleLabel None Blue 10 Size: 690, 30, BorderStyle: Fixed3D, BackColor: Yellow

TextBox1 AmountTextBox 1000000 Default 10 Size: 230, 26 TextBox2 InterestTextBox 6 Default 10 Size: 230, 26 TextBox3 YearsTextBox 15 Default 10 Size: 130, 26 Button1 DisplayButton Display Table Blue 10 Size: 200, 40Button2 ClearButton Clear Blue 10 Size: 100, 40

ListBox DisplayListBox None Red 10 Size: 690, 224, BackColor: Yellow

Your GUI of Lab11Tsk3 VB application should look similar to Figure 3.

Figure 3: Lab11Tsk3 Application at Design Time

Page 9: Department of Computer CS 1408 and Intro to …cms.dt.uh.edu/Faculty/Ongards/cs1408/Labs/Lab8_loops.pdfUnit 2: Visual Basic .NET, pages 1 of 13 Department of Computer and Mathematical

Lab 8: Loops, page 9 of 13

Now we are ready to add code to the Lab11Tsk3 VB application according to statement of the problem. From analyzing the problem, the Lab11Tsk3 VB application has three events and each event has the following actions to be performed:

1. While loading Mortgage Table application/form at the start, it will display the title heading in DisplayTitleLabel as shown in the right figure.

2. Display Table button when clicked will Retrieve the loan amount, interest rate, and number of

years from AmountTextBox, InterestTextBox, and YearsTextBox, respectively.

Compute monthly payment, monthly interest, monthly principle paid, and loan balance.

Display the values of monthly payment, monthly interest, monthly principle paid, and loan balance as a table in DisplayListBox.

3. Clear button when clicked will clear DisplayListBox.

We can organize a chart describes actions to be performed by these three events as follows:

Activity 3.2: The Form1 Load event will perform the following action: 1) Display title heading in DisplayTitleLabel. The code to perform the above action is as follows: REM Display headings when the application is first loaded. DisplayTitleLabel.Text = " Payment Number Monthly Payment Principle Paid Interest Paid Loan Balance"

Where represent a space. Test your application by checking on the control that related to this event procedure. Activity 3.3: The Display Table Button Click event will perform the following actions:

1) Retrieve the loan amount, interest rate, and number of years from AmountTextBox, InterestTextBox, and YearsTextBox, respectively. The code to perform this action is as follows:

REM Retrieve LoanAmount, Interest, and Years to the input from the corresponding textboxes. LoanAmount = Val(AmountTextBox.Text)

Lab11Tsk3 Application

Form1 Load Event Display title heading in DisplayTitleLabel.

Display Table Button Click Event Retrieve the loan amount, interest rate, and number of years from

AmountTextBox, InterestTextBox, and YearsTextBox, respectively. Compute monthly payment, monthly interest, monthly principle paid, and

loan balance. Display the values of monthly payment, monthly interest, monthly principle

paid, and loan balance as a table in DisplayListBox.

Clear Button Click Event Clear DisplayListBox.

These statements retrieve the values from textboxes.

Page 10: Department of Computer CS 1408 and Intro to …cms.dt.uh.edu/Faculty/Ongards/cs1408/Labs/Lab8_loops.pdfUnit 2: Visual Basic .NET, pages 1 of 13 Department of Computer and Mathematical

Unit 2: Visual Basic .NET, pages 10 of 13

Interest = Val(InterestTextBox.Text) Years = Val(YearsTextBox.Text)

Do not forget to declare the above variables that being used in the declaration section.

2) Compute monthly payment, monthly interest, monthly principle paid, and loan balance. The code to perform this action is as follows:

REM Convert the interest rate to monthly. Interest = Interest / 1200 REM Convert years to months. NosPayment = Years * 12 REM Compute monthly payment. MonthlyPayment = LoanAmount * (Interest / (1 - (1 + Interest) ^ (-NosPayment))) REM Set loanBalance to the original loan amount. LoanBalance = LoanAmount REM Compute monthly interest paid, priciple paid, and loan balance, then display them. For Count = 0 To NosPayment - 1 Step 1 InterestPaid = LoanBalance * Interest PrinciplePaid = MonthlyPayment - InterestPaid LoanBalance = LoanBalance - PrinciplePaid Next Count

Do not forget to declare the above variables that being used in the declaration section.

3) Display the values of monthly payment, monthly interest, monthly principle paid, and loan balance as a table in DisplayListBox. The code to perform this action is as follows:

REM Display monthly payment, interest paid, priciple paid, and loan balance DisplayListBox.Items.Add(String.Format(FormatDisplay, CStr(Count + 1), FormatCurrency(MonthlyPayment), FormatCurrency(PrinciplePaid), FormatCurrency(InterestPaid), FormatCurrency(LoanBalance))) The above code must be put in side the For-Next loop so that it will display the table the Mortgage Table. String.Format function is used in conjunction with FormatDisplay variable as a parameter for the function to display outputs in tabular form. FormatDisplay must be declared in the declaration section as follows: Dim FormatDisplay As String = "{0,15}{1,25}{2,23}{3,21}{4,22}"

Test your application by checking on the control that related to this event procedure. Activity 3.4: Insert a code in the event procedure that will clear the DisplayListBox when Clear button is clicked. Test your application for correctness and print out the source code to be turned in later.

Task 4: Multiplication Table

The purpose of this task is to learn how to implement nested Do and While For-Nextstatement.

This statement converts the inputted yearly interest rate in percent to monthly interest rate.

This statement converts #years to #months.

This statements computes - monthly interest paid - monthly principle paid

- loan balance for each month

This declaration will be used to setup the display of values in a tabular form in DisplayListBox.

Page 11: Department of Computer CS 1408 and Intro to …cms.dt.uh.edu/Faculty/Ongards/cs1408/Labs/Lab8_loops.pdfUnit 2: Visual Basic .NET, pages 1 of 13 Department of Computer and Mathematical

Lab 8: Loops, page 11 of 13

A nested loop is a loop contained within another loop. The loop that contains another loop is called the outer loop and the loop that is contained within another loop is called the inner loop. For each single trip through the outer loop, the inner loop runs through its entire sequence. The followings are the syntax for nested Do and loop. While For-Next

Do Boolean expression While Do Boolean expression While

statement(s) Loop

Loop Loop variable1 = startingValue endingValue [ increment] For To Step Loop variable1 = startingValue endingValue [ increment] For To Step statement(s) [Loop variable2] Next [Loop variable1] Next Statement of the Problem: Create a VB.NET application called Multiplication Table that will construct a multiplication table starting and ending values from the user’s entered numbers in the provided textboxes. The multiplication table will be displayed in a provided list box. For example if the starting value is 3 or 1 and the value ending value is 5 or 9, respectively, then the list box will display as shown in the figures. The design/GUI of the application should look similar to the following figure:

Activity 4.1: Create a new project for VB application called Lab11Tsk4 by first create a GUI with the following controls and property settings.

Control Name Text ForeColor Font Size Others From1 Default Multiplication Table Default Default Size: 590, 340 Label1 Default Multiplication Table Blue 14 Size: 215, 30 Label2 Default From: Blue 10 Size: 65, 30 Label3 Default To: Blue 10 Size: 65, 30

ListBox DisplayListBox None Red 8 Size: 550, 180, BackColor: Yellow

TextBox1 FromTextBox 1 Default 10 Size: 80, 26 TextBox2 ToTextBox 9 Default 10 Size: 80, 26

Page 12: Department of Computer CS 1408 and Intro to …cms.dt.uh.edu/Faculty/Ongards/cs1408/Labs/Lab8_loops.pdfUnit 2: Visual Basic .NET, pages 1 of 13 Department of Computer and Mathematical

Unit 2: Visual Basic .NET, pages 12 of 13

Button TableButton Table Blue 10 Size: 80, 30

Your VB application should look similar to Figure 4.

Figure 4: Lab11Tsk4 Application at Design Time

Now we are ready to add code to the Lab11Tsk4 VB application according to statement of the problem. From analyzing the problem, the Lab11Tsk4 VB application has one events and each event has the following actions to be performed:

1. Table button when clicked will Clear DisplayListBox. Retrieve values from the FromTextBox and ToTextBox Create a multiplication table from the value of FromTextBox to the value of

ToTextBox. We can organize a chart describes actions to be performed by these one events as follows:

Activity 4.2: Table Button Click event will perform the following action: Clear DisplayListBox.

The code to perform this action is: REM Clear the listbox. DisplayListBox.Items.Clear()

Retrieve values from the FromTextBox and ToTextBox.

REM Set the MaxRow to the input from REM the ToTextbox as the ending value. MaxRow = Val(ToTextbox.Text)

REM Set Outer to the input from REM the FromTextbox as the starting value. Outer = Val(FromTextBox.Text)

Do not forget to declare the variables being used in the above code.

Dim Outer, Inner, MaxRow As Byte Dim DisplayRow, Column As String

Lab11Tsk4 Application

Table Button Click Event Clear DisplayListBox. retrieve values from the FromTextBox and ToTextBox. create a multiplication table from the value of FromTextBox to the value of

ToTextBox.

This statement retrieves the value for Outer as starting row.

This statement retrieves the value for MaxRow as ending row.

Page 13: Department of Computer CS 1408 and Intro to …cms.dt.uh.edu/Faculty/Ongards/cs1408/Labs/Lab8_loops.pdfUnit 2: Visual Basic .NET, pages 1 of 13 Department of Computer and Mathematical

Lab 8: Loops, page 13 of 13

Create a multiplication table from the value of FromTextBox to the value of ToTextBox. REM Outer loop keeps track of the rows. Do While (Outer <= MaxRow)

REM Initialize the Inner variable to 0. Inner = 1 DisplayRow = ""

REM Inner loop keeps track of the columns. Do While (Inner <= 9) Column = Outer & " x " & Inner & " = " & Outer * Inner & " " DisplayRow = DisplayRow & Column

REM Increment Inner count. Inner = Inner + 1

Loop DisplayListBox.Items.Add(DisplayRow)

REM Increment Outer count. Outer = Outer + 1 Loop

Test your application by checking on the control that related to this event procedure. Print out the source code to be turned in later.

Activity 4.3: Replace the nested Do statement in Activity 4.2 to a nested While For-Nextstatement.

Test your application by checking on the control that related to this event procedure.

This outer loop controls rows in the multiplication table.

This inner loop controls columns in the multiplication table.