For Loop

For Loop

The for loop ensures that the specified operations are repeated as long as the desired condition is met.

A variable is used to specify the initial value and condition. In the example below, a variable named "i" is used. The name we give to the variable is entirely up to us, but programmers usually prefer variable names like "i, j, k".

Let's explain the for loop by examining the following example:

The loop starts with a for statement and consists of three parts in parentheses separated by semicolons:

With i=1, the initial value of the variable is specified.

In the second part, the operating condition of the loop is specified. The specified condition is checked each time and if it holds, the loop continues to run. If the condition is not met, the loop ends and the program continues from the lines after the loop. 

With i++, the value of the variable i is increased by 1 every time the loop returns. The amount of increase or decrease depends on the programmer. For example, if we wrote i-=5 here, the variable i would decrease by 5 at each step of the loop.

In the example below, the loop will repeat as long as the value of the variable i is less than or equal to 100. Since the variable i is initially 1 and is incremented by 1 each time, the loop will run a total of 100 times. What is written inside the loop will also be done 100 times.

int i;

for (i = 1; i <= 100; i++)
{

label1.Text += i.ToString() + "\n";            

}

In the example above, the numbers between 1 and 100 are written one below the other in label1. 

The variable i can also be defined in the start line of the loop.

for (int i = 1; i <= 100; i++)
{

label1.Text += i.ToString() + "\n";            

}

In addition, if the operations to be performed in the loop consist of a single line, curly brackets may not be used.

for (int i = 1 ; i <=100; i++) label1.Text += i.ToString() + "\n";

Check out the Subject Examples section.

 

for loop tutorials, c# for loop, using for loop, loop examples, loops, c# examples of operations with loops

EXERCISES

Adding numbers to ComboBox using for loop

We want to add numbers from 1900 to 2019 in a ComboBox so that the user can select their birth year. We do not add these numbers by manually typing them one by one. The for loop will make our job easier:

for(int i=1900;i<=2019;i++)
{
comboBox1.Items.Add(i);
}

 

For Loop Tutorials 6

for döngüsü örnekleri

The program that generates 50 random numbers and adds them to ListBox1 when Button1 is clicked, and writes the sum of all numbers in ListBox1 to Label1 when Button2 is clicked:

private void button1_Click(object sender, EventArgs e)
{
label1.Text = "";
listBox1.Items.Clear();
 
int s;
 
Random rnd= new Random();
 
for (int i = 1; i <= 50; i++)
{
s= rnd.Next(0, 1000);
listBox1.Items.Add(s);
}
 
}
 
private void button2_Click(object sender, EventArgs e)
{
int sum= 0;
int s;
 
for (int i = 0; i <= 49; i++)
{
s= Convert.ToInt32(listBox1.Items[i]);
sum= sum + s;
}
label1.Text = sum.ToString();
}

 

For Loop Tutorials 5

Write the program that displays the multiplication table of the number selected from the combobox in label1. In the picture above, 5 is selected from the combobox. There are numbers from 1 to 10 in the combobox.

int c;
int x= int.Parse(comboBox1.Text);

for(int i=1;i<=10;i++)
{
      c = i * x;
      label2.Text += x+" x " + i + " = " + c +"\n";
}

 

For Loop Tutorials 4

The user will enter numbers in two text boxes and make a selection from the combobox. All numbers between the entered numbers will be written in the label. However, "BOM" will be written for multiples of the number selected from the combobox.

label1.Text = "";
int a = int.Parse(textBox1.Text);
int b = int.Parse(textBox2.Text);
int z= int.Parse(comboBox1.Text);

for (int i = a; i <= b; i++)
{
     if (i % z == 0) label1.Text += "BOM\n";
     else label1.Text += i.ToString() + "\n";
}

 

For Loop Tutorials 3

Between the numbers entered in the two text boxes, make the program that writes the numbers matching the condition in the selected radiobutton into the label.

int a = int.Parse(textBox1.Text);
int b = int.Parse(textBox2.Text);
int x=1;

if (radioButton1.Checked) x= 5;
else if (radioButton2.Checked) x= 8;
else if (radioButton3.Checked) x= 11;

for (int i = a; i <= b; i++)
if (i % x== 0) label1.Text += i.ToString()+"\n";

 

For Loop Tutorials 2
  1. Write a program that takes 10 numbers from the user with InputBox and calculates the average of odd and even numbers separately.

  2. Make a program that writes the squares of all numbers starting from 1 to the number entered from the keyboard, one under the other, into the label.

For Loop Tutorials 1

1. A program that prints numbers from one hundred to two in labels one after the other.

for( int=1; i<=100 ; i+=2)
label1.Text += i.ToString() + "\n";

2. Program to calculate the sum of numbers from 1 to 100

int x= 0;
 
for (int i = 1; i <= 500; i++)
{
   x= x+ i;
}

label1.Text = x.ToString();

3. The program that adds all the numbers between the numbers entered by the user in the two text boxes (If the user enters a larger number in the first box, it will act accordingly)

int a = int.Parse(textBox1.Text);
int b = int.Parse(textBox2.Text);
int x=0;
 
if(a>b)
{
    for(int i=b;i<=a;i++)
    {
    x= x+ i;
    }
}
else
{
    for (int i = a; i <= b; i++)
    {
        x= x+ i;
    }
}
 
label2.Text = x.ToString();

4. The program that calculates the factorial of the number entered in the textbox

int a = int.Parse(textBox1.Text);
int z= 1;

for (int i = 1; i <= a; i++)
{
      z= z* i;
}

label1.Text = z.ToString();

5. A program that prints numbers from 1 to 100 to the screen at intervals equal to the number entered in the textbox.

int a = int.Parse(textBox1.Text);
 
for (int i = 1; i <= 100; i = i + a)
    label1.Text += i.ToString() + "\n";

6. Program that prints all numbers between two numbers received from the user to the screen

int a = int.Parse(textBox1.Text);
int b = int.Parse(textBox2.Text);
 
for (int i = a; i <= b; i++)
      label1.Text += i.ToString() + "\n";

7. A program that prints the numbers from one to one hundred, divisible by 5, to the screen.

8. A program that prints the numbers from one to one hundred divided by the number entered in the textbox.

9. Program that prints numbers divisible by 5 between the numbers entered in two text boxes.

int a = int.Parse(textBox1.Text);
int b = int.Parse(textBox2.Text);
 
for (int i = a; i <= b; i++)
{
    if (i % 5 == 0) label1.Text += i.ToString() + "\n";
}

10. A program that prints numbers divisible by both 8 and 9 between the numbers entered in two text boxes.

11. Program to calculate how many numbers are divisible by 4 between numbers entered in two text boxes

int a = int.Parse(textBox1.Text);
int b = int.Parse(textBox2.Text);
int c= 0;

for (int i = a; i <= b; i++)
{
    if (i % 4 == 0) c++;
}

label1.Text = c.ToString();

12. Program that finds whether the number entered in the text box is prime or not. 

long a = long.Parse(textBox1.Text);
long d=0;
 
for (long i = 2; i < a; i++)
{
    if (a % i == 0) d++;
}
 
if (d> 0) label1.Text = "Prime number";
else label1.Text = "Not prime";

13. Program that calculates combinations of numbers entered in two text boxes

14. Program that calculates permutations with numbers entered in two text boxes



COMMENTS




Read 851 times.

Online Users: 3788



for-loop-tutorials