Warning: Illegal string offset 'file' in /home/hoangkym/domains/hoangkim.net.vn/public_html/cake/libs/debugger.php on line 622
Warning: Illegal string offset 'line' in /home/hoangkym/domains/hoangkim.net.vn/public_html/cake/libs/debugger.php on line 622
Warning (2): session_start() [function.session-start]: open(/home/hoangkym/tmp/sess_mgcdr0k76nir9llobis8g3o6a0, O_RDWR) failed: No such file or directory (2) [CORE/cake/libs/cake_session.php, line 587]
session_start - [internal], line ??
CakeSession::__startSession() - CORE/cake/libs/cake_session.php, line 587
CakeSession::start() - CORE/cake/libs/cake_session.php, line 219
SessionComponent::__start() - CORE/cake/libs/controller/components/session.php, line 287
SessionComponent::read() - CORE/cake/libs/controller/components/session.php, line 137
AppController::beforeFilter() - APP/app_controller.php, line 25
Controller::startupProcess() - CORE/cake/libs/controller/controller.php, line 522
Dispatcher::_invoke() - CORE/cake/dispatcher.php, line 187
Dispatcher::dispatch() - CORE/cake/dispatcher.php, line 171
[main] - APP/webroot/index.php, line 86
Bài này chúng ta sẽ học câu lệnh rẽ nhánh trong Jaws script với các từ khóa sau:
if, elif, else, then và các toán tử
Ví dụ bài này có trong thư mục lesson8
1. Câu lệnh điều kiện đúng và sai (true và false)
Ví dụ:
5 > 3 - Câu lệnh này là đúng (true)
1 > 2 - Câu lệnh này là sai (false)
Câu lệnh rẽ nhánh điều kiện sử dụng các điều kiện trên như sau:
if <điều kiện> then
<tập lệnh
Nghĩa tiếng việt là:
Nếu < điều kiện đúng> thì
< tập lệnh>
Ví dụ:
*******
void function sayReTest(int result)
if result <4 then
say("need to attend to retest", OT_STRING)
endif
endfunction
****
Ví dụ trên là 1 hàm kiểm tra kết quả học tập của 1 học sinh, ai có điểm dưới 4 sẽ bị thông báo là phải thi lại. Còn lại những người khác mà có điểm lớn hơn 4 sẽ không bị thông báo gì cả.
Lệnh rẽ nhánh elif
elif < điều kiện> then
<tập lệnh>
Nghĩa tiếng việt là:
Còn nếu <điều kiện đúng > thì
< tập lệnh>
Lệnh rẽ nhánh else
else
< tập lệnh>
Nghĩa tiếng việt là:
Nếu biểu thức điều kiện không thỏa mãn trong if và elif thì rơi vào phần else này.if, elif, else là các giá trị có thể của một điều kiện, vì thế biểu thức điều kiện chỉ rơi vào một trong các nhánh đó thôi.
Ví dụ:
*****
int function arrange(int result)
if result <5 then
return 0 ; bad level
elif result >= 5 && result <=8 then
return 2 ; fair level
else
return 1 ; good level
endif
endfunction
*****
Ví dụ trên là một hàm xếp loại điểm cho học sinh xem học sinh sẽ xếp loại gì.
Nếu điểm < 5 thì
trả về loại kém (return 0)
Còn nếu điểm từ 5 đến 8 thì
trả về loại khá (return 2)
Còn lại (từ 8 trở lên) thì
trả về loại giỏi return 1)
Kết thúc các câu lệnh rẽ nhánh điều kiện phải có 1 dòng endif để báo là hết phần rẽ nhánh.
Xem thêm các ví dụ trong bài 8 để hiểu rõ hơn.
2. Các toán tử đi kèm với câu lệnh điều kiện
So sánh bằng: ==
So sánh hơn kém: < >
Điều kiện phủ định: not
Điều kiện kép: &&
Điều kiện hoặc: ||
Xem thêm phần script trong file ví dụ để biết cách dùng điều kiện với hàm.
Warning: Illegal string offset 'file' in /home/hoangkym/domains/hoangkim.net.vn/public_html/cake/libs/debugger.php on line 622
Warning: Illegal string offset 'line' in /home/hoangkym/domains/hoangkim.net.vn/public_html/cake/libs/debugger.php on line 622
Warning: file_exists() [function.file-exists]: open_basedir restriction in effect. File([) is not within the allowed path(s): (/home/hoangkym/:/tmp/:/var/tmp/:/opt/alt/php74/usr/share/pear/:/dev/urandom:/usr/local/php56/lib/:/usr/local/php74/lib/:/usr/local/php80/lib/:/usr/local/php70/lib/:/usr/local/php56/lib/:/usr/local/lib/php/) in /home/hoangkym/domains/hoangkim.net.vn/public_html/cake/libs/debugger.php on line 452
Warning (2): Unknown: open(/home/hoangkym/tmp/sess_mgcdr0k76nir9llobis8g3o6a0, O_RDWR) failed: No such file or directory (2) [Unknown, line ??]
Warning: Illegal string offset 'file' in /home/hoangkym/domains/hoangkim.net.vn/public_html/cake/libs/debugger.php on line 622
Warning: Illegal string offset 'line' in /home/hoangkym/domains/hoangkim.net.vn/public_html/cake/libs/debugger.php on line 622
Warning: file_exists() [function.file-exists]: open_basedir restriction in effect. File([) is not within the allowed path(s): (/home/hoangkym/:/tmp/:/var/tmp/:/opt/alt/php74/usr/share/pear/:/dev/urandom:/usr/local/php56/lib/:/usr/local/php74/lib/:/usr/local/php80/lib/:/usr/local/php70/lib/:/usr/local/php56/lib/:/usr/local/lib/php/) in /home/hoangkym/domains/hoangkim.net.vn/public_html/cake/libs/debugger.php on line 452
Warning (2): Unknown: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/home/hoangkym/tmp) [Unknown, line ??]
Bình luận