تفاوت میان نسخههای «JavaScript:تکه کد Switch در جاوا اسکریپت»
جز (جایگزینی متن - 'جاوا اسکریپت' به 'جاوااسکریپت') |
|||
سطر ۱۰: | سطر ۱۰: | ||
== The JavaScript Switch Statement == | == The JavaScript Switch Statement == | ||
− | عبارت Switch در | + | عبارت Switch در جاوااسکریپت |
Use the <code>switch</code> statement to select one of many code blocks to be executed. | Use the <code>switch</code> statement to select one of many code blocks to be executed. | ||
سطر ۱۲۳: | سطر ۱۲۳: | ||
When JavaScript reaches a <code>break</code> keyword, it breaks out of the switch block. | When JavaScript reaches a <code>break</code> keyword, it breaks out of the switch block. | ||
− | زمانی که | + | زمانی که جاوااسکریپت به کلمه کلیدی break میرسد، جاوااسکریپت از بلاک switch خارج میشود. |
This will stop the execution of inside the block. | This will stop the execution of inside the block. | ||
سطر ۱۳۱: | سطر ۱۳۱: | ||
It is not necessary to break the last case in a switch block. The block breaks (ends) there anyway. | It is not necessary to break the last case in a switch block. The block breaks (ends) there anyway. | ||
− | استفاده از کلمه کلیدی break در آخرین case ضروری نیست. | + | استفاده از کلمه کلیدی break در آخرین case ضروری نیست. جاوااسکریپت از بلاک Switch در آخرین case تحت هر شرایطی بعد از اجرای کد آن خارج خواهد شد. |
'''Note:''' If you omit the break statement, the next case will be executed even if the evaluation does not match the case. | '''Note:''' If you omit the break statement, the next case will be executed even if the evaluation does not match the case. |
نسخهٔ ۱۲ آبان ۱۳۹۸، ساعت ۱۵:۱۰
https://www.w3schools.com/js/js_switch.asp
ظظظظظظظظظظظظظظظظظظظظ [۱]
محتویات
JavaScript Switch Statement
عبارت switch
The switch
statement is used to perform different actions based on different conditions.
عبارت Switch برای انجام عملیاتهای متفاوت در شرایطهای متفاوت استفاده میشود.
The JavaScript Switch Statement
عبارت Switch در جاوااسکریپت
Use the switch
statement to select one of many code blocks to be executed.
از عبارت Switch برای انتخاب کردن بلاکی که میبایست از بین چندین بلاک کد اجرا شود استفاده کنید.
Syntax
شیوه نوشتار
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
This is how it works:
شیوه عمل این عبارت به صورت زیر است:
· The switch expression is evaluated once.
عبارت Switch یک بار ارزیابی میشود.
· The value of the expression is compared with the values of each case.
مقدار عبارت با هر یک از Case ها مقایسه میشود.
· If there is a match, the associated block of code is executed.
اگر با هر یکی از Case ها نتیجه برابر بود، بلاک کد مربوط به آن case اجرا میشود.
Example
The getDay()
method returns the weekday as a number between 0 and 6.
متد getDay() شماره روز را به صورت عددی بین ۰ تا ۶ برمیگرداند.
(Sunday=0, Monday=1, Tuesday=۲ ..)
(یکشنبه = ۰، دوشنبه = ۱، سه شنبه =۲ ، ....)
This example uses the weekday number to calculate the weekday name:
مثال زیر از شماره روز برای به دست آوردن نام روز استفاده میکند:
switch (new Date().getDay()) {
case 0:
day = "Sunday";
break;
case 1:
day = "Monday";
break;
case 2:
day = "Tuesday";
break;
case 3:
day = "Wednesday";
break;
case 4:
day = "Thursday";
break;
case 5:
day = "Friday";
break;
case 6:
day = "Saturday";
}
The result of day will be:
نتیجه متغیر day برابر خواهد بود با:
Tuesday
The break Keyword
کلمه کلیدی break
When JavaScript reaches a break
keyword, it breaks out of the switch block.
زمانی که جاوااسکریپت به کلمه کلیدی break میرسد، جاوااسکریپت از بلاک switch خارج میشود.
This will stop the execution of inside the block.
این کلمه موجب متوقف شدن اجرای کد درون بلاک میشود.
It is not necessary to break the last case in a switch block. The block breaks (ends) there anyway.
استفاده از کلمه کلیدی break در آخرین case ضروری نیست. جاوااسکریپت از بلاک Switch در آخرین case تحت هر شرایطی بعد از اجرای کد آن خارج خواهد شد.
Note: If you omit the break statement, the next case will be executed even if the evaluation does not match the case.
توجه داشته باشید که اگر شما عبارت break را ننویسید، Case بعدی حتی در صورت اینکه با شرط دستور Switch همخوانی نداشته باشد، اجرا خواهد شد.
The default Keyword
کلمه کلیدی Default
The default
keyword specifies the code to run if there is no case match:
کلمه کلیدی Default بلاک کدی را میبایست در صورت برقرار نبودن هیچیک از Caseها اجرا شود را مشخص میکند.
Example
The getDay()
method returns the weekday as a number between 0 and 6.
متد getDay() شماره روز را به صورت عددی بین ۰ تا ۶ برمیگرداند.
If today is neither Saturday (6) nor Sunday (0), write a default message:
اگر امروز چه شنبه (۶) باشد یا چه یکشنبه (۰)، یک پیام پیشفرض مینویسد:
switch (new Date().getDay()) {
case 6:
text = "Today is Saturday";
break;
case 0:
text = "Today is Sunday";
break;
default:
text = "Looking forward to the Weekend";
}
The result of text will be:
نتیجه متغیرtext برابر خواهد بود با:
Looking forward to the Weekend
The default
case does not have to be the last case in a switch block:
Case مربوط به کلمه کلیدی Default لزوم نباید آخرین بلاک کد در دستور Switch باشد:
Example
switch (new Date().getDay()) {
default:
text = "Looking forward to the Weekend";
break;
case 6:
text = "Today is Saturday";
break;
case 0:
text = "Today is Sunday";
}
If default
is not the last case in the switch block, remember to end the default case with a break.
اگر بلاک مربوط به کلمه Default آخرین Case در بلاک دستور Switch نباشد، حتماً به یاد داشته باشید که پایان Case عبارت Default از کلمه break استفاده کنید.
Common Code Blocks
بلاکهای کد مشترک
Sometimes you will want different switch cases to use the same code.
برخی اوقات شما میخواهید که Case های مختلفی در دستور Switch از کد مشترک استفاده کنند.
In this example case 4 and 5 share the same code block, and 0 and 6 share another code block:
در این مثالcase شماره ۴ و ۵ دارای بلاک کد مشترک هستند و Case صفر و شش نیز بلاک کد مشترک دیگری دارند:
Example
switch (new Date().getDay()) {
case 4:
case 5:
text = "Soon it is Weekend";
break;
case 0:
case 6:
text = "It is Weekend";
break;
default:
text = "Looking forward to the Weekend";
}
Switching Details
جزئیات عمل Switching
If multiple cases matches a case value, the first case is selected.
اگر چندین Case با مقدار یک Case برابری کند، بلاک اولین Case انتخاب میشود.
If no matching cases are found, the program continues to the default label.
اگر هیچیک از Caseها نیز برقرار نباشد، برنامه بلاک مربوط به کلمه default را اجرا میکند.
If no default label is found, the program continues to the statement(s) after the switch.
اگر دستور Default نوشته نشده باشد، برنامه سایر کدها بعد از دستور Switch را اجرا خواهد کرد.
Strict Comparison
مقایسهٔ سخت گیرانه
Switch cases use strict comparison (===).
دستور Switch از مقایسهٔ سخت گیرانه (===) استفاده میکند.
The values must be of the same type to match.
برای اینکه Case مورد نظر برقرار باشد، نوع و مقدار آن باید برابر باشد.
A strict comparison can only be true if the operands are of the same type.
مقایسه سخت گیرانه در صورتی تنها میتواند true یا برقرار باشد که عملگرها از یک نوع باشند.
In this example there will be no match for x:
در مثال زیر، هیچیک از Case ها برای متغیر x اجرا نخواهند شد:
Example
var x = "۰";
switch (x) {
case 0:
text = "Off";
break;
case 1:
text = "On";
break;
default:
text = "No value found";
}
منابع آموزشی