Các câu lệnh rẽ nhánh và lặp

Một phần của tài liệu Tài liệu Giới thiệu Internet-intranet pdf (Trang 56 - 62)

Trong JavaScript có 3 câu lệnh điều kiện

• if câu lệnh

• if...else câu lệnh

• switch câu lệnh

a. Câu lệnh If and If...else câu lệnh

Cú pháp câu lệnh if

if (điều kiện) {

Câu lệnh thực hiện nếu điều kiện đúng }

Ví dụ

<script type="text/javascript">

//If the time on your browser is less than 10, //you will get a "Good morning" greeting. var d=new Date()

var time=d.getHours() if (time<10) { document.write("<b>Good morning</b>") } </script> Cú pháp câu lệnh If..else if (điều kiện) {

Câu lệnh thực hiện nếu điều kiện đúng }else

{

Câu lệnh thực hiện nếu điều kiện sai }

Ví dụ:

<script type="text/javascript">

//If the time on your browser is less than 10, //you will get a "Good morning" greeting. //Otherwise you will get a "Good day" greeting. var d = new Date()

var time = d.getHours() if (time < 10)

{

document.write("Good morning!") }

else { document.write("Good day!") } </script> Cú pháp câu lệnh If..else if if (điều kiện) {

Câu lệnh thực hiện nếu điều kiện đúng }else if (điều kiện)

{

Câu lệnh thực hiện nếu điều kiện sai } …… Else { } Ví dụ: <html> <body>

<script language="JavaScript" type="text/javascript"> var myAge = Number(prompt("Enter your age",30)); if (myAge >= 0 && myAge <= 10)

{

document.write("myAge is between 0 and 10<br>"); }

if ( !(myAge >= 0 && myAge <= 10) ) {

document.write("myAge is NOT between 0 and 10<br>"); }

if ( myAge >= 80 || myAge <= 10 ) { (adsbygoogle = window.adsbygoogle || []).push({});

document.write("myAge is 80 or above OR 10 or below<br>"); }

if ( (myAge >= 30 && myAge <= 39) || (myAge >= 80 && myAge <= 89) ) {

document.write("myAge is between 30 and 39 or myAge is between 80 and 89"); }

</script> </body> </html>

if (myAge >= 0 && myAge <= 10) {

document.write("myAge is between 0 and 10"); }

else if ( (myAge >= 30 && myAge <= 39) || (myAge >= 80 && myAge <= 89) ) {

document.write("myAge is between 30 and 39 " + "or myAge is between 80 and 89"); }

else {

document.write("myAge is NOT between 0 and 10, " +

"nor is it between 30 and 39, nor is it between 80 and 89"); } b. Cậu lệnh rẽ nhánh Cú pháp switch (biểu thức) { case nhãn1:

Mã được thực hiện nếu biểu thức = nhãn1 break

case label2:

Mã được thực hiện nếu biểu thức = nhãn2 break

default:

Mã được thực hiện nếu biểu thức khác với nhãn1 và nhãn2 }

Ví dụ:

<script type="text/javascript">

//You will receive a different greeting based //on what day it is. Note that Sunday=0, //Monday=1, Tuesday=2, etc.

var d=new Date() theDay=d.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("I'm looking forward to this weekend!") }

</script>

c. Toán tử điều kiện

JavaScript chứa toán tử điều kiện gán giá trị cho biến dựa vào một vài điều kiện

Cú pháp

variablename=(điều kiện)?giá trị 1: giá trị 2 Ví dụ

d. Vòng lặp

while

Câu lệnh while sẽ thực hiện các câu lệnh đến khi nào điều kiện đúng while (điều kiện)

{ Mã thực thi } Ví dụ: <html> <body> <script type="text/javascript"> i = 0 while (i <= 5) { document.write("The number is " + i) document.write("<br>") i++ } </script> <p>Explanation:</p> <p><b>i</b> equal to 0.</p> (adsbygoogle = window.adsbygoogle || []).push({});

<p>While <b>i</b> is less than , or equal to, 5, the loop will continue to run.</p> <p><b>i</b> will increase by 1 each time the loop runs.</p>

</body> </html>

do...while

Câu lệnh while sẽ thực hiện các câu lệnh đến khi nào điều kiện đúng Do

{

Mã thực thi }

while (điều kiện) Ví dụ: <html> <body> <script type="text/javascript"> i = 0 do { document.write("The number is " + i) document.write("<br>") i++ } while (i <= 5) </script> <p>Explanation:</p> <p><b>i</b> equal to 0.</p>

<p>The loop will run</p>

<p><b>i</b> will increase by 1 each time the loop runs.</p>

<p>While <b>i</b> is less than , or equal to, 5, the loop will continue to run.</p> </body>

</html>

for

Câu lệnh for thực hiện với số lần cho trước for (giá trị khởi tạo; điều kiện; tăng) { Mã thực thi } Ví dụ 1 <html> <body> <script type="text/javascript"> for (i = 0; i <= 5; i++) { document.write("The number is " + i) document.write("<br>") } </script> <p>Explanation:</p>

<p>The for loop sets <b>i</b> equal to 0.</p>

<p>As long as <b>i</b> is less than , or equal to, 5, the loop will continue to run.</p> <p><b>i</b> will increase by 1 each time the loop runs.</p>

</body> </html>

var loopCounter;

for (loopCounter = 1; loopCounter <= 3; loopCounter++) <html>

<body>

<script language="JavaScript" type="text/javascript"> var degFahren = new Array(212, 32, -459.15);

var degCent = new Array(); var loopCounter;

for (loopCounter = 0; loopCounter <= 2; loopCounter++) {

degCent[loopCounter] = 5/9 * (degFahren[loopCounter] - 32); }

for (loopCounter = 2; loopCounter >= 0; loopCounter--) {

document.write("Value " + loopCounter + " was " + degFahren[loopCounter] + " degrees Fahrenheit");

document.write(" which is " + degCent[loopCounter] + " degrees centigrade<br>");

}

</body> </html> (adsbygoogle = window.adsbygoogle || []).push({});

Trong một số trường hợp lấy giá trị của các phần tử trong mảng mà không quan tâm tới chỉ mạng mảng, chúng ta có thể sủ dụng vòng lặp for với cú pháp như sau:

for (chỉ sổ in tênmảng) {

Câu lệnh

} Ví dụ:

var myArray = new Array("Paul","Paula","Pauline"); var loopCounter;

for (loopCounter = 0; loopCounter < 3; loopCounter++) {

document.write(myArray[loopCounter]); }

var elementIndex;

for (elementIndex in myArray) {

document.write(myArray[elementIndex]); }

Các câu lệnh break và continue

<script language="JavaScript" type="text/javascript"> var degFahren = new Array(212, "string data", -459.67); var degCent = new Array();

var loopCounter;

for (loopCounter = 0; loopCounter <= 2; loopCounter++) {

if (isNaN(degFahren[loopCounter])) {

alert("Data '" + degFahren[loopCounter] + "' at array index " + loopCounter + " is invalid"); break; } degCent[loopCounter] = 5/9 * (degFahren[loopCounter] - 32); } Hoặc if (isNaN(degFahren[loopCounter])) {

alert("Data '" + degFahren[loopCounter] + "' at array index " + loopCounter + " is invalid");

continue; }

Một phần của tài liệu Tài liệu Giới thiệu Internet-intranet pdf (Trang 56 - 62)