Repeating statements while a condition is true, Visual Basic Programming

Use the While keyword to verify a condition in a Do...Loop statement. You can verify the condition before you enter the loop (as illustrated in the following ChkFirstWhile instance), or you can verify it after the loop has run at least once (as illustrated in the ChkLastWhile instance). In the ChkFirstWhile procedure, if myNum is set to 9 rather than 20, the statements inside the loop will never run. In the ChkLastWhile process, the statements inside the loop run only once since the condition is already False.

Sub ChkFirstWhile() Dim counter, myNum counter = 0

myNum = 20

Do While myNum > 10 myNum = myNum - 1 counter = counter + 1

Loop

MsgBox "The loop made " & counter & " repetitions." End Sub

Sub ChkLastWhile() Dim counter, myNum counter = 0

myNum = 9

Do

myNum = myNum - 1 counter = counter + 1

Loop While myNum > 10

MsgBox "The loop made " & counter & " repetitions." End Sub

The above figure illustrate two example code fragments illustrating the do... loop and do while ... loop statements. In the first loop the condition is verified before executing the code however in the second loop the statements are first executed once and after that the condition is verified. In the first loop the instructions given in the loop's body will never be executed if the condition initially fails but in the second loop these instructions would be executed at least once.  The following flow chart illustrates the execution plan of the second procedure i.e. CheckLastWhile:

1225_Repeating Statements While a Condition is True.png

                Figure: Execution Plan of the Second Loop

Posted Date: 4/18/2013 5:46:49 AM | Location : United States







Related Discussions:- Repeating statements while a condition is true, Assignment Help, Ask Question on Repeating statements while a condition is true, Get Answer, Expert's Help, Repeating statements while a condition is true Discussions

Write discussion on Repeating statements while a condition is true
Your posts are moderated
Related Questions
A Sub procedure is a series of VBScript statements, enclosed through Sub & End Sub statements which perform actions however don't return a value. A Sub procedure can take arguments

Displaying The Database: The Text box, image box, check box, labels, picture box can be bounded to the data control for exhibiting the data base contents. The two properties a

Question 1 Write about the Visual Basic Application Development Life cycle with a sample Forms development for performing integer arithmetic operations Question 2 Discuss the

Each piece of data is passed in your process using an argument. Arguments serve like placeholders for the data you wish to pass into your process. You can name your arguments along

Assume that s is a string variable that is supposed to contain a value to be converted to integer. Write a fragment of code that converts the value to integer variable and displ

The Document: The document contains the user's data. For illustration, after working on a text processor, the user may wish for to save the file. Such an action builds a docum

Write a program that permits the user to order a product from inventory. The user selects the product and enters the quantity. If there is sufficient quantity on hand of the produc

i need sample balance sheet form in vb.net

Databound Controls - A controls use to display the contents of table using Data Control , have to bound each Control to the specific field and the controls are Known as the Data Bo

ILLUSTRATION OF CCOLORDIALOG The constructor of the CColorDialog takes the initial parameter as the default color and the next parameter specifies how much the dialog is displa