Hello everyone, welcome to the sixth lesson in my Java programming for beginners tutorials series. The fifth lesson(lesson - 4) - Switch Statements can be found at - https://steemit.com/programming/@robertlyon/java-programming-for-beginners-lesson-4-switch-statements
In this lesson, I am going to cover For loops, their basic syntax and how they can be used in programs. I will talk briefly about what for loops are, move onto some code examples and then we can look through the code line by line to understand what is happening. This lesson requires some knowledge from the previous lessons so if you have not completed those yet, then I recommend you go back and have a look at them.
In your code, you are going to want to repeat things, for example, if you have a game, you will want to have a loop that keeps running the game until the player wishes to exit. A more simple example is a program that prints the numbers 1 - 100. Now you could achieve this by using 100 System.out.println() statements but that is extremely tedious. Luckily there is a better method for achieving this. For loops perform an action a set number of times, the for loop is set to run as many times as you want it to. Let us have a look at the for loop below and we can dissect the main syntax of the loop.
public class ForLoops {
public static void main(String[] args)
{
int loopNumber = 100;
for(int i = 0; i < loopNumber; i++)
{
System.out.println(i);
}
}
}
So we can see from the image above that this program prints the numbers 0 - 99, 1 number on each line(the picture only goes to 23 to save space but the program actually printed up to 99).
Let us look at the syntax of the program. Make sure to look at the line numbers on the image as that will be my reference for the line on code that I am analyzing.
On line number 4 I create a variable, you should be able to understand variable assignment by now. The variable loopNumber holds the value 100, this is the number of times that the loop is going to run.
Line 6 is where the loop actually begins
for(int i = 0; i < loopNumber; i++)
the first word on this line is "for". This is a Java keyword which marks the start of a for loop. Without this keyword, the Java compiler would have no idea that you wanted to execute a for loop.
The part inside the parentheses are the requirements of the loop. The first part int i = 0 initializes the loop counter "i" to 0. "i" is the variable which will keep track of how many times the loop has gone around.
The middle part of the inside of the parentheses i < loopNumber. This is the condition for the loop. Each time the loop goes around the program checks this condition. If the condition is true the loop will run again, if it is false then the loop will end and the program will carry on with its next task.
The last part inside the parentheses is "i++". This is how the loop is incremented. Each time the loop goes around the loop counter("i") will be incremented by 1, this keeps happening until i is equal to 100. At this point, i is no longer less than 100 and the loop will end.
A few things to note about the loop setup is that the loopCounter "i" can be any variable you wish although "i" is the standard variable that most people use.
Remember that if your loop counter starts at 0 then you have to start counting from 0, so if you want to loop 100 times, that would be from 0 - 99.
The last thing is that the loop increment can go up in any increment you choose, to achieve this you could use a statement such as i = i + 5.
Looking to the body of the loop between the curly braces you can see a simple print statement. In the parentheses of this statement, you can see the loop counter variable "i". Any statement that is in the loop body is executed each time the loop goes around. This means that "i" is incremented each time and then sent to the print statement, printing out the number 0 - 99.
You should have a go at playing around with for loops and make sure that you are comfortable with the syntax, in the next lesson I will be looking at a different kind of loop so you should spend some time making sure you know what for loops are.
In this lesson, I have covered the basic syntax of a for loop and give a few examples of what they may be used for. We looked through the syntax line by line and talked about how to set up a for loop and what each piece of code in the loop does. You should now be able to set up your own for loops.
In the next lesson, I will be covering repetition and will be focusing on while loops.
If there is anything in this lesson that confuses you or there is anything programming related that you need help with then please comment below and I will try my best to help you.
As always if there are any improvements you think I can make to this post then please leave a comment and I will consider adding it.
Thank you for reading and I hope that someone will get some use out of these tutorials.
Message to readers
Thanks for taking the time to read my post, if you are interested in Science, Technology or Computer Science then check out my blog, content is a little sparse at the moment but I am making an effort to provide good quality original content to the Steemit community.