Your SlideShare is downloading. ×
Conditionals
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Conditionals

1,145
views

Published on

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

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,145
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
46
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Conditionals forks in the road Tuesday, September 22, 2009
  • 2. Tuesday, September 22, 2009
  • 3. Tuesday, September 22, 2009
  • 4. Tuesday, September 22, 2009
  • 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. if Tuesday, September 22, 2009
  • 7. if (conditional) Tuesday, September 22, 2009
  • 8. if (conditional) { Tuesday, September 22, 2009
  • 9. if (conditional) { // statement to be executed Tuesday, September 22, 2009
  • 10. if (conditional) { // statement to be executed } Tuesday, September 22, 2009
  • 11. false true Tuesday, September 22, 2009
  • 12. var age = 60; var status = null; if (age > 62) { status = 'retired'; } status; // null Tuesday, September 22, 2009
  • 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. var age = 65; var status = null; if (age > 62) { status = 'retired'; } status; // "retired" Tuesday, September 22, 2009
  • 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. 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. if (conditional) { // statement if true } Tuesday, September 22, 2009
  • 18. if (conditional) { // statement if true } else Tuesday, September 22, 2009
  • 19. if (conditional) { // statement if true } else { Tuesday, September 22, 2009
  • 20. if (conditional) { // statement if true } else { // statement if false Tuesday, September 22, 2009
  • 21. if (conditional) { // statement if true } else { // statement if false } Tuesday, September 22, 2009
  • 22. false true Tuesday, September 22, 2009
  • 23. var age = 60; var status = null; if (age > 62) { status = 'retired'; } else { status = 'not retired'; } status; // "not retired" Tuesday, September 22, 2009
  • 24. var age = 65; var status = null; if (age > 62) { status = 'retired'; } else { status = 'not retired'; } status; // "retired" Tuesday, September 22, 2009
  • 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. 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. 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. 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. switch statement select one of many blocks of code to execute http://www.w3schools.com/JS/js_switch.asp Tuesday, September 22, 2009
  • 30. switch Tuesday, September 22, 2009
  • 31. switch (value) Tuesday, September 22, 2009
  • 32. switch (value) { Tuesday, September 22, 2009
  • 33. switch (value) { case 1: Tuesday, September 22, 2009
  • 34. switch (value) { case 1: // some code to execute Tuesday, September 22, 2009
  • 35. switch (value) { case 1: // some code to execute break; Tuesday, September 22, 2009
  • 36. switch (value) { case 1: // some code to execute break; case 2: // some code to execute break; Tuesday, September 22, 2009
  • 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. 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. 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. 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. 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. Review Tuesday, September 22, 2009
  • 43. var age = 18; var teens = false; if (age > 12 && age < 20) { teens = true; } teens; Tuesday, September 22, 2009
  • 44. var age = 18; var teens = false; if (age > 12 && age < 20) { teens = true; } teens; // true Tuesday, September 22, 2009
  • 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. 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. 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. 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. Assignment http://teaching.johnnunemaker.com/capp-30550/sessions/conditionals Tuesday, September 22, 2009