English subtitles

← And Or Not - Intro to Java Programming

Get Embed Code
5 Languages

Showing Revision 5 created 05/24/2016 by Udacity Robot.

  1. Many times, when you have complicated conditions, you want to combine them. For
  2. example, maybe you want to do something if one condition is true, or another
  3. one. In Java, there are three operators that you can use to combine conditions.
  4. They look like this. The two ampersands are an and these two vertical bars are
  5. on or. And the exclamation mark somewhat is a not. You might find it useful to
  6. remember that the not equal operator that you've seen a few units ago also has
  7. the exclamation mark. Let me show you a few examples for using these operators.
  8. And once again, we'll be looking at flags where the visual appearance should
  9. help you. Let's start with this maritime flag for the letter d. Here, I made a
  10. sketch of this flag. When you're in the middle, the pixel should be blue. And
  11. otherwise, it should be yellow. So how do we express when you're in the middle?
  12. When the y-value is greater than this level. And less than this level, that's
  13. where our and comes in, then we're in the middle. This here is 1 5th of the
  14. total height. So, we have two conditions, namely y is at least height over 5,
  15. and the other condition is y is at most height times 4 over 5. Both conditions
  16. have to be fufilled for us to be in the middle, so we use the and operator.
  17. Here it is. Let's just try this out. Here is the condition that you just saw
  18. with the and operator here. And in this case, we return blue. Otherwise, we
  19. return yellow. In this case, I will make a square flag and here it is. So, the
  20. and operator worked out for us. Because we wanted y to be at least here at
  21. most, here. Both conditions had to be true for the blue color. Now, let's look
  22. at another flag. I've sketched up the flag here. And the question is, when
  23. should a pixel be blue. In this case, there are four conditions. The x-value
  24. should be at least width over third. That means it's over here. The x-value
  25. should be at most, width times two thirds. That makes it over here. The y-value
  26. should be the height over 3, and the y-value should be at most the height times
  27. two third. All those four conditions need to be fulfilled for a pixel to be
  28. built. Now, it's your turn. You'll get to write a program that draws this flag
  29. here with a bit of blue on the inside and white on the outside.