2. Looping Statement Loop statements are used to execute a block of statements repeatedly. For example to execute a statement 100 times, it is not necessary to write the statement 100 times in the program. VB.NET supports 3 types of loop statements While Do for 2 Department of CE/IT
3. While This loop executes a set of statements when a condition is true. The statement block is executed repeatedly until the condition becomes false. 3 Department of CE/IT
4. Syntax While <condition> Statement End while 4 Department of CE/IT
5. Example Dim n As Integer N=0 While n<10 MsgBox(“Welcome to Ustudy.in”) N=n+1 End While This program displays the word “Welcome to Ustudy.in “ 10 times. 5 Department of CE/IT
6. Do Loop While The Do loop can be used to execute a fixed block of statements indefinite number of times. The Do loop keeps executing it's statements while or until the condition is true. Two keywords, while and until can be used with the do loop. The Do loop also supports an Exit Do statement which makes the loop to exit at any moment. 6 Department of CE/IT
7. Syntax 1 Example: Dim n As Integer n=10 Do While (n<5) n=n+10 Loop MsgBox(n) Output: 10 Do While <condition> Statement block Loop 7 Department of CE/IT
8. Syntax 2 Example: Dim n As Integer n=10 Do n=n+10 Loop While (n<5) MsgBox(n) Output: 20 Do Statement block Loop While <condition> 8 Department of CE/IT
9. Do loop until This loop executes a block of statements repeatedly when the condition is false. 9 Department of CE/IT
10. Syntax 1 Example: Dim n As Integer Do until n>5 n=n+10 Loop MsgBox (n) Output: 10 Do until <condition> Statement block Loop 10 Department of CE/IT
11. Syntax 2 Do Statement block Loop until <condition> Example: Dim n As Integer Do n=n+10 Loop until n>5 MsgBox (n) Output: 20 11 Department of CE/IT
12. For loop The For loop is the most popular loop. For loops enable us to execute a series of expressions multiple numbers of times. The For loop in VB .NET needs a loop index which counts the number of loop iterations as the loop executes. 12 Department of CE/IT
13. Syntax For index=start to end[Step step][statements][Exit For][statements]Next[index] 13 Department of CE/IT
14. Example For i=1 to 10 MsgBox (“Work hard God will stand by you”) Next The program display the string at 10 times 14 Department of CE/IT