adsense


Showing posts with label VB SCRIPT FOR LOOPS. Show all posts
Showing posts with label VB SCRIPT FOR LOOPS. Show all posts

Friday, 6 November 2015

VB SCRIPT FOR LOOPS PAGE 15

COMPUTER LANGUAGES HTML,C,C++.JAVA,.NET AND MULTIMEDIA basics and programs click home button




VB SCRIPT FOR LOOPS





For Loops 

A for loop is a repetition control structure that allows 
a developer to efficiently write a loop that needs to 
execute a specific number of times. 
Syntax 
The syntax of a for loop in VBScript is: 
For counter = start To end [Step stepcount] 
  [statement 1] 
  [statement 2] 
  .... 
  [statement n] 
  [Exit For] 
  [statement 11] 
  [statement 22] 
  .... 
  [statement n] 
Next 
 
 
 
Here is the flow of control in a For Loop: 

• The For step is executed first. This step allows you to 
initialize any loop control variables and increment the step counter variable. 

 

• Secondly, the condition is evaluated. If it is true, 
the body of the loop is executed. If it is false, the 
body of the loop does not execute and flow of control 
jumps to the next statement just after the For Loop. 

 

• After the body of the for loop executes, the flow of 
control jumps to the Next statement. This statement allows 
you to update any loop control variables. It is updated 
based on the step counter value. 

 

• The condition is now evaluated again. If it is true, 
the loop executes and the process repeats itself 
(body of loop, then increment step, and then again condition). 
After the condition becomes false, the For Loop terminates. 

 
Example 
<!DOCTYPE html> 
<html> 
<body> 
<script language="vbscript" type="text/vbscript"> 
 
   Dim a : a=10 
   For i=0 to a Step 2 'i is the counter variable and it is incremented by 2 
     document.write("The value is i is : " & i) 
     document.write("<br></br>") 
   Next 
    
</script> 
</body> 
</html> 
When the above code is compiled and executed, it produces the following result: 
The value is i is : 0 
 
The value is i is : 2 
 
The value is i is : 4 
 
The value is i is : 6 
 
The value is i is : 8 
 
The value is i is : 10 
For...Each Loops 
A For Each loop is used when we want to execute a statement
 or a group of statements for each element in an array or collection. 
A For Each loop is similar to For Loop; however, the loop 
is executed for each element in an array or group. Hence, 
the step counter won't exist in this type of loop and it 
is mostly used with arrays or used in context of File 
system objects in order to operate recursively. 
Syntax 
The syntax of a For Each loop in VBScript is: 
For Each element In Group 
  [statement 1] 
  [statement 2] 
  .... 
  [statement n] 
  [Exit For] 
  [statement 11] 
  [statement 22] 
Next 
Example 
<!DOCTYPE html> 
<html> 
<body> 
<script language="vbscript" type="text/vbscript"> 
   
 'fruits is an array  
 fruits=Array("apple","orange","cherries") 
 Dim fruitnames 
 
 'iterating using For each loop.  
 For each item in fruits 
    fruitnames=fruitnames&item&vbnewline 
 Next 
 
 msgbox fruitnames 
    
</script> 
</body> 
</html> 
 
 
When the above code is executed, it prints all 
the fruitnames with one item in each line. 
apple 
orange 
cherries