This program explains how to use FOR loop in VB programming.
FOR Loop allows us to run one or more lines of code repeatedly without any break. FOR loop will break if the condition that you are given become false, otherwise it will continue.
Mainly used loops in VB.NET:
FOR Loop
WHILE Loop
DO WHILE Loop
In FOR Loop, your desired code must be inside FOR Statement.
Syntax for FOR Loop
|
1 2 3 4 |
For (counter = startvalue;(counter<=EndValue);Counter++) { //Code to be executed for each value of counter. } |
- Counter – Numeric value or counter for repeating the loop.
- StartValue – Value from where the counter starts the loop.
- EndValue – Valuefrom where the counter stops the loop
Using FOR loop in C# – Complete Code for FOR LOOP
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { int Counter; int StartValue; int EndValue; StartValue = 1; EndValue = 10; for (Counter = StartValue; (Counter <= EndValue); Counter++) { MessageBox .Show (("Counting Starts from " + (Counter + (" Counting ends in " + EndValue)))); } } } } |
Using FOR loop in C# – Working with steps
- Step 1: You have to declare Counter, StartValue, EndValue as integer
- Step 2: Set StartValue as 1 (here you set the value where counter starts)
- Step 3: Set EndValue as 10 (Here you set the value where the loop stops)
- Step 4: Assign the StartValue to the counter and starts loop until the counter reaches the EndValue.
- Step 5: Executing the loop body, till the counter reaches end and finally loops out.
If you have any suggestions or doubts regarding FOR Loop in C#, please contact us our Mail ID: Codingangel@gmail.com
Using FOR Loop in C#







