Thursday, June 13, 2013

VBS :Tutorial 4 (Loops )

VBS :Tutorial 4 (Loops )

 
Loop Statements

 

1.      ·      Do...Loop: Loops while or until a condition is True.

2.      ·      While...Wend: Loops while a condition is True.

3.      ·      For...Next: Uses a counter to run statements a specified number of times.

4.      ·      For Each...Next: Repeats a group of statements for each item in a collection or each element of an array.

5.      exit do

6.      exit while

7.      exit for

 

---------------------------------------------------------------------------------Do While

 

1) Do While  condition

    Statements

    -----------                  

    -----------

    Loop

 

Example :

Dim sStr

sStr="Deepak"

i=1

 

Do while mid(sStr,i,1) <> ""

               msgbox mid(sStr,i,1)

               i=i+1

loop

 

 

2) Do

    Statements

    -----------                  

    -----------

    Loop While  condition

 

Example :

Dim sStr

sStr="Deepak"

i=1

 

Do

               msgbox mid(sStr,i,1)

               i=i+1

 

loop while mid(sStr,i,1) <> ""

 

-------------------------------------------------------------------------------------Do Until

 

3) Do Until  condition'-----------       False =loop , True=Exit

    Statements

    -----------                  

    -----------

    Loop

 

Example :

Dim sStr

sStr="Deepak"

i=1

 

Do until mid(sStr,i,1) = ""

               msgbox mid(sStr,i,1)

               i=i+1

 

loop

 

4) Do

    Statements

    -----------                  

    -----------

    Loop Until condition

 

Example :

Dim sStr

sStr="Deepak"

i=1

 

Do

               msgbox mid(sStr,i,1)

               i=i+1

 

loop until mid(sStr,i,1) = ""

 

--------------------------------------------------------------------------------------While...Wend

5) While  condition   'True= loop ,false =Exit

            Statements

            -----------          

            -----------               

   Wend

 

Example :

Dim sStr

sStr="Deepak"

i=1

 

while  mid(sStr,i,1) <> ""

               msgbox mid(sStr,i,1)

               i=i+1

 

wend

-------------------------------------------------------------------------------------For

6) For start to end [Step step]

       statements

Next

Example :

Dim sStr

sStr="Deepak"

for i=1 to len(sStr) step 1

            msgbox mid(sStr,i,1)

next

 

-----------------------------------------------------------------------------------Excercise

 

1.   Display Even numbers up to n?

2.   Display Natural numbers in reverse order up to n?

3.   Reverse a string

4.   Display String in the form of a right angled triangle ?

5.       Matrix 2D,Accept and Display

6.       Transpose--

 

-------------------------------------------------------------------

 

Dim aMat(2,2), row_count , col_count  , sRes

 

aMat(0,0)=1

aMat(0,1)=1

aMat(0,2)=1

 

aMat(1,0)=2

aMat(1,1)=2

aMat(1,2)=2

 

aMat(2,0)=3

aMat(2,1)=3

aMat(2,2)=3

 

for row_count=0 to ubound(aMat,1) step 1

 

 

        for col_count =0 to ubound(aMat,2) step 1

       

                                       

                                        sRes1=sRes1& " "& aMat(row_count ,col_count)

                                        sRes2=sRes2& " "& aMat(col_count  ,row_count  )

         

        next

 

  sRes1=sRes1&vblf

  sRes2=sRes2&vblf

 

 

next

 

 

msgbox sRes1

msgbox sRes

No comments:

Post a Comment