You are on page 1of 13

In 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 Sub Main()

Dim X As Integer Dim Y As Integer Dim 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 Sub Main() X As Integer Y As Integer cnt As Integer 'Our counter.

For 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

Create a windows application to calculate for swapping two numbers.


Dim a As integer Dim b As integer a=a+b b=a-b a=a-b End Sub End class

Create a windows application to calculate to generate Fibonacci series. Private Sub Command1_Click() Dim x, g, n, i, sum As Integer n = Val(Text1.Text) x = 0 y = 1 Print x Print y For i = 3 To n sum = x + y Print sum x = y y = sum y = sum Next i End Sub

--------------------------------------------Create a windows application to calculate factorial of a number.


Private Sub Command1_Click() Dim j, i as Integer i = CInt(Text1.Text) Label2.Caption = facts(i) End Sub Private Function facts(i) F=1 For j = 1 To i F=F*j facts = F Next j End Function

Create a windows application to calculate simple interest


Private Sub Command1_Click() Dim a Dim b, c, d As Double a = Val(Text1.Text) b = Val(Text2.Text) c = Val(Text3.Text) d = (a * b * c) / 100 Label6.Caption = d

End Sub Private Sub Command2_Click() Text1.Text = "" Text2.Text = "" Text3.Text = "" Label6.Caption = "" End Sub Private Sub Command3_Click() End End Sub Private Sub Form_Load() End Sub

Create a program with a textbox and one button control check the year is leap year or Not.
Private Sub cmdCalc_Click() txtYear = IsLeapYear(txtYear)

If TorF = True Then lblSolution.Caption = "The year " & txtYear & " is a leap year." Else lblSolution.Caption = "This is not a leap year." End If End Sub

Create a program with a textbox and one button control to check no is even or odd.
Private Sub Command1_Click() Dim num As Integer num = Val(Text1.Text) If num Mod 2 = 0 Then Label2.Caption = "Result : The number is even" Else Label2.Caption = "Result : The number is odd" End If End Sub

Private Sub rev_no() Dim rev As Integer old = Val(Text1.Text) While (Round(old) > 0) rev = rev * 10 + old Mod 10 old = old / 10 Wend End Sub

WAP for sum of N Numbers?

(B) Property(Controls Used) :


2 Labels(label1 & label2) as Captions Sum of 1 to Number and

2 other Labels also needed (name lbl_num and lbl_sum) for storing the value of n and the sum from 1 to n Horizontal Scroll Bar (Name-HScroll1,Min-1,Max-100,LargeChange-5)

(C) Attaching Code to the Object :

Private Sub HScroll1_Change() lbl_num.Caption = HScroll1.Value lbl_sum.Caption = (HScroll1.Value + 1) * HScroll1.Value / 2 End Sub

Write a VB application for show the day for selected date?

Attaching Code to the Object : Private Sub DTPicker1_Change() Select Case Weekday(DTPicker1) Case 1: Text1.Text = "Sunday" Case 2: Text1.Text = "Monday" Case 3: Text1.Text = "Tuesday" Case 4: Text1.Text = "Wednesday" Case 5: Text1.Text = "Thursday" Case 6: Text1.Text = "Friday" Case 7: Text1.Text = "Saturday" End Select End Sub Private Sub Form_Load() DTPicker1_Change End Sub

WRITE A Vb Application for using radion button?

CODE FOR :

(B) Property(Controls Used) :


One Label to Entitle the program "Select Form's Background color" 3 OptionButtons(Captions as Red,Green & Blue) with name Option1 (Same name creates a control array with index 0,1 & 2 .That is the name of the OptionButtons are : Option1(0),Option1(1) and Option1(2) Form name is Form1

(C) Attaching Code to the Object :

Private Sub Option1_Click(Index As Integer) Select Case Index Case 0 Form1.BackColor = vbRed Case 1 Form1.BackColor = vbGreen Case 2 Form1.BackColor = vbBlue End Select End Sub

You might also like