‫Welcome back.
‫Before we get started with anything else, I have a little challenge for you, and that is to take this
‫for loop here.
‫This nested for loop and adjust it so that our console right here or this for loop in general will not
‫print out all elements of our matrix, but only the ones that are odd numbers.
‫So only the two to 4 to 6 and the eight.
‫So please adjust the code so that it does it.
‫And just a little hint you will need to use if statements as well as the modulo operator.
‫Okay.
‫So I hope you passed the video and you tried it for yourself to get this running and to make the code
‫do exactly as we like it to do.
‫And what we all need is an if statement the checks the matrix at the i j position for modulo two because
‫if something is modulo two zero, then it is going to be an even number.
‫So what this basically does is it compares if the modulo remainder when you divide something by two
‫is going to be zero.
‫So if the remainder is zero, then we know we have an even number.
‫Okay, so basically that's going to be it.
‫So if this is the case, then print the number and otherwise else.
‫We could also add a statement here where it's just console, right?
‫Like soul and just an empty space, nothing more.
‫Okay.
‫So this will now print it out.
‫And print specifically the even numbers two, four, six, eight.
‫So that's what I meant with.
‫We all need an F statement as well as a modulo operator.
‫So this is the modulo operator and it works with remainders.
‫Now, by the way, if one is the result here, so if the remainder is one, then we know that we have
‫an odd number.
‫So let's run it again.
‫And you see, we only get the odd numbers from our matrix.
‫So this was just a little challenge for you to get the juices flowing.
‫So now let's look at how we can print the diagonal elements of a matrix.
‫So one, five and nine, for example.
‫Or seven, five and three.
‫Or the other way around, actually.
‫Three, five, seven.
‫So how can we actually achieve that?
‫So if we look at our nested for loop that we created here, then we realize that here I is zero and
‫J is zero.
‫If we look at I and J at this case, I is one and J is one, and in this case I is two and J is two.
‫So if we want to have a diagonal, we know that I and J have to be the same.
‫So at least for this diagonal here, four one, five and nine, four, seven, five and three, it's
‫going to be a different story.
‫But for the one five at nine, we can basically do something very simple, and that is to just check
‫if the matrix.
‫Well, actually a challenge for you.
‫Try.
‫Try it for yourself.
‫Okay.
‫So I hope you checked it.
‫So what?
‫We need to check this.
‫Is I equal J.
‫And if that's the case, then print out the value.
‫Otherwise, print out an empty space.
‫So let's run that real quick.
‫And we will see that we get one, five and nine.
‫I now I want this to look a little better.
‫What we actually also can do is we can use a control bright line statement with an empty space and then
‫it would look like this.
‫So one, five, nine for aesthetic reasons.
‫So that's something that we can achieve here.
‫You can, of course, create a significantly more complex logic.
‫So what you could do is you could replace the diagonal values.
‫Now, for example, instead of just printing them out, you could assign them.
‫So you could assign a different value for them saying something like.
‫All of them should be a one, for example.
‫So that could be something that you could do.
‫And then let's actually do the for each statement underneath it.
‫Let's run this again.
‫And you will see that now it will be one, two, three, four, one, six, seven, eight, one.
‫Okay, so basically replaced this five and this nine with a one.
‫So now this is just an example of how you can do it using a nested for loop.
‫The thing is, you can actually do it using just one for loop.
‫It's a little easier, of course, and a little challenge for you.
‫Try to do it with just one for loop.
‫Okay.
‫So basically it's just going to be this here.
‫So we just use this for loop where it goes through the matrix get length, so through the column length,
‫so to speak and it just prints out I.
‫So basically saying one or in this case, zero zero.
‫In this case one one.
‫In this case, two, two.
‫So this will be the position of the numbers that I just showed you.
‫Okay.
‫If we run this, we should also get one, five, nine.
‫Let's look at it and we can see we get 159.
‫Okay, now what if we want to get three, five and seven?
‫So now I would like you to pause the video and find a logic that will print out three, five, seven
‫instead.
‫Okay.
‫This will be a little more complicated because we now need to think where we need to start our eye and
‫where we need to start.
‫RJ Because basically what we need to do is our eye still starts at the beginning and goes up by one.
‫So we go through one row at a time.
‫But RJ.
‫He's going to go down from the maximum value in this case from two.
‫Okay.
‫So.
‫Okay.
‫So and now I would like to show you a little trick how you can go down or basically show the diagonal
‫signal from the other way around.
‫So from three, five and seven, and therefore you can use something where a four loop is going to be.
‫Having to counter variables.
‫So usually let's look at a for loop.
‫We can just enter four and then press tab twice.
‫This is how a four loop usually looks like.
‫Right?
‫So you have a counter variable ie and then you have the length and in our case the length will be predefined
‫is three because we will hardcoded this time.
‫We know that we will just have three lines here.
‫Of course you could also say matrix get length like so matrix get length.
‫For the zeroth dimension.
‫And now what you also can do, by the way, is you can add a comma here and say that J should start
‫as two and then you decrement J.
‫For each iteration.
‫Like so.
‫So now what we have is we have to counter variables.
‫So we have the eye counter available and at the same time the j count the variable, but we are only
‫limiting based on the eye value.
‫So this will only work properly for a matrix that has the same amount of columns as it has amount of
‫rows, because otherwise you would have to use a nested for loop as we've done before.
‫So this will not work and we can just go ahead and write the matrix position I and J to our console.
‫So let's look at it and we can see it shows three, five and seven.
‫So this time it went from top right to bottom left.
‫But this is just an example of what you can do with a follow up.
‫This is not very common practice, so be careful when you use it.
‫This is a little more difficult to read and this is also something that you should consider when developing.
‫So if you use it in a simple example, it's going to be probably fine.
‫But if the example becomes more complex, then this can become quite confusing, or it could potentially
‫become quite confusing when double checking your application, or when a third developer or a second
‫developer comes in and checks your code.
‫So always take that into consideration when writing your code.
‫Make it as straightforward as possible so that it stays readable.