الجمل الشرطية في Javaالنص: إذا، وإلا، وإلا إذا
Javaعبارات شرطية في البرنامج النصي
هناك ثلاثة أنواع رئيسية من البيانات الشرطية في Javaالنصي.
- إذا البيان: عبارة "if" تنفذ التعليمات البرمجية بناءً على الشرط.
- if… else البيان: عبارة if...else تتكون من كتلتين من التعليمات البرمجية؛ عندما يكون الشرط صحيحًا، فإنه ينفذ المجموعة الأولى من التعليمات البرمجية، وعندما يكون الشرط خاطئًا، فإنه ينفذ المجموعة الثانية من التعليمات البرمجية.
- إذا…إلا إذا…بيان آخر: عندما يلزم اختبار شروط متعددة وتنفيذ كتل مختلفة من التعليمات البرمجية بناءً على الشرط الصحيح، يتم استخدام عبارة if…else if…else.
كيفية استخدام العبارات الشرطية
تُستخدم العبارات الشرطية لتحديد تدفق التنفيذ بناءً على شروط مختلفة. إذا كان الشرط صحيحًا، فيمكنك تنفيذ إجراء واحد، وإذا كان الشرط خاطئًا، فيمكنك تنفيذ إجراء آخر.
إذا البيان
بناء الجملة:
if(condition)
{
lines of code to be executed if condition is true
}
يمكنك استخدام if بيان إذا كنت تريد التحقق من حالة معينة فقط.
جرب هذا بنفسك:
<html>
<head>
<title>IF Statments!!!</title>
<script type="text/javascript">
var age = prompt("Please enter your age");
if(age>=18)
document.write("You are an adult <br />");
if(age<18)
document.write("You are NOT an adult <br />");
</script>
</head>
<body>
</body>
</html>
إذا... بيان آخر
بناء الجملة:
if(condition)
{
lines of code to be executed if the condition is true
}
else
{
lines of code to be executed if the condition is false
}
يمكنك استخدام If….Else بيان إذا كان عليك التحقق من شرطين وتنفيذ مجموعة مختلفة من الرموز.
جرب هذا بنفسك:
<html>
<head>
<title>If...Else Statments!!!</title>
<script type="text/javascript">
// Get the current hours
var hours = new Date().getHours();
if(hours<12)
document.write("Good Morning!!!<br />");
else
document.write("Good Afternoon!!!<br />");
</script>
</head>
<body>
</body>
</html>
إذا...إلس إذا...بيان آخر
بناء الجملة:
if(condition1)
{
lines of code to be executed if condition1 is true
}
else if(condition2)
{
lines of code to be executed if condition2 is true
}
else
{
lines of code to be executed if condition1 is false and condition2 is false
}
يمكنك استخدام If….Else If….Else بيان إذا كنت تريد التحقق من أكثر من شرطين.
جرب هذا بنفسك:
<html>
<head>
<script type="text/javascript">
var one = prompt("Enter the first number");
var two = prompt("Enter the second number");
one = parseInt(one);
two = parseInt(two);
if (one == two)
document.write(one + " is equal to " + two + ".");
else if (one<two)
document.write(one + " is less than " + two + ".");
else
document.write(one + " is greater than " + two + ".");
</script>
</head>
<body>
</body>
</html>

