Conditionals

1,552 views

Published on

Conditional statements in JavaScript (if, if...else, if...else if...else, and switch).

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,552
On SlideShare
0
From Embeds
0
Number of Embeds
278
Actions
Shares
0
Downloads
47
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Conditionals

  1. 1. Conditionals forks in the road Tuesday, September 22, 2009
  2. 2. Tuesday, September 22, 2009
  3. 3. Tuesday, September 22, 2009
  4. 4. Tuesday, September 22, 2009
  5. 5. if statement execute some code only if a statement is true http://www.w3schools.com/JS/js_if_else.asp Tuesday, September 22, 2009
  6. 6. if Tuesday, September 22, 2009
  7. 7. if (conditional) Tuesday, September 22, 2009
  8. 8. if (conditional) { Tuesday, September 22, 2009
  9. 9. if (conditional) { // statement to be executed Tuesday, September 22, 2009
  10. 10. if (conditional) { // statement to be executed } Tuesday, September 22, 2009
  11. 11. false true Tuesday, September 22, 2009
  12. 12. var age = 60; var status = null; if (age > 62) { status = 'retired'; } status; // null Tuesday, September 22, 2009
  13. 13. var age = 60; var status = null; if (age > 62) { 60 > 62 === false status = 'retired'; this never executes } status; // null so status is never assigned and remains null Tuesday, September 22, 2009
  14. 14. var age = 65; var status = null; if (age > 62) { status = 'retired'; } status; // "retired" Tuesday, September 22, 2009
  15. 15. var age = 65; var status = null; if (age > 62) { 65 > 62 === true status = 'retired'; this executes } status; // "retired" so status is set to “retired” Tuesday, September 22, 2009
  16. 16. if...else statement execute some code if statement is true and some other code if statement is false http://www.w3schools.com/JS/js_if_else.asp Tuesday, September 22, 2009
  17. 17. if (conditional) { // statement if true } Tuesday, September 22, 2009
  18. 18. if (conditional) { // statement if true } else Tuesday, September 22, 2009
  19. 19. if (conditional) { // statement if true } else { Tuesday, September 22, 2009
  20. 20. if (conditional) { // statement if true } else { // statement if false Tuesday, September 22, 2009
  21. 21. if (conditional) { // statement if true } else { // statement if false } Tuesday, September 22, 2009
  22. 22. false true Tuesday, September 22, 2009
  23. 23. var age = 60; var status = null; if (age > 62) { status = 'retired'; } else { status = 'not retired'; } status; // "not retired" Tuesday, September 22, 2009
  24. 24. var age = 65; var status = null; if (age > 62) { status = 'retired'; } else { status = 'not retired'; } status; // "retired" Tuesday, September 22, 2009
  25. 25. if...else if...else statement execute some code if statement is true and some other code if statement is false http://www.w3schools.com/JS/js_if_else.asp Tuesday, September 22, 2009
  26. 26. if (conditional) { // statement if true } else if (conditional) { // statement if false and else if true } else { // statement if all false } Tuesday, September 22, 2009
  27. 27. if (conditional) { // statement if true } else if (conditional) { // whichever matches first } else if (conditional) { // whichever matches first } else if (conditional) { // whichever matches first } else { // statement if all false } Tuesday, September 22, 2009
  28. 28. var age = 19; var status = null; if (age >= 62) { status = 'retired'; } else if (age > 22 && age < 62) { status = 'working'; } else if (age >= 18 && age <= 22) { status = 'college'; } else { status = 'no responsibilities'; } status; Tuesday, September 22, 2009
  29. 29. switch statement select one of many blocks of code to execute http://www.w3schools.com/JS/js_switch.asp Tuesday, September 22, 2009
  30. 30. switch Tuesday, September 22, 2009
  31. 31. switch (value) Tuesday, September 22, 2009
  32. 32. switch (value) { Tuesday, September 22, 2009
  33. 33. switch (value) { case 1: Tuesday, September 22, 2009
  34. 34. switch (value) { case 1: // some code to execute Tuesday, September 22, 2009
  35. 35. switch (value) { case 1: // some code to execute break; Tuesday, September 22, 2009
  36. 36. switch (value) { case 1: // some code to execute break; case 2: // some code to execute break; Tuesday, September 22, 2009
  37. 37. switch (value) { case 1: // some code to execute break; case 2: // some code to execute break; default: // nothing matched so do this Tuesday, September 22, 2009
  38. 38. switch (value) { case 1: // some code to execute break; case 2: // some code to execute break; similar to else in if...else default: // nothing matched so do this Tuesday, September 22, 2009
  39. 39. switch (value) { case 1: // some code to execute break; case 2: // some code to execute break; default: // nothing matched so do this } Tuesday, September 22, 2009
  40. 40. var age = 18; switch (typeof(age)) { case 'number': "I'm a number!"; break; case 'string': "I'm a string!"; break; case 'boolean': "I'm a boolean!"; break; default: "I don't know what I am!"; } // "I'm a number!" Tuesday, September 22, 2009
  41. 41. for(action in json) { var selectors = json[action]; switch(action) { case 'replace' : for(selector in selectors) $(selector).html(selectors[selector]); break; case 'append' : for(selector in selectors) $(selector).append(selectors[selector]); break; case 'prepend' : for(selector in selectors) $(selector).prepend(selectors[selector]); break; case 'replaceWith' : for(selector in selectors) $(selector).replaceWith(selectors[selector]); break; case 'insertBefore' : for(selector in selectors) $(selectors[selector]).insertBefore($(selector)); break; case 'sidebar' : Sidebar.add(selectors); break; case 'remove' : $(selectors.join(',')).remove(); break; } } Tuesday, September 22, 2009
  42. 42. Review Tuesday, September 22, 2009
  43. 43. var age = 18; var teens = false; if (age > 12 && age < 20) { teens = true; } teens; Tuesday, September 22, 2009
  44. 44. var age = 18; var teens = false; if (age > 12 && age < 20) { teens = true; } teens; // true Tuesday, September 22, 2009
  45. 45. var days = ['sunday', 'monday', 'tuesday']; var first = days[0]; var week_starts_on_sunday = false; if (first === 'sunday') { week_starts_on_sunday = true; } week_starts_on_sunday; Tuesday, September 22, 2009
  46. 46. var days = ['sunday', 'monday', 'tuesday']; var first = days[0]; var week_starts_on_sunday = false; if (first === 'sunday') { week_starts_on_sunday = true; } week_starts_on_sunday; // true Tuesday, September 22, 2009
  47. 47. var days = ['monday', 'tuesday', 'sunday']; var first = days[0]; var week_starts_on_sunday = false; if (first === 'sunday') { week_starts_on_sunday = true; } week_starts_on_sunday; Tuesday, September 22, 2009
  48. 48. var days = ['monday', 'tuesday', 'sunday']; var first = days[0]; var week_starts_on_sunday = false; if (first === 'sunday') { week_starts_on_sunday = true; } week_starts_on_sunday; // false Tuesday, September 22, 2009
  49. 49. Assignment http://teaching.johnnunemaker.com/capp-30550/sessions/conditionals Tuesday, September 22, 2009

×