Một toán tử bậc ba có thể được sử dụng để thay thế một câu lệnh
// program to check pass or fail
let marks = prompt['Enter your marks :'];
// check the condition
let result = [marks >= 40] ? 'pass' : 'fail';
console.log[`You ${result} the exam.`];
7 trong một số tình huống nhất định. Trước khi bạn tìm hiểu về các toán tử bậc ba, hãy nhớ kiểm tra JavaScript nếu. hướng dẫn khácToán tử ternary là gì?
Toán tử bậc ba đánh giá một điều kiện và thực thi một khối mã dựa trên điều kiện
Cú pháp của nó là
condition ? expression1 : expression2
Toán tử ternary đánh giá điều kiện kiểm tra
- Nếu điều kiện là
0, biểu thức 1 được thực hiện// program to check pass or fail let marks = prompt['Enter your marks :']; // check the condition let result = [marks >= 40] ? 'pass' : 'fail'; console.log[`You ${result} the exam.`];
- Nếu điều kiện là
1, biểu thức 2 được thực hiện// program to check pass or fail let marks = prompt['Enter your marks :']; // check the condition let result = [marks >= 40] ? 'pass' : 'fail'; console.log[`You ${result} the exam.`];
Toán tử bậc ba nhận ba toán hạng, do đó, tên toán tử bậc ba. Nó còn được gọi là toán tử có điều kiện
Hãy viết một chương trình để xác định xem một học sinh đạt hay trượt trong kỳ thi dựa trên số điểm đạt được
Thí dụ. Toán tử bậc ba JavaScript
// program to check pass or fail
let marks = prompt['Enter your marks :'];
// check the condition
let result = [marks >= 40] ? 'pass' : 'fail';
console.log[`You ${result} the exam.`];
Đầu ra 1
Enter your marks: 78 You pass the exam.
Giả sử người dùng nhập 78. Sau đó, điều kiện
// program to check pass or fail
let marks = prompt['Enter your marks :'];
// check the condition
let result = [marks >= 40] ? 'pass' : 'fail';
console.log[`You ${result} the exam.`];
2 được kiểm tra để đánh giá thành // program to check pass or fail
let marks = prompt['Enter your marks :'];
// check the condition
let result = [marks >= 40] ? 'pass' : 'fail';
console.log[`You ${result} the exam.`];
0. Vì vậy, biểu thức đầu tiên // program to check pass or fail
let marks = prompt['Enter your marks :'];
// check the condition
let result = [marks >= 40] ? 'pass' : 'fail';
console.log[`You ${result} the exam.`];
4 được gán cho biến kết quảĐầu ra 2
________số 8Giả sử sử dụng nhập 35. Sau đó, điều kiện
// program to check pass or fail
let marks = prompt['Enter your marks :'];
// check the condition
let result = [marks >= 40] ? 'pass' : 'fail';
console.log[`You ${result} the exam.`];
2 đánh giá thành // program to check pass or fail
let marks = prompt['Enter your marks :'];
// check the condition
let result = [marks >= 40] ? 'pass' : 'fail';
console.log[`You ${result} the exam.`];
1. Vì vậy, biểu thức thứ hai // program to check pass or fail
let marks = prompt['Enter your marks :'];
// check the condition
let result = [marks >= 40] ? 'pass' : 'fail';
console.log[`You ${result} the exam.`];
7 được gán cho biến kết quảToán tử bậc ba được sử dụng thay cho if. khác
Trong JavaScript, một toán tử bậc ba có thể được sử dụng để thay thế một số loại câu lệnh
// program to check pass or fail
let marks = prompt['Enter your marks :'];
// check the condition
let result = [marks >= 40] ? 'pass' : 'fail';
console.log[`You ${result} the exam.`];
7 nhất định. Ví dụ,Bạn có thể thay thế mã này
// program to check pass or fail
let marks = prompt['Enter your marks :'];
// check the condition
let result = [marks >= 40] ? 'pass' : 'fail';
console.log[`You ${result} the exam.`];
2với
// program to check pass or fail
let marks = prompt['Enter your marks :'];
// check the condition
let result = [marks >= 40] ? 'pass' : 'fail';
console.log[`You ${result} the exam.`];
3Đầu ra của cả hai chương trình sẽ giống nhau
đầu ra
// program to check pass or fail
let marks = prompt['Enter your marks :'];
// check the condition
let result = [marks >= 40] ? 'pass' : 'fail';
console.log[`You ${result} the exam.`];
4Toán tử ternary lồng nhau
Bạn cũng có thể lồng một toán tử bậc ba làm biểu thức bên trong một toán tử bậc ba khác. Ví dụ,
// program to check pass or fail
let marks = prompt['Enter your marks :'];
// check the condition
let result = [marks >= 40] ? 'pass' : 'fail';
console.log[`You ${result} the exam.`];
5đầu ra
// program to check pass or fail
let marks = prompt['Enter your marks :'];
// check the condition
let result = [marks >= 40] ? 'pass' : 'fail';
console.log[`You ${result} the exam.`];
6Ghi chú. Bạn nên cố gắng tránh các toán tử bậc ba lồng nhau bất cứ khi nào có thể vì chúng làm cho mã của bạn khó đọc
Trong hướng dẫn này, bạn sẽ tìm hiểu về Java Ternary Operator và ứng dụng của nó với ví dụ thực tế
Toán tử bậc ba Java [ ?. ]
Trong Java, toán tử có điều kiện hoặc bậc ba được coi là viết tắt của câu lệnh if-else trong java. Toán tử điều kiện còn được gọi là “Toán tử bậc ba”
Mục lục−
- Toán tử bậc ba Java [ ?. ]
cú pháp
1
điều kiện ? kết quả1 . kết quả2
Nếu điều kiện đúng, biểu thức sẽ trả về kết quả 1, nếu không, nó sẽ trả về kết quả 2