Java Basics

Java Basics 09: nested loops (for)

14 videos

80 minutes

Well, once you are seen as one, for loop works, we will see another step when nested loop, what does this mean? Poke a loop within a loop, ie this loop for let's say you are inside another loop, why? Because we want to traverse an array, so let's go touring the two sides of this matrix we had before, now we are going to create us the matrix, so here I say that it will make a matrix of five by two

why? Well, first, we'll have to go through each one of the cars in its two data, both the number of seats as the number people who are occupying it, so here, for this to stay closer, let's put it as follows, we will say so, and here we will say: Seats I do not know if they get to see it. When I'm over the video. I dont know! and on the other hand, 'passengers' 'seats'. Here I put 'passengers' ok !, well.

Well, the next thing is now we have to go through the two loops, one we said that ranges from 1 to 4, so this loop, which will do is, I to put this so to be available to see this loop, which will do is go through each one of the rows, we now have to do is another that loops through each one of the columns, so, well, I need another variable to iterate the next loop, so I put it here for the loop and say that this loop with the 'j'

that will be the next iterator, you will be starting at zero, how long? In this case, we have to go, these columns, so it will be, while 'j' is 'less than or equal to 1' ok! And what we do it is that 'j' increase to 'j ++' to increase every time we open keys, and in here is where we put all this code, ok! Therein, we put it. Now what we will do is this, let's say that indeed the first car

It has 10 seats, the second 20, third 30, but let's say they are occupied by half, ok! Each car has half of occupation, then this First thing first we have to save this coordinate: '[i] [j]' here will keep the number of seats, it is no longer the 'i' only, it is not a network, if we are not to give the two data, because we work with a matrix, two-dimensional, so you have to indicate the two

coordinates, so in the 'i j' when 'j' is zero, we will be working seats and when 'j' is 1 in the next round, work number passengers, so let's put here, that for the 'j' zero we here, If, 'j = 0' for then we say, I gave it. And if, j = 1, then in here let's say that this is equal to half of the passengers, how we say half? For cars ... here it would be zero, all this by 2. Ok! You understand it?....

if you do not understand, then more times Look at him! But it is very simple, when 'j' is zero, what we do is record the number of seats we have said and when 'j' is 1 then we say that has half the seats, and the seats are stored in the zero position, What does this do? It starts with the front row, and crosses the zero and one, which are the columns, are the 'jots' then advances to row one, crosses the zero and one, and then proceeds to two, walking

each of the two, with this loop, go around the columns, and with this loop, loops through the rows, ok! Let's see how it would be if I threw it, I run to see ... let me check what's going on here ... here gives me this error ... ah! I already know that! ... This error something is wrong here ... ++ ... ah! Already! What it is what is ! Wrong ... wrong here this System.out.println got what when with Arrays and now here is a ... is a matrix, then here; we will tell you this,

Here this we will delete it, then what we see now, we take another loop, here in the first loop, we will be putting data in the second loop, System.out.println, say how much you have, so I put it, I put it here, we'll ... well, look what I'll do here, all in one and thus saving me this, all one, and we will see, as you have to get out: 10, 5; 20,10 half; 30, 15, here I have to exit 40 then 20 and 50 ... 25, the number of seats and

half. With nested loops, arrays can go both to give them courage to read and display on the screen ... we will now see other Loop, which is not for, but and do while While, in the following video.

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.