Hot-Link Menu in Right-Side Column
Last Updated 6-30-2010
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
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:
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.
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
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.
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.
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):
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
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:
Visual BasicIntroduction to VB.NET
How VB is Compiled
Start Visual Studio
Windows Form App
Save Your WorkVB OOP Programming
Visual Basic Code
Button Event CodeCoding Recommendations
Error List Window
Built-In Data Types
Declare VariablesDeclare Constants
Code Arithmetic Expressions
Math ClassString Declaration
Loop ConstructsFor Next Loop
Do While Loop
Do Until LoopDo...Loop-While
Exit Do | Exit ForDo...Loop
Rnd( ) FunctionListbox Control
Parallel ArraysKey Event Args
Dynamic ArraysRedimension Array
MultiDimensional ArraysDataGridView Control
Length and Sort Methods
Add Form To Project
Form Object Methods
Form Show Method
Form Close Method
Form Accept Button
Multiform Project Example
ASP.NET Web ProgrammingCreate Data Source
Configure Access Data Source
Add Product Class
Extract Local Database DataOrder PageLoad VB Code
Add New Web Page
Set Start PageDisplay Cart Aspx Code
Display Cart Design View
Sorted List Definition
VB.NET Session State
Create CartItem ClassGetCartContents Function
Add To Cart Event HandlerRemove Cart Item Event
Clear Cart Event Handler