Search This Blog

Thursday 27 October 2011

For loop & For Each loop with Examples


A)    For loop:-
We can use this loop to run a block of code specified number of times.

                        Syntax:-          for i=1 to n
                                                     ---------                ====è loop runs n times.
                                                     ---------         
                                                Next

EX:-    for i=1 to 10 step 2
                        ---------             =====è loop runs  5 times
                        ---------
                        Next



EX:-    for i=10 to 1 step (-1)
                        ---------             =====è loop runs 10 times
                        ---------
                        Next
B)    For Each loop:-
We can use this loop to execute a block of code for each item in given list.

                        Syntax:-          for each i in list/array
                                                            ---------
                                                            ---------
                                                Next
Arrays:-
                        Array is a list of values in any type.

                        EX:-    x(4)
101
Kishore
23.4
A
23a4
                              X(0)           x(1)             x(2)             x(3)                x(4)

From the above example, x is an Array with 5 elements . every array first element index is zero, called as Lbound. In index of last element in an array Ubound. So the size of array is Ubound+1.

To store data to an array , we can use below code.

Method:-1                                                                               Method:-2
Static Array                                                                           Dynamic Array

Option explicit                                                                        option explicit
Dim x                                                                                      dim x(4),i
X=array(“kish”,23,43.22)                                                       for i=0 to 4
                                                                        X(i)=inputbox(“enter value”)
                                                                        Next

            EX:-
                        To read 5 subject marks of a student and then display total marks.
                       
                                    Option explicit
                                    Dim x(5),I,total
                                    For i=0 to 4
                                                X(i)=inputbox(“enter the subject marks”)
                                    Next
                                    Total=0
                                    For i=0 to 4
                                                Total=total+x(i)
                                    Next
                                    Print(“total=”&total)

           


Ex:-2
                        To read 5 subject marks of  a student and then display highest marks in list.

                                    Option explicit
                                    Dim x(5),I,max
                                    For i=0 to 4
                                                X(i)=inputbox(“enter the subject marks”)
                                    Next
                                    Max=x(0)
                                    For i=1 to 4
                                                If  cint(max)<cint(x(i)) then
                                                            Max=x(i)
                                                End if
                                    Next
                                    Print(“maximum marks=”&max)

Ex:-3
            To read specify no:of employees & to read every emp salary. If salary graters then equal 30000 the comm. 10% . sal<30000 and sal>=15000, then comm is 5% .If  sal<15000, then  comm is 200. Here gross salary= bsal+comm. We can replace bsal with gross in that array.

                                    Option explicit
                                    Dim emp(10), noe, i, comm., gsal
                                    noe=inputbox(“enter no:of employess”)
                                    for i=0 to noe-1
                                                emp(i)=inputbox(“enter emp bsal”)
                                    next
                                    for i=0 to noe-1
                                                if(emp(i)>=30000) then
                                                            comm.= emp(i)*10/100
                                                            gsal=emp(i)+comm.
                                                            Emp(i)=gsal
                                                Elseif(emp(i)<30000 and emp(i)>=15000) then
                                                            Comm.=emp(i)*5/100
                                                            Gsal=emp(i)+comm.
                                                            Emp(i)=gsal
                                                Else
                                                            Comm.=200
                                                            Gsal=emp(i)+comm.
                                                            Emp(i)=gsal
                                                End if
                                    Next
                                    For i=0 to noe-1
                                                Print(emp(i))
                                    Next

6 comments:

  1. Thanks for sharing such informative article on Loadrunner Automation testing tool. This load testing tool will provide most precise information about the quality of software.Loadrunner Training in Chennai | Loadrunner training institute in Chennai|Qtp training institutes in chennai

    ReplyDelete
  2. The future of software testing is on positive note. It offers huge career prospects for talented professionals to be skilled software testers.
    Regards,
    Software testing training in Chennai| Best software testing training institute in Chennai|Software testing courses in Chennai

    ReplyDelete
  3. Hello there! This is my first comment here, so I just wanted to give a quick shout out and say I genuinely enjoy reading your articles. Can you recommend any other blogs/websites/forums that deal with the same subjects? Thanks.
    fire and safety course in chennai

    ReplyDelete