Breaking News
You are here: Home / Notes / SEM-4 / .NET PROGRAMMING / Loop Control Statements in vb.net

Loop Control Statements in vb.net

Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed.

VB.Net provides the following control statements. Click the following links to check their details.

Control Statement Description
Exit statement Terminates the loop or select case statement and transfers execution to the statement immediately following the loop or select case.
Continue statement Causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating.
GoTo statement Transfers control to the labeled statement. Though it is not advised to use GoTo statement in your program.

Exit Statement

The Exit statement transfers the control from a procedure or block immediately to the statement following the procedure call or the block definition. It terminates the loop, procedure, try block or the select block from where it is called.

If you are using nested loops (i.e., one loop inside another loop), the Exit statement will stop the execution of the innermost loop and start executing the next line of code after the block.

vb.net_exit_statement

 

 

Example:

Module loops
   Sub Main()
      ' local variable definition 
      Dim a As Integer = 10
      ' while loop execution '
      While (a < 20)
          Console.WriteLine("value of a: {0}", a)
          a = a + 1
          If (a > 15) Then
              'terminate the loop using exit statement 
              Exit While
          End If
      End While
      Console.ReadLine()
   End Sub
End Module

 

Continue Statement

continue_statement

The Continue statement causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating. It works somewhat like the Exit statement. Instead of forcing termination, it forces the next iteration of the loop to take place, skipping any code in between.

For the For…Next loop, Continue statement causes the conditional test and increment portions of the loop to execute. For the While and Do…While loops, continue statement causes the program control to pass to the conditional tests

Example:

Module loops
   Sub Main()
      ' local variable definition 
      Dim a As Integer = 10
      Do
          If (a = 15) Then
              ' skip the iteration '
              a = a + 1
              Continue Do
          End If
          Console.WriteLine("value of a: {0}", a)
          a = a + 1
      Loop While (a < 20)
      Console.ReadLine()
   End Sub
End Module

 

GoTo Statement

The GoTo statement transfers control unconditionally to a specified line in a procedure.

The syntax for the GoTo statement is:

goto_statement

 

Example:

Module loops
   Sub Main()
      ' local variable definition 
      Dim a As Integer = 10
Line1:
      Do
          If (a = 15) Then
              ' skip the iteration '
              a = a + 1
              GoTo Line1
          End If
          Console.WriteLine("value of a: {0}", a)
          a = a + 1
      Loop While (a < 20)
      Console.ReadLine()
   End Sub
End Module

 

 

 

Download Paper Solutions from Below Link

About admin

Scroll To Top