II

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

ArguS academy

V.B PART 2

Looping Constructs
A loop construct is used whenever a statement or group of statements have to be executed a number of time. VB.NET supports
the following type of loops:
While ..end while
Do.loop
For. Next
For Each.next
While..End While Loop
The control first checks for the condition and as long as the condition is true, the statements enclosed within the While.End
While block is executed. Each time before it before it executes the statements, the condition is tested. If the condition evaluates
to False before the control enters the loop, the loop statements will not be executed even once.
E.g.
Module mloop
Sub main()
Dim i as integer
i=1
While i<=10
System.Console.WriteLine(i)
i=i+1
End while
End sub
End module
Do.loop
Do loop construct is similar to the while end while construct. If the condition evaluates to True, then the loop statements are
executed until the condition becomes False. The Do keyword may be followed by a while or until keyword along with a condition
expression but this is not mandatory. If however there is no while or until keyword included after the do, then the loop
Module mloop2
Sub main()
Dim a as integer
Dim b as integer
Dim c as integer
Dim n as integer
a=0
b=1
n=3
System.Console.Write(a& )
System.Console.Write(b& )
Do
c=a+b
System.Controle.WriteLine(c& )
a=b
b=c
n=n+1
loop while<=10
end sub
end module
Fool.Next loop
This loop construct is usually used when the limit of the loop is known for certain. A for next statement specifies a lower limit, a
higher limit, and an optional step value.
Module mloop3
Sub main()
Dim i as integer
For i=1 to 10
System.Console.WriteLine(i)
Next i
End sub

Page 1

ArguS academy

V.B PART 2

End module
For Each.next
Module mloop4
Sub main()
Dim i as integer
For i=0 to 10 step 2
System.Console.WriteLine(i)
Next
End sub
End module
Nested loop
It is possible to nest one loop within another in vb.net. the loops may be of the same type or different types. For instance, we can
have a fornext loop embedded within another for .next loop.
Module mloop5
Sub main()
Dim i as integer
Dim j as integer
For i=1 to 5
For j=1 to i
System.Console.Write(*)
Next j
System.Console.Writeline()
Next j
End sub
End module
Exit statement
The exit statement causes control to be transferred to another section of the program.
Module mloop6
Sub main()
Dim i as integer
i=10
while i<100
System.Console.WriteLine(i)
if i mod 7=0
exit while
end if
i=i+1
end while
end sub
end module

Array
An array is an entity that contains multiple variables of the same type. These variables are referred to by a single name. the
variables are called as the elements of the array. The elements within an array are stored consecutively one after another. If we
need to store the names of 15 students, instead of using 15 variables, we can use an array which can be accessed using a single
name.
Example 1
Module marr
Sub main()
Dim values() as Boolean={False,True,True}
Dim i as Integer
For i=0 to 2
System.Console.WriteLine(values(i))
Next

Page 2

ArguS academy

V.B PART 2

End sub
End module
Example 2
Module marr1
Sub main()
Dim names(5 ) as String
Dim i as integer
System.Console.WriteLine(Enter 5 name)
For i=0 to 5
names(i)=System.Console.ReadLine()
next
System.Console.WriteLine(The name entered are-)
For i=0 to 5
System.Console.WriteLine(Name (i) is &names(i))
Next
End sub
End module
Example 3
Module marr2
Sub main()
Dim arr(10) as integer
Dim i as integer
Dim sum as integer
sum=0
System.Console.writeLine(Enter 10 nunber)
For i=0 to 9
arr(i)=System.Console.ReadLine()
next
System.Console.WriteLine(Array Element)
For i=0 to 9
System.Console.WriteLine(arr(i))
sum=sum+arr(i)
Next
System.Console.WriteLine(Sum of all no. &sum)
End sub
End module
Array related functions
VB.Net provides a number of functions and keywords that are used with arrays.
Module mrra5
Sub main()
Dim nums(9) as Single
Dim nums2() as Single
Dim i as Integer
For i=0 to 9
nums(i)=(i+1)/10
next
nums2=nums
System.Console.writeLine(isArray (nums2))
System.Console.writeLine(ubound (nums2))
System.Console.writeLine(LBound (nums2))
End sub
End module
Redim keyword
Redim cannot be used to change the data type of an array nor can it be used to changed the initialization values of an array. It is
only used to change the size of an array.

Page 3

ArguS academy

V.B PART 2

Module marr6
Sub main()
Dim nums(10) as integer
Dim i as integer
Redim nums(5) Array is redeclared with upper bound 5
For i=0 to 5
nums(i)=i
next
System.Console.writeLine(Array elements)
For i=0 to 5
System.Console.WriteLine(nums(i))
Next
End sub
End module
Multidimensional Arrays
We can also create multidimensional arrays in VB.Net. An array can have a maximum of 32 dimensions though we rarely use
more than three dimensions.
Module mrra9
Sub main()
Dim arr(3,3) as integer
Dim i as integer
Dim j as integer
System.Console.writeLine(Enter array element)
For i=0 to 2
For j=0 to 2
arr(i,j)=System.Console.ReadLine()
next
next
System.Console.writeLine(Array Element)
For i=0 to 2
For j=0 to 2
System.Console.write(arr(i,j)& )
Next
System.Console.WriteLine()
Next
End sub
End module

Procedures and Function


Procedures and functions are blocks of code in a program. They are also termed as sub programs and are basically used to avoid
repetition of the same code a number of times. By making use of procedures and functions, we save time thereby making
development faster.
Procedures
A VB.NET procedures is written within a sub End sub block.
Module mf1
Sub main()
Dim j as integer
System.Console.WriteLine(Enter any no)
j=System.Console.ReadLine()
square(j)
System.Console.WriteLine(End of Main program)
End sub
Sub square(x as integer)
System.Console.WriteLine(x*x)
End sub
End module
Example 2
Module mf2

Page 4

ArguS academy

V.B PART 2

Sum main()
Dim att as char
System.Console.WriteLine(R for Regular, A for Absentee and I for Irregular)
System.Console.WriteLine(Enter Attendance)
att=System.Console.ReadLine()
total(att)
end sub
sub total(x as char)
Dim m1 as Integer
Dim m2 as integer
Dim m3 as integer
Dim ave as single
If x=A or x=I
System.Console.WriteLine(Absentee or Irregular hence no result calculated)
Elseif x=r or x=R
System.Console.WriteLine(Enter 3 marks )
m1=System.Console.ReadLine()
m2=System.Console.ReadLine()
m3=System.Console.ReadLine()
ave=(m1+m2+m3)/3
System.Console.WriteLine(Average marks is &ave)
End if
End sub
End module
Function Procedures
Function procedures are almost similar to sub procedures except that function procedures can return values back to the calling
procedure. Function procedures are enclosed between function ...end function block
Module fm3
Sub main()
Dim num as integer
Dim f as long
System.Console.WriteLine(Enter a number)
num=System.Conlose.ReadLine()
f=fact(num)
System.Console.WriteLine(Factorial is &f)
End sub
Function fact(x as integer) as integer
Dim fa as long
Dim i as integer
fa=1
for i=1 to x
fa=fa*i
next i
return fa
end function
end module
Passing arguments to functions and procedures
Arguments can be passed to functions and procedures in VB.NET using the ByVal or ByRef keywords. If we do not specify any of
these keywords, it is assumed to be ByVal.
ByVal Keyword
The ByVal keywords is used to pass an arguments by value. It indicates that the value of the variable should not modified by the
called procedure or function.
Module val
Sub disp(ByVal x as integer)
x=x+2
System.Console.WriteLine(The value of X= &x)
End dub
Sub main()

Page 5

ArguS academy

V.B PART 2

Dim n as integer
n=10
System.Console.WriteLine(The Value of n= &n)
disp(n)
System.Console.WriteLine(The Value of n after function= &n)
End sub
End module
ByRef Keywords
The ByRef keyword is used to indicate that the arguments are passed by reference and not value.
Module ref
Sub disp(ByRef x as integer)
x=x+2
System.Console.WriteLine(The Value of x= &x)
End sub
Sub main()
Dim n as integer
n=10
System.Console.WriteLine(The Value inside main=&n)
disp(n)
System.Console.WriteLine(The Value after function =&n)
End sub
End module
Loop Assignment
WAP
*
**
***
****
1
121
12321
1234321
1.
2.
3.

1
23
345
4567
**********
**
**
**
**
**********

7531
531
31
1
1
01
010
0101

7777
555
33
1
1
01
101
0101

WAP to find the sum of series


S=1+(1*2)+(1*2*3)+..n
WAP to accept a number and find the sum of digits of the number e.g. input 453, output : 4+5+3=12
WAP to accept a number and find all the factors e.g. input 15 output: factors of 15 = 1,3,5

3.
4.

Array Assignment
WAP to read price of 5 item in an array and then display sum of all prices, product of all the prices and average of them.
WAP to count the number of employee earning more than Rs. 1 lakh per annum. The monthly of 10 employee accept
from user
WAP to delete duplicate elements from vector.
WAP to print the upper and lower triangle of a matrix.

1.
2.
3.

Function Assignment
WAP swap the value between two value by using ByRef Method.
WAP accept 10 no from user and display the maximum no and minimum no by using function.
WAP accept 10 roll no and 10 name in two different single dimension array and find the name of any roll no.

1.
2.

Page 6

You might also like