Advertisement
Guest User

Untitled

a guest
Dec 8th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. /*
  2.  
  3. Control Flow
  4.  
  5. Flow Control is important because it allows your program to follow a strict flow using if, if-else, or if-else-if-else statements. This shows how code will react or flow if a certain condition or conditions is or is not met.
  6.  
  7.  
  8. If Statement:
  9.  
  10. if (condition){
  11. Execute code if statement is true
  12. }
  13.  
  14.  
  15. If-Else Statement:
  16.  
  17. if (condition){
  18. Execute code if statement is true
  19. }
  20.  
  21. else{
  22. Execute code if statement is false
  23. }
  24.  
  25.  
  26. If-Else If Statement:
  27.  
  28. if (condition){
  29. Execute code if statement is true
  30. }
  31.  
  32. else if (condition){
  33. Execute code if statement is true instead
  34. }
  35.  
  36. else{
  37. Execute code if all statements are false
  38. }
  39.  
  40. */
  41.  
  42. function isRich(money) {
  43. if (money > 500.00) {//first condition
  44. return "I'm rich!";
  45. } else if (money < 500.00 && money > 100.00) { //second condition
  46. return "Get a job."
  47. } else { //default condition
  48. return "It's over."
  49. }
  50. }
  51.  
  52. console.log(isRich(32.00)); //prints "It's over." Does not meet first two coniditions.
  53.  
  54. /*
  55.  
  56. Switch Statements:
  57.  
  58. You can also use Switch Statements to control the flow of code when many routes depend on one condition.
  59.  
  60.  
  61. switch (expression)
  62. {
  63. case condition 1: statement(s)
  64. break;
  65.  
  66. case condition 2: statement(s)
  67. break;
  68. ...
  69.  
  70. case condition n: statement(s)
  71. break;
  72.  
  73. default: statement(s)
  74. }
  75. */
  76.  
  77. var Car = 'Subaru';
  78. switch (Car) {
  79. case 'Toyota':
  80. case 'Subaru':
  81. case 'Chevy':
  82. console.log('This car is within budget.');
  83. break;
  84. case 'Lexus':
  85. default:
  86. console.log('This car is outside of budget.');
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement