1 / 10

CIS 375—Web App Dev II

CIS 375—Web App Dev II. JavaScript II. if and if…else Syntax. if (condition) { code to be executed if condition is true } if (condition) { code to be executed if condition is true } else { code to be executed if condition is true }. Example of if Statement.

earl
Download Presentation

CIS 375—Web App Dev II

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CIS 375—Web App Dev II JavaScript II

  2. if and if…else Syntax if (condition) { code to be executed if condition is true } if (condition) { code to be executed if condition is true } else { code to be executed if condition is true }

  3. Example of if Statement // Display “Good morning” if before 10 am var today = new Date() var time = today.getHours() if (time < 10) { document.write("<b>Good morning</b>") }

  4. Example of if…else Statement // Display “Good morning” if before 10 am // Display “Good day!” otherwise var today = new Date() var time = today.getHours() if (time < 10) { document.write("<b>Good morning</b>") } else { document.write("Good day!") }

  5. Syntax of switch Statement switch (expression) { case label1: code to be executed if expression = label1 break case label2: code to be executed if expression = label2 break default: code to be executed if expression is different from both label1 and label2 }

  6. Example of switch Statement var today = new Date() theDay = today.getDay() switch (theDay) { case 5: document.write("Finally Friday") break case 6: document.write("Super Saturday") break case 0: document.write("Sleepy Sunday") break default: document.write(“Can’t wait till the weekend!”) }

  7. Syntax for Looping • while while (condition) { code to be executed } • do…while do { code to be executed } while (condition) • for (equivalent to _______ with a counter variable) for (initialization; condition; increment) { code to be executed }

  8. Examples of while & do…while • while i = 0 while (i <= 5){ document.write("The number is " + i) document.write("<br>") i++ } • do…while i = 0 do{ document.write("The number is " + i) document.write("<br>") i++ } while (i <= 5)

  9. Example of for Loop for (i = 1; i <= 6; i++){ document.write("<h" +i+ ">This is header " + i) document.write("</h" + i + ">") }

  10. Miscellaneous Guidelines • JavaScript is ______ sensitive • A function named "myfunction" is not the same as "myFunction". • White space (use to make code more ___________) • JavaScript ignores extra spaces • name="Hege" same as name = "Hege“ • Use the “\” to insert special characters • document.write (“Let’s sing \"Happy Birthday\".") • Comments • Use // for single line, /*comments */ for a block

More Related