14 videos
80 minutes
In this video we will see another operator conditions, that helps us when we want, do many queries on the same data, rather than having to put many, If, consecutive and condition, If, and condition, If, and condition, we can save all that, using, Switch, I'll show you an example, For example, let's imagine here we have a variable where we store a month for example say that is the month, No. 3, would be March, now we want
show how many days this month, good but instead of asking, If, month = 1 has 31 days, If, month = 2 has 28 days, If, month = 3, 31 days, and so the 12 months would be something very tedious, and would have to write a lot of code, all that can be summed up with one thing that is, Switch, and here, Switch, we what we see, we see the month as we, Switch, month, and here we will put all the different cases, that would be,
for example, if it is 1, if it is 2 if is 3, there would put them all without having to put 12, If, but what happens? I'm going to put here an example, so you see what happens when we do this here, if for example, here, I want all these do the same thing, here at the very end, I put. System.out.println, and says, 'has 31 days', then I'll do next, I'll put here, so in this way, for all
I put these, case 1, as would be the value of the month and then two points, two points case 3, case 5 colons, for all this would do the same thing, you will see! Launch, and come and tell me: 'It is three months this is fulfilled and executes this,' and if I put it here, 'month is 5, the same thing happens.' He tells me he has 31 days, perfect! And if any are not met the conditions I have to put here, after the end of all, it is default, which is something like like, Else,
of, If, if otherwise, because if you do not give, lest any of the conditions that I put above, default, I have to put I want it done in that part, so I put System.out.println here, and here say, 'he does not have 31 days,' for example if I tell here April 1 and launch, say, 'do not have 31 days,' there you are, right? Well, but, notice that there is a problem, remember that in the If, and Else, were running all
instructions were inside the keys, in this case we have not put keys, What does this mean? If for example, the compiler when they start execute the instructions, I say 3 passes through here, and this runs, but continuous, and this also runs another, Go to see! .. I throw, and see? Me says the two things, when it is 3, he tells me he has and then goes and tells me no, why? What shape because I say, as far as is
to run? in the case of, If, and, Else, was simple, to what is inside the keys, 'but ... there are no keys' here only the key It is, Switch, but everything in there, no key, how do I do then? i write, Break, and say that when you run this as complete and out, they will see as now, only tells me what I have to say, and here, and does, break, and exit, and from there not continuous, you see? ... well,
here, of course, would have to put all other cases, I put 1, 3, 5, only, but many more missing, missing the rest of months, I have done so to sum it up, but there understand ... with Switch, saved many, If, remember put, case, value and colon, and then, Break, when they want there is no longer execute instructions and if not is none of the above, then put the, default, ok! ... because in following videos
we will move forward with more concepts of Java.
About the Author
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.