Conditionals
Upcoming SlideShare
Loading in...5
×
 

Conditionals

on

  • 1,572 views

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).

Statistics

Views

Total Views
1,572
Views on SlideShare
1,296
Embed Views
276

Actions

Likes
0
Downloads
40
Comments
0

6 Embeds 276

http://teaching.johnnunemaker.com 270
http://www.ustudy.in 2
http://www.slideshare.net 1
http://webcache.googleusercontent.com 1
http://teaching.harmonyapp.com 1
http://127.0.0.1:8000 1

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Conditionals Conditionals Presentation Transcript

  • Conditionals forks in the road Tuesday, September 22, 2009
  • Tuesday, September 22, 2009
  • Tuesday, September 22, 2009
  • Tuesday, September 22, 2009
  • if statement execute some code only if a statement is true http://www.w3schools.com/JS/js_if_else.asp Tuesday, September 22, 2009
  • if Tuesday, September 22, 2009
  • if (conditional) Tuesday, September 22, 2009
  • if (conditional) { Tuesday, September 22, 2009
  • if (conditional) { // statement to be executed Tuesday, September 22, 2009
  • if (conditional) { // statement to be executed } Tuesday, September 22, 2009
  • false true Tuesday, September 22, 2009
  • var age = 60; var status = null; if (age > 62) { status = 'retired'; } status; // null Tuesday, September 22, 2009
  • 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
  • var age = 65; var status = null; if (age > 62) { status = 'retired'; } status; // "retired" Tuesday, September 22, 2009
  • 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
  • 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
  • if (conditional) { // statement if true } Tuesday, September 22, 2009
  • if (conditional) { // statement if true } else Tuesday, September 22, 2009
  • if (conditional) { // statement if true } else { Tuesday, September 22, 2009
  • if (conditional) { // statement if true } else { // statement if false Tuesday, September 22, 2009
  • if (conditional) { // statement if true } else { // statement if false } Tuesday, September 22, 2009
  • false true Tuesday, September 22, 2009
  • var age = 60; var status = null; if (age > 62) { status = 'retired'; } else { status = 'not retired'; } status; // "not retired" Tuesday, September 22, 2009
  • var age = 65; var status = null; if (age > 62) { status = 'retired'; } else { status = 'not retired'; } status; // "retired" Tuesday, September 22, 2009
  • 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
  • if (conditional) { // statement if true } else if (conditional) { // statement if false and else if true } else { // statement if all false } Tuesday, September 22, 2009
  • 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
  • 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
  • switch statement select one of many blocks of code to execute http://www.w3schools.com/JS/js_switch.asp Tuesday, September 22, 2009
  • switch Tuesday, September 22, 2009
  • switch (value) Tuesday, September 22, 2009
  • switch (value) { Tuesday, September 22, 2009
  • switch (value) { case 1: Tuesday, September 22, 2009
  • switch (value) { case 1: // some code to execute Tuesday, September 22, 2009
  • switch (value) { case 1: // some code to execute break; Tuesday, September 22, 2009
  • switch (value) { case 1: // some code to execute break; case 2: // some code to execute break; Tuesday, September 22, 2009
  • 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
  • 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
  • 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
  • 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
  • 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
  • Review Tuesday, September 22, 2009
  • var age = 18; var teens = false; if (age > 12 && age < 20) { teens = true; } teens; Tuesday, September 22, 2009
  • var age = 18; var teens = false; if (age > 12 && age < 20) { teens = true; } teens; // true Tuesday, September 22, 2009
  • 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
  • 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
  • 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
  • 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
  • Assignment http://teaching.johnnunemaker.com/capp-30550/sessions/conditionals Tuesday, September 22, 2009