Hello!
Today we will be going to learn about an, in my opinion, essential part of the Objective-C language. Imagine writing code to do something thousands of times. Well, loops take care of that. Those thousands of lines of code will be reduce to maybe three. There are four different kinds of loops: for loops, nested for loops, while loops, and do...while loops. So before we go into each loop in more detail, loops are very convenient and efficient. Really take the time to understand them because truly, this will save you tons of time.
Today we will be going to learn about an, in my opinion, essential part of the Objective-C language. Imagine writing code to do something thousands of times. Well, loops take care of that. Those thousands of lines of code will be reduce to maybe three. There are four different kinds of loops: for loops, nested for loops, while loops, and do...while loops. So before we go into each loop in more detail, loops are very convenient and efficient. Really take the time to understand them because truly, this will save you tons of time.
For Loops
Now open up the starter project and navigate to the Loops.m file. There you will find five methods. Go to the init method. Uncomment the first method call to the forLoopMethod. In the forLoopMethod write your first for loop like this:
Let's go through this. So for loops start with the keyword for. Then you add parentheses that will hold the initialization, condition, and increment. So the initial value, when you are going to stop the loop, and how you are going to increment the value by. Usually when writing for loops the initialization will always start with int i = a number. In this case you declare and give i the variable a value of 0. Then you put a semicolon. Then you write the condition. In this next line you are saying run this for loop as long as i < 6. Then you put a semicolon. Then you put the increment. So each time Xcode's compiler goes through the loop it will subtract 1 from i in this case. Let's look at this closer though. If i starts out as 0 and each time the loop runs 1 is subtracted from i, then will i ever be greater than 6? No! This means that the loop will run forever or infinitely! If a loop is an infinite loop than this will produce a bug that will crash your app. So let's fix that. Instead of subtracting one. Let's add one. Here is how your new for loop should look.
Now run your app. The output area should like this.
Nested For Loops
Now navigate to the nested for loops method. Cut and paste the for loop from the for loop method to the nested for loop method. But delete everything between the curly braces of the loop. Now create another for loop that will only run 4 times inside the other for loop like this:
Have it output "This is a nested for loop."
Run the app! Count how many lines of output you have! 20! Why? Well for each time the outer for loop is run the inside for loop is run 4 times. So 5 X 4 = 20!
While Loops
Now delete all of the code inside the nested for loop method. Now navigate to the while loop method. Write your first while loop now. Like this:
If you run the app, "This is a while loop" should be outputted 5 times. The while loop has the same concept as the for loop. You declare a variable. Then you have the while keyword. Then in the parentheses you put a condition. Then the code inside is run that many times. The only difference is that the increment goes inside of the curly braces. Be careful though. Make sure you have an increment because if you don't then you might have an infinite while loop.
Do...While Loops
Do...while loops are not that different than while loops. The only difference is that you are guaranteed that the code will be run at least once. Here this will help you understand. Delete the while loop you just wrote so that method is empty. Now write this do...while loop.
Because of how the code is read (top to bottom) the stuff inside of the curly braces after the keyword do is run at least once. This is because the compiler doesn't check until after the do block of code is run. Change the less than sign to a greater than sign and run your code. Even though 1 is not greater than 5 the code is still run at least once.
Well, that was some very important content we just learned! Great job! Now you will be extremely clean, efficient, and smart when you write code! In the next post we will make your code think by making decisions with if statements and switch statements! Don't forget to leave a comment, suggestion, or comment on this page or email me at [email protected].
Thank you,
Zachary Cmiel
The PoKoBros
Well, that was some very important content we just learned! Great job! Now you will be extremely clean, efficient, and smart when you write code! In the next post we will make your code think by making decisions with if statements and switch statements! Don't forget to leave a comment, suggestion, or comment on this page or email me at [email protected].
Thank you,
Zachary Cmiel
The PoKoBros