0% found this document useful (0 votes)
21 views5 pages

While End Loop

Lets not waste time and be a pro in the looping structure

Uploaded by

narshelmalipa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views5 pages

While End Loop

Lets not waste time and be a pro in the looping structure

Uploaded by

narshelmalipa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

VB.

NET While End Loop


The While End loop is used to execute blocks of code or statements in a program, a
long as the given condition is true. It is useful when the number of executions of a block
is not known. It is also known as an entry-controlled loop statement, which means i
initially checks all loop conditions. If the condition is true, the body of the while loop is
executed. This process of repeated execution of the body continues until the condition is
not false. And if the condition is false, control is transferred out of the 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.

Flow Diagram of the While End Loop in VB.NET


As we know, the While End loop is an entry-controlled loop used to determine if the
condition is true, the statements defined in the body of the loop are executed, and the
execution process continues till the condition is satisfied. Furthermore, after each
iteration, the value of the counter variable is incremented. It again checks whether the
defined condition is true; And if the condition is again true, the body of the While loop is
executed. And when the condition is not true, the control transferred to the end of the
loop.

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

1. Public Class Total_Sum ' Create a Class Total_sum


2. Shared Sub Main()
3. 'Declare an Integer variable
4. Dim n, remainder, sum As Integer
5. sum = 0
6.
7. Console.WriteLine(" Enter the number :")
8. n = Console.ReadLine() ' Accept a number from the user
9.
10. ' Use While loop and write given below condition
11. While (n > 0)
12. remainder = n Mod 10
13. sum += remainder
14. n = n / 10
15. End While
16. Console.WriteLine(" Sum of digit is :{0}", sum)
17. Console.WriteLine(" Press any key to exit...")
18. Console.ReadKey()
19. End Sub
20. End Class
Output:

Nested While End Loop


In VB.NET, when we write a While End loop inside the body of another While End loop, it is
called Nested While End loop.

Syntax

1. While (condition -1)


2. // body of the outer while loop
3. While (condition -2)
4. // body of inner while loop
5. End While
6. // body of the outer loop
7. End While

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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy