|
Hot-Link Menu in Right-Side Column |
Last Updated 6-30-2010 |
||||||||||||||||||||||||||||||||||||||||||||||||||
|
Visual Basic VB.Net Examples 7.7 to 7.10Visual Basic VB.NET - Example 7Assignment: 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.7Create 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:
7.10 Exit Do | Exit ForThere 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 8Assignment: 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.8Create 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.
Infinite LoopAn 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...LoopThis 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 9Assignment: 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.9Create 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:
|
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 ProgrammingVisual Basic Code Exit Code Button Event Code Coding RecommendationsIf/Then/Else Error List Window Comment Syntax Help Window Language Essentianl Built-In Data Types Declare Variables Declare ConstantsCode Arithmetic Expressions Assignment Statements Operator Precedence Type Casting Math Class String DeclarationConversion Functions Conversion Methods Formatting Functions String Formatting Variable Scope EnumerationsNullable Types Loop Constructs For Next LoopDo While Loop Do Until Loop Do...Loop-WhileDo...Loop-Until Exit Do | Exit For Do...LoopNested Loops Arrays Array DeclarationRnd( ) Function Listbox ControlKeyPressEventArgs Parallel Arrays Key Event ArgsDynamic Arrays Redimension ArraySet Breakpoint Start Debugger ReDim Preserve MultiDimensional Arrays DataGridView ControlLength and Sort Methods Structures Pad RightSplit 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 SourceConfigure Access Data Source Add Product Class Extract Local Database Data Order PageLoad VB CodeAdd New Web Page Set Start Page Display Cart Aspx CodeDisplay Cart Design View Sorted List Definition VB.NET Session State Create CartItem Class GetCartContents FunctionAdd To Cart Event Handler Remove Cart Item EventClear Cart Event Handler |
|||||||||||||||||||||||||||||||||||||||||||||||||