C# Console - 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 running 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. In this example, the loop will repeat as long as the value of the variable i is less than or equal to 100.

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.

int i;

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

{

    Console.WriteLine(i.ToString());

}

Console.ReadKey();

In the example above, numbers between 1 and 100 are printed one after the other. 

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

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

Console.WriteLine(i.ToString());          

}

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++) Console.WriteLine(i.ToString());

Check out the Subject Examples section.

 
C# console for loop tutorials, for loop examples and explanations

EXERCISES

For Loop Example: Program that prompts the user for numbers 10 times and calculates the sum of these numbers
int total = 0;
 
for (long i = 1; i <= 10; i++)
{
Console.Write( i.ToString() + ". number: ");
int a = int.Parse(Console.ReadLine());
total += a;
}
 
Console.WriteLine("Total: " + total.ToString() );
 
Console.ReadKey();

 

For Loop Example: Program that asks the user for numbers 10 times and finds how many are even
int x = 0;
 
for (long i = 1; i <= 10; i++)
{
Console.Write( i.ToString() + ". number: ");
int a = int.Parse(Console.ReadLine());
    if (a % 2 == 0) x++;
}
 
Console.WriteLine(x.ToString() + " of them are even");
 
Console.ReadKey();

 

For Loop Example: Program to find whether a given number is prime or not

Let's divide the entered number from 2 to all numbers minus 1 and look at the remainder. Let's increase the state variable by 1 when the remainder is 0. If the state variable is still 0 after the loop is finished, the number is prime because it has not been divided by any number during the loop.

Console.Write("Number: ");
int a = int.Parse(Console.ReadLine());
 
long state = 0;
 
for (long i = 2; i < a; i++)
{
    if (a % i == 0) state++;
}
 
if (state> 0) Console.WriteLine("Number Not Prime");
else Console.WriteLine("Number is Prime");
 
Console.ReadKey();

 

Using if Statement in a For Loop

To print the numbers divisible by 5 between the two entered numbers, we look at the remainder of the current variable i divided by 5, using if in the for loop. If the remainder is 0, we print the number on the screen, and if the remainder is 0, we do nothing.

Console.Write("Number1: ");
int a = int.Parse(Console.ReadLine());
 
Console.Write("Number2: ");
int b = int.Parse(Console.ReadLine());
 
for (int i = a; i <= b; i++)
{
    if(i % 5 == 0)
    Console.WriteLine(i.ToString());
}
 
Console.ReadKey();

 

For Loop Example: Program to calculate factorial of entered number
Console.Write("Number: ");
int a = int.Parse(Console.ReadLine());
 
int carpim = 1;
 
for (int i = 1; i <= a; i++)
{
    x= x* i;
}
 
Console.WriteLine(x.ToString());
Console.ReadKey();

 

For Loop Example: Adding all numbers between two entered numbers
Console.Write("Number1: ");
int a = int.Parse(Console.ReadLine());
 
Console.Write("Number2: ");
int b = int.Parse(Console.ReadLine());
 
int x= 0;
 
for (int i = a; i <= b; i++)
{
    x= x+ i;
}
 
Console.WriteLine(x.ToString());
Console.ReadKey();

 

For Loop Example: Write the numbers 1 to 100 to the screen by skipping 2
for (int i = 1; i <= 100; i+=2)
{
    Console.WriteLine(i.ToString());
}
 
Console.ReadKey();
For Loop Tutorials
The program that prints all the numbers between the two entered numbers to the screen:
 
Console.Write("First number: ");
int a = int.Parse(Console.ReadLine());
 
Console.Write("Second Number: ");
int b = int.Parse(Console.ReadLine());
 
for (int i = a; i <= b; i++)
{
    Console.WriteLine(i.ToString());
}
 
Console.ReadKey();

 



COMMENTS




Read 568 times.

Online Users: 1247



for-loop-in-c-sharp-console