Java Basics

Java Basics 04: Operators if, else, and and or

14 videos

80 minutes

Now let's see logical operators, we will see how, If, which is a very useful sentence that helps to regulate the flow, which will work out the program, ie how we will control, in what order, instructions are executed and if we want, some run only in certain situations while others may not run when something is not happening, so we can control, what instructions, if executed and which not,

conditioning with, If, so I'll show you how we do this, for example we will create us a variable, as we have seen, it is a number and let's say that is of value 6, for example, and here we will do the following, we will show a message on screen that says, System.out.println, as we have seen, read: 'the number is greater than 5', ok! Then this message, if I compile and threw will always run, it will always say that the

number is greater than 5, because we are not putting any conditions here simply is the order that is getting instructions, see? then a variable, and then I say the number is greater than five, but only want that to be said, when really the number is greater than five, if I put here, equal to 3 number three is not greater than five, but if I throw also tells me, is greater than five, then how do I control this? It's simple, here

put, If, open parenthesis (), and within the parentheses have to put the condition I want to be fulfilled, for the following run instructions, What instructions? Which are within the braces {}, you see it here open keys? For these statements inside keys, I'll copy this and I'll put it here, this is only run when the condition is met in here, what condition I put?

For that number, whichever is greater,> which 5 then there yes, if I now released, does nothing, you see that does not put anything? Because this condition is not met, so So, do not access this line and do not realize it, because this condition must be fulfilled, otherwise we would put here, Else, and again we open keys, {, And will run only what is inside the keys, so we can put here, 'the number is less than five,' so that when I grow up, tell us

greater, and when I say not more less, less or equal, and it makes me throw 'the number is less than or equal to five,' if I say that the number is 5, what will happen? It will ask here, is greater than 5, 5 is greater than five, no! Five is not greater fives, then you go to, Else, and the following is does, will see! Launch and I get that number is greater than or equal to 5, well, these instructions, we're seeing here, what comes to return a, If, when we

this condition is a value, Boolean is a true or false, so we saw earlier in this video, is that variables such data, so here in Actually, that is to say, is whether this is true, if this is true, and if it is not, because it is false, it becomes the other, why it is so important to know manage Boolean logic, I explain the following in these, If. You can put as they are, more conditions, not just one, for example we

say you want them also to be greater than 5, you want to be more than 10, How do you do that? Well, you put the two conditions and this for two ampersand '&&', you are saying that the two conditions have to be met, the number is greater than 5 and less than 10, have to be both, if not given the two, it is not done in this case, we will put here to be a 7, the two conditions are met, right? When I throw, I will say this

sentence 'is greater than 5 and less than 10' right? I throw here and give it to me, but if one of the two, is not met, no longer applies, for example, let's say, that has to be well below 7, and let's say that here is an 8, change this, Else, 'the number does not meet the conditions' and will see now, as the number does not meet the conditions, because it fails, the two conditions, that'm getting here, when it is, If they have to fulfill all, either one or the

another, but if I want only, with one meets and longer sufficient, we, Or, that is, with these two vertical poles, 'II' are already saying if it is so, if it is one thing, as if it is another, no matter what, but that one is fulfilled, it is sufficient if one is true, and if I give and there are, as if it comes, then, remember this, with, And, with two ampersand have to be the two conditions, binding the two, with, Or only with a will

give, is enough to enter to execute instructions that are within If either as we have seen, logical operators, the, And, and the Or, will continue to see most basic concepts of Java, on these videos.

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.