C-SHARP - 3.12 Loop For and Foreach

We now find ourselves on Visual Studio to put into practice the notion of For and Foreach loop. Let's start with the For loop. The difference between the for loop and the While loop is that instead of initializing a variable before the loop and incrementing the variable which allows the loop to run inside the braces this time we put everything in the parenths its from the for loopHere to create the for loop, I'm going to delete all that and I'm going to type For then in the parentheses, I'm going to create a variable giving its type is int then I give it a name I'm going resume counter and I will initialize it directly to 0. Then I put a ; and I pass the condition I use the same as that of while as long as counter is less than the total number of values ​​in the array then you loop. Either counter less than tab_prenom.Length then; and the I pass al incrementation, I put here counter++. You see that it is + on writing that the while loop and the for loop will differ. Then there are some cases where you will use the while loop more than the for loop, but that's still on a case-by-case basis. Then we open the braces like the while loop. And we put our instructions there. Here it was to display the values ​​of our array so we made our Console. WriteLine() and we put our array in parentheses with the index in square brackets using the counter variable to iterate through the array. And if we run the program, we have the same result as the while loop. This time, with the for loop, we have a clearer, simpler code, and we have less risk of falling into an infinite loop since we must directly increment our counter in the parentheses. Now let's move on to the Foreach loop which is a little different, it will not work with an index system in the same way as the for loop and the While loop. It is written this way: Foreach parentheses then we create a variable of the same type of the things we want to return it is first names so a string then we give it a name for example a First name then you add in which means in which we want to loop and here it is in the table so fill in just after our table tab_prenom We open the braces and we put our Console.WriteLine() except that here between the parentheses, we will no longer put our table but the variable unPrenom. Why ? Here roughly, the loop will loop over each element of the array and at each turn of the loop, the variable unPrenom will be assigned to the element to which the loop is roughly located in round 1 unPrenom will take the 1st value of the array. In round 2, it will take the 2 me value etc. So since the variable unPrenom is equivalent to a value of the table at each round then it is it that we will put in the parentheses. And if we launch the program, we can see that we have all our first names displayed. That's all for this video. All these notions of loops are very easy to understand, if you have not understood certain points do not hesitate to watch the videos on loops again. And in the next video I will give you an exercise to consolidate and perfectly master these notions of loops. In any case, I hope this video has been enjoyed and that everything is clear for you. I'll tell you right away for the exercise!