Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Conditionals

 Conditionals

John Nunemaker

September 22, 2009
Tweet

More Decks by John Nunemaker

Other Decks in Programming

Transcript

  1. if statement execute some code only if a statement is

    true http://www.w3schools.com/JS/js_if_else.asp Tuesday, September 22, 2009
  2. var age = 60; var status = null; if (age

    > 62) { status = 'retired'; } status; // null Tuesday, September 22, 2009
  3. var age = 60; var status = null; if (age

    > 62) { status = 'retired'; } status; // null 60 > 62 === false this never executes so status is never assigned and remains null Tuesday, September 22, 2009
  4. var age = 65; var status = null; if (age

    > 62) { status = 'retired'; } status; // "retired" Tuesday, September 22, 2009
  5. var age = 65; var status = null; if (age

    > 62) { status = 'retired'; } status; // "retired" 65 > 62 === true this executes so status is set to “retired” Tuesday, September 22, 2009
  6. 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
  7. if (conditional) { // statement if true } else {

    // statement if false Tuesday, September 22, 2009
  8. if (conditional) { // statement if true } else {

    // statement if false } Tuesday, September 22, 2009
  9. var age = 60; var status = null; if (age

    > 62) { status = 'retired'; } else { status = 'not retired'; } status; // "not retired" Tuesday, September 22, 2009
  10. var age = 65; var status = null; if (age

    > 62) { status = 'retired'; } else { status = 'not retired'; } status; // "retired" Tuesday, September 22, 2009
  11. 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
  12. if (conditional) { // statement if true } else if

    (conditional) { // statement if false and else if true } else { // statement if all false } Tuesday, September 22, 2009
  13. 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
  14. 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
  15. switch statement select one of many blocks of code to

    execute http://www.w3schools.com/JS/js_switch.asp Tuesday, September 22, 2009
  16. switch (value) { case 1: // some code to execute

    break; Tuesday, September 22, 2009
  17. switch (value) { case 1: // some code to execute

    break; case 2: // some code to execute break; Tuesday, September 22, 2009
  18. 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
  19. switch (value) { case 1: // some code to execute

    break; case 2: // some code to execute break; default: // nothing matched so do this similar to else in if...else Tuesday, September 22, 2009
  20. 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
  21. 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
  22. 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
  23. var age = 18; var teens = false; if (age

    > 12 && age < 20) { teens = true; } teens; Tuesday, September 22, 2009
  24. var age = 18; var teens = false; if (age

    > 12 && age < 20) { teens = true; } teens; // true Tuesday, September 22, 2009
  25. 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
  26. 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
  27. 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
  28. 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