|
Hot-Link Menu in Right-Side Column |
Last Updated 6-30-2010 |
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
7.7 Do-Until...LoopDo-Until is another pretest loop. the logic of Do-Until is the opposite of the Do-While loop. The condition is for termination of the loop. In other words, the loop stops when the condition evaluates to True. To better understand the logic of Do-Until, let us consider drinking a glass of iced water on a hot summer day. You might say "I will drink the water, until the glass is empty." You might consider a Do-Until loop, whenthe number of iterations is not known and the logic of the problem suggests that the loop should continue until the condition becomes True. There is usually a loop variable used to form the condition. Like a Do-While loop, the initialization and updating of the loop variable should be done explicitly. Below is the general syntax fo a Do-Until loop: General Syntax (keywords are in bold):
Key Points to Remember
Visual Basic VB.NET - EXAMPLE 4Assignment: Write a Do-Until loop to add up all the numbers from 0 to 100, i.e. 0+1+2+3...+100. Although this is a good problem to code using a For loop, you can use a Do-Until loop as well. To write a computer program for this problem, you need two variables: one variable to add up the numbers and a loop variable to assume the values 0,1,2...100. The variables should be initialized to 0 before the loop. The condition of the loop should be for stopping the loop. Hence, the condition should be until the loop variable is greater than 100. In the body of the loop, the loop variable should be added to the sum of the numbers, and then incremented by 1 to assume the next value. Below is the pseudocode for solving this problem: Pseudocode for Example 7-4
Create a Label for output changing the characteristics as indicated in the diagram below. Change the Form Text to Do-Until Sum 0 to 100 and double-click on the form to enter the text in the diagram below: Solution to 7.4
Posttest LoopsIn a posttest loop, the condition of the loop is checked at the end of each cycle. The body of a posttest loop gets executed at least once, regardless of the condition. Sometimes the logic of the problem's solution fits well into a posttest loop. However, every solution that requires a loop can be coded using one of the pretest loops covered in Sections 7.5 to 7.7. 7.8 Do...Loop-WhileThe statements surrounded with Do and Loop get executed at least once. The condition is examined at the end of each cycle. The loop continues while the condition evaluates to True. General Syntax (keywords are in bold):
Key Points to Remember
Visual Basic VB.NET - EXAMPLE 5Assignment: Write a Do..Loop-While to add up all the numbers from 1 to 100, i.e. 1+2+3...+100. As discussed in previous sections, a computer solution for this problem requires two variables: one variable to add up the numbers and another to be used as a loop variable, which will assume the values 1,2,3....100. Below is the pseudocode for this problem using a Do...Loop-While. Pseudocode for Example 7-5
Solution to 7.5Create a Label for output changing the characteristics as indicated in the diagram below. Change the Form Text to: Sum 1 to 100 with Do...Loop-While and double click on the form to enter the text in the diagram below:
7.9 Do...Loop-UntilThe statements surrounded with the Do and Loop get executed at least once. The loop stops when the condition at the end of a cycle evaluates to True. General Syntax (keywords are in bold):
Key Points to Remember
Visual Basic VB.NET - EXAMPLE 6Assignment: Write a Do...Loop-Until to add up all the numbers from 1 to 100, i.e. 1+2+3...+100. Pseudocode for Example 7-6
Solution to 7-6Create a Label output for changing the characteristics as indicated in the diagram below. Change the Form Text to Sum 1 to 100. Double-click on the form to enter the text as 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 |
|||||||||||||||||||||||||||||||||||||||||||||||||||