Repetition

Objectives

Repetition, document.write()

Repetition structures

  while (expr) {
     ... code executed while expr is true ...
  }

  do {
     ... code executed once and then while expr is true ...
  } while (expr);

  for (initexpr; testexpr; modexpr) {
     ... code executed while testexpr is true ...
  }

  for (variable in object) {
     ... code executed once for each element in object ...
  }

break and continue

labeled break and continue

void operator

empty statement

Repetition and arrays

Generating tables using JavaScript (repetition)

<!doctype HTML> <html lang="en"> <head> <meta charset="utf-8" /> <title>Create a table using JavaScript</title> <style> table, td, th { border-width: 1; border-style: solid; border-collapse: collapse; } </style> </head> <body> <table> <script type="text/javascript"> for (row=1; row<=10; row++) { if (row%2==0) { document.write('<tr bgcolor="lime">'); } else { document.write('<tr>'); } document.write('<th bgcolor="yellow">row=' + row + ',<br /> col=1</th>'); for (col=2; col<=10; col++) { document.write('<td>row=' + row + ',<br /> col=' + col + '</td>'); } document.writeln('</tr>'); } </script> </table> </body> </html>

Result of preceding program