JavaScript while loop
gelung while ialah gelung termudah dalam JavaScript Sintaksnya ialah:
penyataan while (expr){
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> <script type="text/javascript"> var i = 1; while (i <= 10) { document.write(i + "<br />"); i++; } </script> </head> <body> </body> </html>Hasil berjalan:
12
3
4
5
6
7
8
9
10
gelung do while
Gelung do while sangat serupa dengan gelung while, cuma bezanya do while dijamin untuk dilaksanakan sekali, manakala sementara adalah Jika ungkapan tidak dipegang, tiada operasi boleh dilakukan. sintaks gelung lakukan sambil:buat { pernyataan
}sambil (expr)
rreee
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> <script type="text/javascript"> var i = 1; do { document.write(i + "<br />"); i++; } while (i <= 10); </script> </head> <body> </body> </html>Gunakan gelung semasa
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <script> cars=["BMW","Volvo","Saab","Ford"]; var i=0; for (;cars[i];){ document.write(cars[i] + "<br>"); i++; } </script> </body> </html>