C-SHARP - 3.10 Loop

Hello everyone and welcome to this new video. In this video, we will cover the notion of loop in C#. First, before getting to the heart of the matter, we will define what a programming loop is. . A loop in programming is an instruction that repeats itself until a specified condition is met. Basically, it will allow you to repeat one or more actions several times as long as the condition you gave is not reached. For example to illustrate this notion of loop. Let's take a character who has a starting point and an ending point. This character has a function that allows him to move forward one step at a time We will call this function Advance() Now our character must take 20 steps to reach the end point. We must therefore repeat the Advance() function 20 times to reach its end point Can you imagine the number of lines of code that this will require? We can't afford to do that because if our character has to take 100 steps, we're going to write Advance 100 times in our code? This is where loops will play a big role. We will tell our character character that As long as you have not reached the condition then you will move forward and once you have reached this condition then you stop. We increase the number of steps at each turn of the loop to reach our condition. The program will therefore loop each time and repeat the same instructions. And each turn of the ball is called an iteration. This program will therefore have 20 iterations. On the other hand there, we will not have to write dozens of lines but only a few. And once the 20 step condition is met then the program stops. To summarize here, what happened, it uses exactly the same principle as the conditions except that here we will reiterate several times, here we look if the number of steps is less than or equal to 20, it is lower c true so we enter the loop then we execute the instructions and we increment the number of steps by 1, incrementing means adding by 1 Then we start again the number of steps is now worth 2 Is it still less than 20 or equal? Yes, so we start again like this until we reach 20. At the 20th iteration, the step value is equal to 20. So it will advance by 1, we enter the condition. Then the number of steps will be incremented so it will be 21, the program will perform a test 21 is less than or equal to 20. No c false then the program is finished, we no longer enter the loop condition. Here we imagine ourselves in a game but that also applies to a website from which we want for example to recover the list of all the orders made by a customer here we will in particular use a loop for the displayed ones, you see roughly? Loops are part of the basics of programming and will be constantly present in your life as a developer. It is important that you understand this notion of a loop. In any case, know that it is very simple, it is not complicated at all. We are going to go to Visual Studio to put this notion of a loop into practice in C#. I'll tell you right away!