Loop for C#
Blog > C# > Loop for C#

Loop for C#

PL

Loop for C#


The loops (Loop for C#) together with the conditional instructions are the most important elements in programming. They allow you to repeatedly execute the same block of code, you can also use them to access all elements of a given set. The following entry describes the for loop with examples of its uses.


Contents


Syntax of for loops


The for loop needs a variable whose value will be checked with the final condition. Such variable can be declared earlier or already in the loop. In case of declarations in the loop, the range of visibility of the variable is limited to this loop. In the scope of the loop, you can use the declared variable just like any other contained in the program.
Loop for C#


Declaration of for loops requires three elements, they are:


  • Initial variable value – performed only once, before the first start of the loop.
  • Condition terminating the loop – the most frequently used comparison is the value of the declared variable with the value at which we would like to terminate the execution of the loop.
  • The operation is performed after each iteration of the loop – usually increasing or decreasing the value of the variable. The loop termination condition is checked after this operation.

In code, it looks like this:


//Example 1 - the loop will be executed 5 times, the value of the variable and will be greater by 1 in each iteration.
for( int i = 0; i < 5; i++)
{
//Repeated program code
}

//Example 2 - changed conditions and values. For one line of code in the loop, no clamp is required
for( int i = 8; i > 5; i--)
//1 program code line repeated

//Example 3 - declaration of variable outside the loop
int x;
for( x = 0; x < 10; x++)
//1 program code line repeated

Application of for loops


The for loop, due to its construction, is very well suited for checking collections that have a certain number of elements. Examples of such lists are tables. The following code presents the declaration of the array and supplementing its elements with values equal to the array indexes. The condition for making the loop is the value of the variable smaller than the size of the table. Arrays are indexed from 0, so array indexes are in the range of integers <0, Length – 1>. This condition does not allow to go beyond the size of the table.


int[] tablica1 = new int[8];

for (int i=0; i < tablica1.Length; i++)
{
     tablica1[i] = i;
}

You can display all elements of an array in a similar way


for (int i = 0; i < tablica1.Length; i++)
{
Console.WriteLine(tablica1[i]);
}

Use the for loop to perform actions on the values of the set. The calculation of the average value from the elements of the previously presented supplemented table is presented below.


double wartoscSrednia = 0; //it is necessary to initialize the value, because the values from the table will be added to it
for (int i = 0; i < tablica1.Length; i++)
{
     wartoscSrednia += tablica1[i];//casting is not necessary because the int value is completely double
}
wartoscSrednia /= tablica1.Length;//the sum of values is divided by the number of elements

For loop and multidimensional arrays


The for loop perfectly deals with one-dimensional arrays, but it can also be used for multidimensional arrays. Obtaining access to all elements of the table requires using a separate loop for each dimension of the table. The following example illustrates the completion of array elements with values that are the product of the row and column index of a given element.


int[,] tablica2 = new int[3, 5]; 

//GetLength() returns the dimension size of an array whose index is given in brackets as a parameter.
for (int i = 0; i < tablica2.GetLength(0); i++) 
{
    for (int j = 0; j < tablica2.GetLength(1); j++)
    {
        tablica2[i,j] = i * j;
    }
}

Above placed loops in the loop. This works in such a way that the value of the variable “i” assumes a certain value and operations for all available values of the variable “j” are performed for it. After completing all operations, the variable “i” takes the next value. The activity is repeated until all elements are reviewed. In the example shown, the variable “and” represents the next rows of the table, while the variable “j” of the column.


Reading all elements of a two-dimensional array looks exactly the same:


for (int i = 0; i < tablica2.GetLength(0); i++)
{
   for (int j = 0; j < tablica2.GetLength(1); j++)
   {
      Console.WriteLine(tablica2[i,j]);
   }
}

Loop for a while loop (Loop for C#)


The for loop (Loop for C#) is not the only possible loop, and the action it performs can be successfully replaced with another solution. The choice of a loop depends on the current solution that seems more convenient. The following example shows two loops that perform exactly the same operations.
Loop for C#


//Pętla for
for (int i = 0; i < 10; i++)
{
   //Repeated program code
}

//Loop while
int i = 0;
while(i < 10)
{
   //Repeated program code i++;
}

×
Any questions?
TOP