﻿ ﻿
﻿

"For ... Next" Statement Example Examples

This section provides a tutorial example on how to use 'For ... Next' statements to repeat a block of statements.

﻿

To help you understand how "For ... Next" statements work, I wrote the following the example, loop_for.html:

```<html>
<body>
<!-- loop_for.html
-->
<pre>
<script language="vbscript">
document.writeln("")
For i = 3 To 30 Step 2
bIsPrime = True
For j = 2 To i\2
bIsPrime = i Mod j > 0
If Not bIsPrime Then Exit For
Next
If bIsPrime Then
document.writeln("Found a prime number: " & i)
End If
Next
</script>
</pre>
</body>
</html>
```

Here is the output:

```Found a prime number: 3
Found a prime number: 5
Found a prime number: 7
Found a prime number: 11
Found a prime number: 13
Found a prime number: 17
Found a prime number: 19
Found a prime number: 23
Found a prime number: 29
```

The output looks good. Some notes about the sample script:

• Nested loops, one loop inside another loop, are used.
• The outer loop goes through integers between 3 and 20, and skips even numbers with "Step 2"
• The inner loop goes determines if the current integer "i" is a prime or not
• The inner loop has a conditional "Exit For" statement to terminate the loop as soon as "i" has been determined as a non-prime number.

﻿

﻿ ﻿