C-SHARP - 3.11 Loop (demo)
We now find ourselves on Visual Studio in order to put into practice the notion of loop in C#. Before we begin, there are different types of loops in C#, there is the While loop, the For loop and the Foreach loop. These different loops will allow us to do more or less the same thing. We will therefore start with the While loop. And we will see in another video the For and Foreach loop First of all we will create an array which will contain for example a list of first names and thanks to the While loop we will display them one by one So you remember to create a table is simple, you give the type here it's first names so string then brackets you give a name to this array for example tab_first name Then = new to create an instance of the Array class Then we put the String type hook and there we open our braces To define the values from our array. I am going to put 3 4 first names in this table, so quotes Jibril then comma, quotes Jason comma quotes Thibaut comma and to finish quotes Ismael and do not forget them; after the braces Now we are going to create a variable which will serve as a counter in our loop but also to browse the array because you remember, each value of an array has an index. The 1st value has an index of 0, the second has an index of 1 etc. And since the counter evolves at each iteration of the loop, we can recover each value of the array by putting the value of the counter as an index. It's perhaps a little vague saying like that but you will understand right away. So it's a counter, so the variable will be of type int, we're going to call it counter you can call it what you want in general the developers call it i but here we're going to call it counter just to see more clearly And we're going to initialize it a 0 Then to create a while loop, you type while then parentheses and between these parentheses you put the condition our condition here is that as long as the counter is less than the number of values that there is in the table then you will display each value and a Once you have reached this condition then the loop will stop. How do you write this in C#? You type counter the sign < tab which is the name of the table from which we want to retrieve the values And you put a point.Length Why did I add point Length? Because here we are doing an operation between a number and an array And this is not possible to make this kind of comparison, we have to compare a number with number, don't we? here the point will allow us to call a function on our array and then we put the Length function to retrieve the number of values in our array. Which means that now we compare a number with a number, if I translate this condition it is as if we did As long as 0 is less than 4 because counter is equivalent to zero and the number of values in the table is equivalent to 4 Then we open the braces and there we will put our instructions which were: that we wanted to display each value of the array. So we do a Console.WriteLine() and inside the parentheses, we're going to put our array. If we do this, will it work? Nope ! Why ? Because here we want to display what our array is worth, while we want to display each value of our array and what is and what to do to display a value from an array if you remember ? You have to use square brackets and put the index of a value inside. Here we want to display each array value at each iteration of the loop either we cannot put for example the index of the value 1 of the array or 0 C is index does not evolve during the loop it will therefore display Jibril at each time it will be blocked on one and the same value. So how could we succeed in increasing the index put between the brackets and succeed in displaying each value of the array? Well simply by putting the counter variable between the brackets. At the beginning of the counter program is = 0 and as the loop progresses it will be incremented by 1 to reach the condition placed between the parentheses of the while Let the index also be between the brackets and will therefore display each value corresponding to its index until the end of the loop. If I now run the program, we can see that all the first names in the table are displayed. Last very important thing it is an error which can happen very often by carelessness when you are going to program it is to forget the increment of your counter inside the loop to reach the condition put between the brackets? ?ses of the while. This will cause what is called an infinite loop. Basically, if you forget it and run your program here, we have a counter which is = zero and the condition is that as long as it is less than the number of values in the array, i.e. 4, then you execute the instructions. Thus, since we never increment our counter, it will never reach The condition will always be true and we will execute the instructions to infinity. So pay attention in the future to avoid errors and bugs. That's all for this video I hope you liked it and that everything was clear to you See you in the next video to see other types of loop, the For and Foreach loop I tell you everything right now !