Java programming for beginners - Lesson 5 - For LoopssteemCreated with Sketch.

in #programming7 years ago

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.

For Loops

What are they?

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.

Structure of a for 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.

Conclusion

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.

Next Lesson

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.

Sort:  

Great Job!

Its refreshing to see that some one else is also doing programming tutorials on steem. I'm working through a rust tutorial, but Java is obviously important as well. This is a fairly comprehensive tutorial as well which is really impressive.

Yeah, I thought there would be a lot more programming and development related posts on Steemit. Thanks for your input and keep up the good work on your tutorials.

Really nice tutorial. You running a nice series for learners. Java has become one of the most used languages after the advent of Android operating system.

Thanks for this helpful series. Also check out utopian.io if your interested in development.

Thanks you for your input, I appreciate it.

Resteemed.

(Posts will be resteemed 24 hours after the posting because there is a chance that the posts will be discovered by curators. If it is resteemed before 24 hours of it's posting, and the payout exceeds $1, then usually, curators will pass on it).

Nice work @robertlyon.

Loops are important for repetitive tasks. Similar to how multiplication is in comparison to addition.

1 + 1 + 1 + 1 + 1 can be summarized by 1 X 5.

They are also used in the forms and security checks of computer and websites. Without loop, computer and information technology is nothing.

Thanks, Yeah loops are one of the fundamentals, without loops(or recursion), source code would be tediously long and annoying to write.

I am also a Programmer in the field of WEB Development/Engineering and android development. Loops takes very importance in my life like food.

Haha. Nice, you planning to do any programming/development related posts?

Awesome, I have followed you, look forward to reading your posts :)

Thanks @robertlyon.
Actually I have big interest in technology based posts rather than programming related posts. But i will be post on programming.

@steem-marketing has voted on behalf of @minnowpond.
If you would like to recieve upvotes from minnowponds team on all your posts, simply FOLLOW @minnowpond.

            To receive an BiggerUpvote send 0.5 SBD to @minnowpond with your posts url as the memo
            To receive an BiggerUpvote and a reSteem send 1.25SBD to @minnowpond with your posts url as the memo
            To receive an upvote send 0.25 SBD to @minnowpond with your posts url as the memo
            To receive an reSteem send 0.75 SBD to @minnowpond with your posts url as the memo
            To receive an upvote and a reSteem send 1.00SBD to @minnowpond with your posts url as the memo

Awesome post friend.
I like your all content because your content type and quality is so good.
best of luck go ahead friend.

Thanks

Awesome post friend.
I like your all content because your content type and quality is so good.
best of luck go ahead friend.

This post was promoted with @monitorcap traffic bot & STEEM promotion service.

Send MIN. $1 SBD to @monitorcap bot with your link in MEMO field
and recieve upvotes & resteems for your posts. @monitorcap - where 'seen' matters !

Coin Marketplace

STEEM 0.27
TRX 0.11
JST 0.030
BTC 69061.75
ETH 3774.04
USDT 1.00
SBD 3.51