Java Basics

Java Basics 08: loop

14 videos

80 minutes

As we begin to see loops in Java. The first thing we see is the for loop, which is a very powerful loop and practically most widely used compared to the while, do-while. So this for loop, I'll explain using this matrix we have seen above as an example, we are going to do first with An ARRAY and then see how nested loops ok! So look! Let's imagine that this is an Array, I'll put here only; that we may see,

there no more; It has only one dimension. And now we want, let's say we want to give that Array, each wagon; let's say you have ten times the number of passengers of the car, that is, in the car 1 has 10 passengers, the car 2 has 20; the three has 30, the four has 40 and five 50. What does this mean? We have to go from 1 to 5 and we have to scroll through those numbers How do we make that journey? Because instead of putting

Here wagons 1 wagon 2 cars 3 cars 4 and repeat the same formula, five times create a for loop; with this loop, we make that journey; so we here for and now we travel with a variable that is the iterator that iterates that is crossing it, let's call it 'i'. We go from zero, we started at zero, this variable has to be declared, then we can declare here put: int i, you see? There is! 'Int i' and now in the

For loop has three parts: the first part is where we say as the loop begins; the second part, which says even when the loop has to go spinning, what condition must be fulfilled so that the instructions will put here then repeat. So, this has to be while 'i' is less than

matrices, are only with this now; so, we started the 'i' to zero and the 'i' through four, and each time 'i' has to increase in value; first need be zero and then has to be 1, and then has to be 2 and each time 'i' will be one more. How do we do that? We can do this as follows: we can say 'i' is = to have + 1 or this is summarized as follows: 'i' + + to increase 'i' a value ok! And now we put our keys and everything

that is within the braces is what is to be repeated. Begins 'i' it being worth zero, runs what we write here and then increase 'i', and check 'i' is less or equal to 4; then if returns are true, it increases 'i' and rechecked 'i' remains less than or equal to 4, if satisfied run here! When this is not met for loop ends and continues for here are the instructions below. In this case, we will not only put instructions let

explain loop. What would we do here? Here we can say is that wagons in the position of the iterator, it will be = a; I have 10, because we have said that the first car will have a 10 and the second 20, third a 30. In this case, since 'i' will start being zero and we will indicate the position of the car, not the number of the car. This is what is going to put 'i + 1', in that way, so do there, what we do is that the first car

have a 10, the second 20 and this has to display on screen, let's put a screen message that says: 'the wagon' and here we 'i + 1', has, and many passengers 'and here we: wagons [i] ok! Let's see how this would work, if I compile this what I have to walk the entire loop, for all wagons. Let's see the result we have there, the car 1 has 10; the two is 20; three, has 30; four, 40 and five, 50.

We have been traveling and while we recorded shows on screen. Well, this would be the standard for loop in the next video we will see how to nest a for loop within a loop.

To find new courses, subscribe. There will be no spam, promised :)

About the Author

foto de jotajotavm
José Javier Villena

Biography: Analyst-Programmer in different languages. Tutor PREMIUM platforms recognized worldwide as CodigoFacilito. Editor Cristalab articles. My YouTube Channel is sponsored by the ANAYA and LaTostadora publisher. I like to explain in detail and give several examples for the avoidance of doubt.