Page 56 - การโปรแกรมเว็บ
P. 56

7-44 การโปรแกรมเว็บ
       5.2 	ค�ำสั่ง if-else นอกจากน้ียังสามารถท่ีจะใช้ค�ำส่ัง else-if ในกรณีที่เงื่อนไขมีหลายเง่ือนไข

และการทำ� งานในแตล่ ะอยา่ งแตกตา่ งกนั ดงั จะแสดงในตวั อยา่ งตอ่ ไป ซงึ่ เปน็ การประเมนิ ผลการเรยี นจาก
คะแนน ดงั ตัวอยา่ งที่ 7.16 การใชค้ �ำส่ังท้งั หมดรวมกัน

       ตัวอย่างที่ 7.16 การใช้ if แบบที่มี if-else และ else-if

 MyJSP7_else_if.jsp
 โปรแกรมนี้เปน็ การใช้คำ� สง่ั if เพื่อตรวจสอบผลการเรยี นจากคะแนน

     1 <%@page contentType="text/html" pageEncoding="UTF-8"%>
    2 <!DOCTYPE html>
    3 <html>
    4 	 <head>
    5 		 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    6 		 <title>JSP Page</title>
    7 	 </head>
    8 	 <body>
    9 	 <%
    10 	 int score = 81; // กำ� หนดตวั แปร score แทนค่าคะแนนทไี่ ด้รบั
    11 	 if (score >= 80) {
    12 			 out.print("Your score is excellent.");
    13 			 out.print("You grant grade S.");
    14 		 } else if (score >= 60) {
    15 			 out.print("Your score is good.");
    16 			 out.print("You grant grade A.");
    17 		 } else if (score >= 40) {
    18 			 out.print("Your score is fair.");
    19 			 out.print("You grant grade B.");
   20 		 } else {
    21 			 out.print("Your score is poor.");
   22 			 out.print("You grant grade C.");
   23 		 }
   24 	 %>
   25 	 </body>
   26 </html>
   51   52   53   54   55   56   57   58   59   60   61