For...Next Loop

For...Next Loop:

This loop executes one or more statements for a specified number of times.

Syntax:

The Syntax of loop is as follows:

Working of For...Next Loop:

The number of iterations of counter variable is depend upon the starting and ending value of  counter variable.By default, the counter variable is incremented by 1 but we can change its incremented value by using step clause that is optional for example. If we want to increment our counter variable 2 times each clause then we write this code.
  For i=0 To 5 step 2
        Statement(s)
  Next

If we want to decrement our counter variable by one then we write this code:

For i=5 To 0 step -1
        Statement(s)
Next

Flow Chart:

Flow Chart of loop is as follows:



















Example-1:

Write program in VB that display the sum of first five numbers.
  •  Start a new standard EXE project.
  • Design the form as follows.














  • Open the click event of the button and add the following code in it. 












  • Run The program and click on button.
  • Output is display as.













How it Works:

In the above example, the value of counter variable is i incremented from 0 to 5. Step option is not used because this loop can increment 1 the value of counter variable.

Example-2:

Write a program that displays the first five odd numbers using For...Next loop.
  •  Start a new standard EXE project.
  • Design the form as follows.













  • Open the click event of the button and add the following code in it. 











  • Run The program and click on button.
  • Output is display as.













Example-3:

Write a program that display the table of 2 using For...Next loop.

  •  Start a new standard EXE project.
  •  Design the form as follows.























  • Open the click event of the button and add the following code in it. 













  • Run The program and click on button.
  • Output is display as.





















Example-4:

  •  Start a new standard EXE project.
  •  Design the form as follows.

  • Open the click event of the button and add the following code in it. 

  • Run the project click on the button.
  • Your program take input from you.
  • Output is display as.

Early Termination of Loops:

Normally, The  termination of loop is depend upon the specific condition but we can terminate our loop earlier without looking at the condition. Exit For  statement is used in the body of For...Next  Loop to terminate it abnormally.

Example-5:

Write a VB program that gets five number from user and displays whether the number is even or odd. But if user enter a negative number, it is terminated immediately.
  •  Start a new standard EXE project.
  •  Design the form as follows.




















  • Open the click event of the button and add the following code in it. 
  • Run the project click on the button.
  • Your program take input from you.
  • Output is display as.























No comments:

Post a Comment