0% found this document useful (0 votes)
100 views6 pages

For Countervariable Fromvalue To Tovalue Next Countervariable

This document discusses different types of loops in Visual Basic that can be used to repeatedly execute blocks of code: 1. Do loops will continuously execute the code block until an Exit Do statement is reached. For Fibonacci series, a counter and If/Exit Do was used to exit after 8 iterations. 2. Do Until loops will execute the code block until the Until condition is true, at which point it will exit. This avoids needing an If/Exit Do. 3. Do While loops are similar but will exit when the While condition becomes false rather than true. 4. For/Next loops allow specifying an initial counter value, final value, and increment to automatically repeat the code block that number

Uploaded by

mkumbhcar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
100 views6 pages

For Countervariable Fromvalue To Tovalue Next Countervariable

This document discusses different types of loops in Visual Basic that can be used to repeatedly execute blocks of code: 1. Do loops will continuously execute the code block until an Exit Do statement is reached. For Fibonacci series, a counter and If/Exit Do was used to exit after 8 iterations. 2. Do Until loops will execute the code block until the Until condition is true, at which point it will exit. This avoids needing an If/Exit Do. 3. Do While loops are similar but will exit when the While condition becomes false rather than true. 4. For/Next loops allow specifying an initial counter value, final value, and increment to automatically repeat the code block that number

Uploaded by

mkumbhcar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

How to find the given no is prime or not Dim flag As Boolean = True Dim num As Integer = Val(txtNo.

Text) For i = 2 To num / 2 If (num Mod i = 0) Then flag = False Exit For End If Next i If flag = False Then MsgBox("Not Prime") Else MsgBox("Prime") End If

For counterVariable = fromValue To toValue ...VB Statements... Next counterVariable

Dim intCount As Integer For intCount = 0 To 5 MessageBox.Show("Counter is currently: " + CStr(intCount))

Next intCount

n our tutorial about if statements in Visual Basic 6 (Understanding if statements and conditions in VB6) we describe how conditional statements work - these can be used in loops as while so if you have not read that tutorial please do so now. Now continueing on with our original problem we described in our if statement tutoria how do we generate the Fibonacci series. Its formula is relatively simple: 1. 2. 3. 4. 5. Let x = 1, let y = 0. Print x. Let x = y + x Let y = x - y Repeat from 2.

This pseudo code will print out: 1, 1, 2, 3, 5, 8, 13, 21, ... as expected. But now how do we tell a Visual Basic program how to do this? The answer lies in using loops. Loops provide the ability to repeatedly execute the same block of code, and to each time change values such that each run through the loop produces different results. Visual Basic provides four main kinds of loops: the classic Do-Loop, the Do-Until Loop, the Do-While Loop, and the ForNext Loop.

Do-Loops The most basic form of loop in Visual Basic is the Do-Loop. Its construct is very simple: Do (Code to execute) Loop This, quite simply, executes the block of code, and when it reaches Loop, returns to the beginning of the Do Loop and executes the same block of code again. The same block of code will be repeatedly executed until it is told to stop executing. So let's try to apply this to our problem of generating the Fibonacci series:
Dim X As Integer Dim Y As Integer Do Debug. Print X X = Y + X Y = X - Y Loop

And, believe it or not, this code works! Well, sorta. If you try to run this code, it will indeed generate the Fibonacci series; however, it will continually generate and print out the next number infinitely--or, in this case, until it reaches an overflow error. This is known as the problem of the infinite do-loop, one that all programmers will experience, and some quite frequently. Exit Do So we clearly need some way to escape from the Do-Loop. You could, of course, simply End the program once you have calculated enough values, but what if you still need to perform tasks after you're done calculating? The answer is to use the Exit Do statement. Whenever your program reaches an Exit Do statement within a loop, it will exit the current loop. So, let's try a somewhat different approach to the Fibonacci problem. We decide that we want to calculate only eight values of the Fibonacci series, so we'll keep a counter and increment it each time throughout the loop. Then, once the counter reaches eight, we'll exit the loop.
Public Dim Dim Dim cnt Do Sub Main() X As Integer Y As Integer cnt As Integer 'Our counter. = 1 Debug.Print X X = Y + X Y = X - Y If cnt >= 8 Then Exit Do Else cnt = cnt + 1 End If Loop End Sub

And now we're talking! This program successfully computes and prints out the first eight values of the Fibonacci series. Do Until As an alternative approach to nesting an If-Statement inside the loop, and invoking Exit Do once we're done looping, Visual Basic provides a Do Until statement. Its syntax is the following: Do Until (Expression) (Code to execute) Loop

(Expression) can be any legal logical expression that we wish to evaluate to determine whether or not to exit the loop. Each time the program reaches Loop it will evaluate this expression. If the expression is True, it will exit the loop for us, but otherwise it will continue looping.. So let's try rewriting our Fibonacci program to use a Do-Until loop instead of Exit Do.
Public Dim Dim Dim Sub Main() X As Integer Y As Integer cnt As Integer 'Our counter.

cnt = 1 Do Until cnt >= 8 Debug.Print X X = Y + X Y = X - Y cnt = cnt + 1 Loop End Sub

Here we've replaced the hideous If cnt >= 8 Then ... Else: Exit Do with a very simple Until cnt >= 8. We must, however, still be sure to increment our counter every time through the loop, or else the Until expression will never be True, resulting in an infinite Do Loop. Do While In the place of Do Until, you can also use Do While. Its syntax is the following: Do While (Expression) (Code to execute) Loop (Expression) can be any legal logical expression that we wish to evaluate to determine whether or not to exit the loop. Each time the program reaches Loop it will verify that this expression is True, and if it is False, it will exit the loop for us. Thus, instead of exiting when an expression is True, it now exits only once this expression is false. Let's try rewriting our Fibonacci program to use a Do-While loop instead of a Do-Until loop.
Public Dim Dim Dim Sub Main() X As Integer Y As Integer cnt As Integer 'Our counter.

cnt = 1 Do While cnt < 8 Debug.Print X X = Y + X Y = X - Y

cnt = cnt + 1 Loop End Sub

For-Next Loops In situations where you merely want to run the loop a predefined number of times, it can become quite tiresome to have to create and manage a counter for each loop, which is why we also have something called a For-Next Loop. This kind of loop allows you to specify a counter, to tell it to count from one number to another each time through the loop, and to exit once the counter has reached its upper limit. The syntax is as follow: Dim I As Integer For I = (Integer) To (Integer) (Code to execute) Next I We used the variable name "I" above, as it is the most common name used for For-Loops; however, you can use any variable name you want, so long as the variable is of the type Integer. Now, let's improve our Fibonacci program even further:
Public Dim Dim Dim For Sub Main() X As Integer Y As Integer cnt As Integer cnt = 1 To 8 Debug. Print X X = Y + X Y = X - Y Loop End Sub

In the example above, we first dimensioned cnt as an Integer, and then, in the declaration of the For-Next loop, set its value to 1. Each time through the loop, the value of cnt was incremented by 1 until it reached 8, at which point the loop was executed. Exit For As with Do Loops, there is a statement that can be used to exit a For-Next loop, and it is called Exit For. Simply invoke this statement anywhere within a For-Next loop and the current loop will be exited. Step

By default, the variable used in the declaration of the For-Next loop is incremented by 1 each time through the loop; however, if you want to increment this value by a different amount each time through the loop, you can simply append Step (Integer) to the end of the For-Next loop declaration. If, for instance, we wanted to print out every even number counting backward from 20 to 0, we could do this using the following code:
Dim I As Integer For I = 20 To 0 Step -2 Debug. Print I Next I

So there you have it now you can use loops all over your Visaul Basic 6 programs. These are one of the most useful tools you have. You might want to bookmark this tutorial so that later you can reference back to this great VB6 loop examples. If you have any questions or comments please post them below.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy