کلمه Condition بهمعنای شرط و کلمه statement به دستور یا دستوراتی اشاره دارد که بهازای برقرار شدن شرط اجرا میشوند. منظور از برقرار بودن شرط این است که مقدار عبارت Condition برابر True باشد. کلمه else بهمعنای «درغیراینصورت» است. محتوای else زمانی اجرا میشود که شرط if برقرار نباشد. یعنی مقدار عبارت condition برابر False باشد.
شکل ۱۰
ساختار else-if: امکان بررسی چند شرط را بهصورت پیدرپی فراهم میکند. در این مرحله رتبه یک دانشآموز بر اساس نمره کسبشده مشخص میشود. در انتهای اسکریپت قبل، کد روبهرو را بنویسید.
در این اسکریپت، متغیر score با مقدار 70 برای نگهداری نمره دانشآموز تعریف شده است. ابتدا شرط score>=90 و سپس شرط score>=80 بررسی میشود. با توجه به اینکه مقدار هر دو شرط نادرست است، بنابراین دستور console.log() در خطوط 3 و 5 اجرا نمیشود. سپس شرط score>=70 بررسی میشود. با توجه به برقرار بودن شرط، دستور شماره 7 اجرا شده و عبارت Grade: C روی صفحه چاپ میشود. بعد از اجرای دستور 7 شرطهای بعدی بررسی نمیشود و کنترل برنامه به خط 13 منتقل میشود.
صفحه ۲۱۲
Page 211
The word Condition means the condition and the word statement points to the statement or statements that run when the condition holds. The condition holding means that the value of the Condition expression is True. The word else means "otherwise". The content of else runs when the condition of the if does not hold, that is, the value of the condition expression is False.
Figure 10
The else-if structure: makes it possible to check several conditions one after another. In this step the grade of a student is decided based on the score obtained. At the end of the previous script, write the code opposite.
In this script, the variable score is defined with the value 70 to hold the student's score. First the condition score>=90 and then the condition score>=80 are checked. Since both conditions are false, the console.log() statements in lines 3 and 5 do not run. Then the condition score>=70 is checked. Since this condition holds, statement number 7 runs and the phrase Grade: C is printed on the page. After statement 7 runs, the next conditions are not checked and the control of the program moves to line 13.