Web engineering: Lecture 17, 18 - Majid Mumtaz
Thông tin tài liệu:
Nội dung trích xuất từ tài liệu:
Web engineering: Lecture 17, 18 - Majid Mumtaz Web Engineering Lecture 17-18 MAJID MUMTAZ Department of Computer Science, CIIT Wah 1 JavaScript Statements • In JavaScript we have the following conditional statements: – if statement - use this statement to execute some code only if a specified condition is true – if...else statement - use this statement to execute some code if the condition is true and another code if the condition is false – if...else if....else statement - use this statement to select one of many blocks of code to be executed – switch statement - use this statement to select one of many blocks of code to be executed 2 JavaScript Statements • If Statement if (condition) { code to be executed if condition is true } • Example if (time JavaScript Statements • If...else Statement: Use the if....else statement to execute some code if a condition is true and another code if the condition is not true. – if (condition) { code to be executed if condition is true } else { code to be executed if condition is not true } Example: if (time JavaScript Statements • If...else if...else Statement: Use the if....else if...else statement to select one of several blocks of code to be executed. – if (condition1) { code to be executed if condition1 is true } else if (condition2) { code to be executed if condition2 is true } else { code to be executed if neither condition1 nor condition2 is true } 5 JavaScript Statements • The JavaScript Switch Statement :Use the switch statement to select one of many blocks of code to be executed. Syntax: switch(n) { case 1: execute code block 1 break; case 2: execute code block 2 break; default: code to be executed if n is different from case 1 and 2 } • Home work: – Display today's weekday-name. Note that Sunday=0, Monday=1, Tuesday=2, etc: 6 JavaScript Loops • Loops can execute a block of code a number of times. • Different Kinds of Loops: JavaScript supports different kinds of loops: – for - loops through a block of code a number of times – for/in - loops through the properties of an object – while - loops through a block of code while a specified condition is true – do/while - also loops through a block of code while a specified condition is true 7 The For Loop • The for loop has the following syntax: for (initialization; condition; Increment/decrement) { the code block to be executed } 8 The For/In Loop • The JavaScript for/in statement loops through the properties of an object: Syntax for(var property_name in object){ statements; } Example: var txt=''; var person={Fname:“Ali“, Lname:“khan', age:25}; for (var x in person) { txt=txt + person[x]; } Document.write(” ” + txt); // Alikhan25 9 JavaScript While Loop • Loops can execute a block of code as long as a specified condition is true. • Syntax while (condition) { code block to be executed } Example: var i =0; while (i The Do/While Loop • The do/while loop is a variant of the while loop. This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true. • Syntax do { code block to be executed } while (condition); Example do { document.write ('The number is ' + i + '“); i++; } while (i Home work • Create a for loop that displays numbers as: 10 9 8 7 6 5 4 3 2 1. Put the numbers in HTML table cells. • Repeat same task using while loop • JavaScript dialog Boxes – Alert box – Confirm box – Prompt box 12 JavaScript Break and Continue • The break statement 'jumps out' of a loop. • The continue statement 'jumps over' one iteration in the loop. E.g. Examples: 1. for (i=0;i JavaScript Errors • The try statement lets you test a block of code for errors. • The catch statement lets you handle the error. • The throw statement lets you create custom errors. 14 JavaScript Errors • JavaScript try and catch: var txt=''; – Syntax function message() { try try { adddlert('Welcome guest!'); { } catch(err) { //Run some code here txt='There was an error on this page.\n\n'; txt+='Error description: ' + err.message + } '\n\n'; txt+='Click OK t ...
Tìm kiếm theo từ khóa liên quan:
Web Engineering Lecture Web engineering JavaScript Statements If Statement JavaScript Loops JavaScript While LoopTài liệu cùng danh mục:
-
Chương trình khung trình độ trung cấp nghề nghề Thiết kế trang Web - Trường CĐN GTVT Đường Thuỷ 1
6 trang 387 0 0 -
45 trang 370 4 0
-
Elasticity for MQTT brokers in IoT applications
13 trang 288 0 0 -
13 trang 270 0 0
-
Đề thi thực hành môn Thiết kế Web - Trường Cao đẳng nghề Vĩnh Phúc
3 trang 254 2 0 -
A study on cloud computing security
15 trang 254 0 0 -
EBay - Internet và câu chuyện thần kỳ: Phần 1
143 trang 249 0 0 -
14 trang 232 0 0
-
8 trang 196 0 0
-
Tìm kiếm thông minh hơn với Google
4 trang 187 0 0
Tài liệu mới:
-
Khảo sát tình trạng dinh dưỡng trước mổ ở người bệnh ung thư đại trực tràng
9 trang 20 0 0 -
94 trang 18 0 0
-
Tham vấn Thanh thiếu niên - ĐH Mở Bán công TP Hồ Chí Minh
276 trang 19 0 0 -
Kết hợp luân phiên sóng T và biến thiên nhịp tim trong tiên lượng bệnh nhân suy tim
10 trang 18 0 0 -
Đề thi giữa học kì 1 môn Ngữ văn lớp 9 năm 2024-2025 có đáp án - Trường THCS Nguyễn Trãi, Thanh Khê
14 trang 20 0 0 -
Đánh giá hiệu quả giải pháp phát triển thể chất cho sinh viên Trường Đại học Kiến trúc Hà Nội
8 trang 18 0 0 -
Tỉ lệ và các yếu tố liên quan đoạn chi dưới ở bệnh nhân đái tháo đường có loét chân
11 trang 19 0 0 -
39 trang 18 0 0
-
Đề thi học kì 1 môn Tiếng Anh lớp 6 năm 2024-2025 có đáp án - Trường TH&THCS Quang Trung, Hội An
6 trang 18 1 0 -
Tôm ram lá chanh vừa nhanh vừa dễRất dễ làm, nhanh gọn mà lại ngon. Nhà mình
7 trang 18 0 0