1 / 15

ECMM6018 Enterprise Networking For Electronic Commerce Tutorial 4

ECMM6018 Enterprise Networking For Electronic Commerce Tutorial 4. Client Side Scripting JavaScript Looping. Types of Looping. While Do While For. While Statement. loops through a block of code while a condition is true Syntax while (condition) {

faught
Download Presentation

ECMM6018 Enterprise Networking For Electronic Commerce Tutorial 4

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. ECMM6018 Enterprise Networking For Electronic CommerceTutorial 4 Client Side Scripting JavaScript Looping

  2. Types of Looping • While • Do While • For

  3. While Statement • loops through a block of code while a condition is true • Syntax while (condition) { code to be executed } • JavaScript is case sensitive while ≠ WHILE • N.B. All JavaScript reserved keywords are case sensitive After the while statement is executed program flows to the next statement after the while is finished

  4. While Statement ctd. • The while statement can be used in 2 ways • 1.) The number of repetitions is known in advance e.g. calculating the average of a class. <html> <head> <title>Class Average Program</title> <script language=“JavaScript”> var total, gradeCounter, gradeValue, average, grade; total = 0; gradeCounter =1;

  5. While Statement ctd. while (gradeCounter <=10) { grade = window.prompt (“Enter integer grade:”, “0”); gradeValue = parseInt(grade); total = total +gradeValue; gradeCounter = gradeCounter + 1; } average = total /10; document.writeln(“<H1>” Class average is “ +average + “</H1>”); </script>

  6. While Statement ctd. </head> <body> Some Text Here </body> </html>

  7. While Statement using a sentinel value • 2.) Where the number of repetitions is not known in advance. In this scenario a value known as a sentinel value is used as a stopping value. <html> <head> <title>Class Average Program</title> <script language=“JavaScript”> var total, gradeCounter, gradeValue, average, grade; total = 0; gradeCounter =0;

  8. While Statement using a sentinel value grade = window.prompt(“Enter Integer Grade, -1 to Quit:”, “0”); gradeValue = parseInt(grade); while (gradeValue != -1) { total = total + gradeValue; gradeCounter = gradeCounter +1; grade = window.prompt(“Enter Integer Grade, -1 to Quit: “, “0”); gradeValue = parseInt(grade); }

  9. While Statement using a sentinel value if (gradeCounter !=0) { average = total/gradeCounter; document.writeln(“<H1>Class average is “ +average + “</H1>”); } else { document.writeln(“<H1>Class average is “ +average + “</H1>”); } </script>

  10. While Statement using a sentinel value </head> <body> Some text </body> </html>

  11. Do While Statement • Loops through the block at least once • Syntax do { code to be executed } while (condition); example <html> <body> <script type="text/javascript"> i = 0

  12. Do While ctd. do { document.write("The number is " + i) document.write("<br>") i++ } while (i <= 5); </script> </body> </html>

  13. The For Loop • run statements a specified number of times • Requires: - the name of a control variable - its initial value - the increment/decrement by which the loop counter is modified - the testing condition for the final value

  14. For Loop ctd. • Syntax for(initialization; test condition; increment) { code to be executed; } • Example <html> <head> <title>Sum the Even Integers from 2 to 100</title>

  15. For Loop ctd <script language = “JavaScript”> var sum = 0; for (var number =2; number<=100; number +2) { sum += number; } document.writeln(“<H1>”The sum of the even integers “ + “ from 2 to 100 is “ +sum + “</H1>”); </script> </head> <body> </body> </html>

More Related