English subtitles

← Evaluating Statements - Programming Languages

Get Embed Code
3 Languages

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

  1. So let's go over the answer to this
  2. or 1 possible answer together.
  3. We're trying to write a program to interpret
  4. if-then-else statements,
  5. and the big concept here about control flow is
  6. that we don't want to execute both the then branch
  7. and also the else branch.
  8. Instead, we'll have to pick one.
  9. So the first think I'm going to do is evaluate
  10. the conditional expression in the same
  11. environment in the same set of evaluations
  12. to variables that we currently have.
  13. If it comes out true, I'll want to execute the then statements.
  14. If it comes out false, I'll want to execute the else statements,
  15. and if you like, this if statement is
  16. the same as one where I checked to see if
  17. the result of evaluating the expression is equal to true,
  18. but if you have a little experience with propositional logic,
  19. that's actually the same as just checking
  20. the value of this variable directly.
  21. So if the conditional expression evaluates to true,
  22. I will evaluate all of the statements in the then branch.
  23. In our running example, that was A and B.
  24. In our current environment there haven't been any intervening
  25. assignment statements so the value of the variables hasn't changed.
  26. Otherwise, if the value of the conditional
  27. expression wasn't true,
  28. presumably it was false, we'll go over here
  29. to the else branch, evaluate the else statements
  30. in the same environment,
  31. and note that no matter what happens,
  32. we're either evaluating the then statements
  33. or the else statements, but not both,
  34. and that decision is made based on the
  35. value of the conditional expression, and that's it.
  36. These 4 lines suffice to evaluate
  37. an if-then-else statement in java script.