Hey there,Gamers and Game Makers! Welcome back to the introduction to C# series. In this, the final post of introduction tutorials, we're going to take a look at loops. So, we use loops when we want to execute a section of code multiple times as long as it continues to meet a certain condition. For this example, I'm going to use our script from the Arrays tutorial as seen below. Ok, I want to print the contents of my array but I don't want to write a print command for each one as that's just time consuming and just not good practice for coding. Since I know the size of my array, I can use a For Loop to run through the array and print out each name up until it reaches the last index of the array. To do this I need a variable called index to keep track of the position of the array. Now, I don't have to declare this new variable up with my other variables. I can simply declare it within my for loop and initialize it in the loop also. It'll still work if I declare the index variable as I normally do with my other variables. The syntax of my loop would change slightly. the syntax for my for loop is as follows: I start off my for loop simply by typing the word "for" followed by a set of brackets. Inside the brackets, I declare my index variable and assign it a value of zero. I then set the for loops condition which in this case is index < 4. This means, as long as the index value is less than 4, the for loop will continue to execute. I follow this with index ++. This is our exit condition. This increments the value of index as long as it is still less than 4. Once the value of index reaches 4, the for loop knows it should no longer execute. Inside the body of our for loop, we can put what ever code we want to run. It's very much like how we did it for our If Statements. Since we want to print each name in the array without using multiple print commands, what we do is type our print command once and inside the brackets, type the name of our array followed by square brackets. Inside the square brackets we type our variable index. What this does is print the name that corresponds to the index position that is currently the value of the index variable. As you can see, when we run the script all the names of the array get printed even though we only have one print command. Ok, let's take a look at a While Loop. A while loop works similarly to that of a for loop but we can use a while loop in cases where we don't know the number of times we need it to run. Now, I'll still use the array example so we do actually know the number of times it's going to run. The syntax for a while loop is as follows: As you can see with the while loop, I've declared my index variable as I normally would at the top and inside my while loop I've simply put the loops condition which is index less than 4. In the main body of the loop I still have my print command with the array name and index variable inside the print command but I've moved my exit condition down into the body of the loop. This means the index will increment after each printout until it reaches 4 at which point the loop will know not to execute anymore. Once again, when we run the script we can see all the names of the array have been printed out using just the one print statement. Loops are a great way of running multiple calculations without having to clutter your script with excess code.
That does it for this short introduction to C# series. I hope you've found it helpful and I plan to start another series soon that will expand upon this and dive into making more complex and fun projects. Until next time! |
Archives
April 2019
Categories |