While End Loop
While End Loop
Syntax:
1. While [condition]
2. [ Statement to be executed ]
3. End While
Here, condition represents any Boolean condition, and if the logical condition is true
the single or block of statements define inside the body of the while loop is executed.
Example: Write a simple program to print the number from 1 to 10 using while End loop
in VB.NET.
while_number.vb
1. Imports System
2. Module while_number
3. Sub Main()
4. 'declare x as an integer variable
5. Dim x As Integer
6. x=1
7. ' Use While End condition
8. While x <= 10
9. 'If the condition is true, the statement will be executed.
10. Console.WriteLine(" Number {0}", x)
11. x = x + 1 ' Statement that change the value of the condition
12. End While
13. Console.WriteLine(" Press any key to exit...")
14. Console.ReadKey()
15. End Sub
16. End Module
Output:
In the above example, while loop executes its body or statement up to the defined state (
<= 10). And when the value of the variable i is 11, the defined condition will be false; the
loop will be terminated.
Example 2: Write a program to print the sum of digits of any number using while End
loop in VB.NET.
Total_Sum.vb
Syntax
Write a program to understand the Nested While End loop in VB.NET programming.
Nest_While.vb
1. Imports System
2. Module Nest_While
3. Sub Main()
4. ' Declare i and j as Integer variable
5. Dim i As Integer = 1
6.
7.
8. While i < 4
9. ' Outer loop statement
10. Console.WriteLine(" Counter value of Outer loop is {0}", i)
11. Dim j As Integer = 1
12.
13. While j < 3
14. 'Inner loop statement
15. Console.WriteLine(" Counter value of Inner loop is {0}", j)
16. j = j + 1 ' Increment Inner Counter variable by 1
17. End While
18. Console.WriteLine() 'print space
19. i = i + 1 ' Increment Outer Counter variable by 1
20. End While
21. Console.WriteLine(" Press any key to exit...")
22. Console.ReadKey()
23. End Sub
24. End Module
Output:
In the above example, at each iteration of the outer loop, the inner loop is repeatedly
executed its entire cycles until the inner condition is not satisfied. And when the condition
of the outer loop is false, the execution of the outer and inner loop is terminated.