use of the for loop in c #

1

Hi, I'm learning c # (I already have some experience in python)

I'm learning about the for loops and I really do not see much use for them if I already have the while loop, I know that the while is indeterminate and the for is determined but the while I can do it easily with an accountant, which for me leaves without utulity to the

Can someone explain to me what this loop is about?

something else in python the for if it has more use since I can use it to scroll words or lists letter by letter

    
asked by Erick Toledo 11.10.2018 в 03:35
source

3 answers

4

First of all a loop for is a C ++ inheritance, and it works in the same way as in this language.

The most common use of a for is when you want to iterate a known number of times and in turn have access to the counter, for example;

var numerosPares = new List<int>();

for(var c = 0; c < 10; c++)
    numerosPares.Add(c * 2);

Compare the two previous lines with the equivalent using while

var numerosPares = new List<int>();

var c = 0;
while (c < 10)
    numerosPares.Add(c++ * 2);

So far with the for we save a line, apart from clearly separating the part where the counter increases with the part where the value is calculated.

We have a more complicated example with a for / while nested

The following code:

var coordenadas = new List<(int,int)>();

for (var i = 0; i < 10; i++)
    for (var j = 0; j < 10; j++)
        coordenadas.Add((i,j));

It should be written as follows using while

var coordenadas = new List<(int, int)>();

var i = 0;
while(i < 10)
{
    var j = 0;
    while (j < 10)
    {
        coordenadas.Add((i, j++));
    }
    i++;
}

As you can see, the number of lines is increasing. In the case of the while it happens if you forget to increment i or if you increase it in an incorrect place, or if you forget to initialize j to 0 in each round of i . There is also an asymmetry: j can be increased with the postfix increment operator: ++ however i must necessarily be increased at the end of while on a separate line.

Compare it to the for syntax that is much clearer.

    
answered by 11.10.2018 / 03:59
source
0

Let's start from the basis that there is always more than one way of doing things.

Well then having this clear you can iterate using for or while as you like and better apply in your code, but remember that sometimes it is more verbose to use a certain instruction.

For example, when you iterate a known number of values, the for defines a scope for the variances avoiding that by mistake it is modified outside of this

for(int i=0; i<10; i++){
   Console.Write(i);
}

in this case the variable i will only be accessible within the scope of the for once it leaves this will not be accessible

int i=0;
while(i<10){
   Console.Write(i);
   i++;
}

is the same, of course, but I change the scope of the variable, which can be accessed once the while

ends

By rule when you iterate a known number of values you use the for you need to define a more complex condition you use the while , for example

string salir = "";

do{
   //codigo
   Console.WriteLine("Desea salir? yes/no :")
   salir = Console.ReadLine();
}while(salir != "yes")

there if it is clear that you should use the while , since under that condition the for does not apply

In c # you can also iterate items from string with foreach

string cadena = "texto de prueba";
foreach(char letra in cadena){
   //codigo
}

but actually it is used more with lists and collections

    
answered by 11.10.2018 в 03:58
-1

In C # the difference is clear and consists in the way in which the condition is processed: FOR is executed UNTIL the condition is true and WHILE is executed WHILE the condition is true.

The point is that the two are loops is to say data or whatever you want a certain number of times and this point in common is what makes the cycles are in an equal point of view but with different syntax.

I tell you that with the for loop you can go through the letters of a phrase you just have to do a split before.

    
answered by 11.10.2018 в 03:55