YouTube

Got a YouTube account?

New: enable viewer-created translations and captions on your YouTube channel!

English subtitles

← The If Statement - Intro to Java Programming

Get Embed Code
6 Languages

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

  1. Hello, and welcome to this new lessons. In this lesson you will learn how your
  2. programs can make decisions. Let's look at an example. Here you have a typical
  3. elevator panel, and yes elevators are controlled by computers these days. And
  4. if you look at the buttons, you will notice that there are buttons for 10, 11,
  5. 12 and then goes on with 14. so, there's no button for 13. and that's of course
  6. because, many people are superstitious and don't want to be in the 13th floor
  7. of a building. Now, contrary to popular belief, the 13th floor is not filled
  8. with secret government offices. There actually is a 13th floor, they just call
  9. it the 14th. So, the software that controls the elevator car needs to adjust
  10. for this human foible. Here's how you can do that in Java. Let's say floor is
  11. the number as provided by the user, 10, 11, 12, 14, 15, 16 or whatever, and now
  12. we want to compute that actual floor. Then we need to make a decision. In Java
  13. you use the if statement to make decisions, and you enclose the condition in
  14. parenthesis. Then you use a pair of braces, and inside you put the statement or
  15. statements that you want to have executed when this condition is true. So, if
  16. the floor is greater than 13, then we want it to adjust by subtracting 1. On
  17. the other hand, if the condition is false, then we'll carry out the actions in
  18. the else part of this statement. In which case, the actual floor number equals
  19. the floor number without any adjustment. And that's all there is to it. Let's
  20. go over the important parts again. The keyword if starts out the conditional
  21. statement. The condition goes in here, and is surrounded by parentheses. If the
  22. condition is true, then the part following the if is executed. And if the
  23. condition is false, then the part after the else clause is executed. Alright,
  24. now it's your turn. In some Asian countries, it's not the number 13 that brings
  25. bad luck, but the number 14. And those elevators wouldn't have a number 14. And
  26. in fact, just to play it safe, some property owners eliminate both the 13 and
  27. 14. So, go ahead and modify the program in the next exercise so that it
  28. translates the user selected number to the actual floor number. And know, there
  29. are no hidden floors with secret government offices.