MATLABMATLAB · Chapterبەش 5 · Lectureوانە 5.3
The switch Statementڕستەی switch
Choose one of many paths by a single value with switch, case and otherwise: only the first match runs (no fall-through), text cases match exactly, a cell array gives several values one case, and fix(mark/10) turns ranges into values — with the lab's day-name, grade and salary programs tested and corrected (100 → F becomes case {9, 10}).
بە switch، case و otherwise بە یەک یەکێک لە چەندین ڕێگا هەڵبژێرە: تەنها یەکەم یەکسانبوون جێبەجێ دەبێت (بێ )، caseـی دەقی تەواو یەکسان دەبن، cell array چەند دەخاتە یەک caseـەوە و fix(mark/10) هەر دەکات بە یەک — لەگەڵ ناوی ڕۆژ، پلە و مووچەی تاقیگە، تاقیکراوە و چاککراو (نمرەی 100 کە F وەردەگرت، بە case {9, 10} چاک دەکرێت).
What you will learnچی فێر دەبیت
- Write a switch with case and otherwise and explain why only the first matching case runs.
- Switch on numbers and on text, using lower for capitals and braces for several values.
- Turn ranges into values with fix and test the boundaries, including the mark 100.
- Decide between switch and if, and avoid comparisons inside a case.
- switchـێک بە case و otherwise بنووسیت و ڕوونی بکەیتەوە بۆچی تەنها یەکەم caseـی یەکسان جێبەجێ دەبێت.
- لەسەر ژمارە و لەسەر دەق switch بکەیت، بە lower بۆ پیتی گەورە و کەوانەی لووتکەدار بۆ چەند .
- بە fix هەر بکەیت بە یەک و سنوورەکان تاقی بکەیتەوە، لەوانە نمرەی 100.
- لە نێوان switch و ifـدا بڕیار بدەیت و خۆت لە بەراورد لەناو caseـدا بپارێزیت.
One value, many pathsیەک نرخ، چەندین ڕێگا
Often a program must choose one of many actions by the value of a single variable: a menu choice, a day number, a grade group. You could write a long if … elseif … elseif … chain, but the says it more clearly. MATLAB compares the value after switch with each case in turn; otherwise catches every value no case matched:
زۆرجار دەبێت بە تەنها یەک یەکێک لە چەندین کار هەڵبژێرێت: هەڵبژاردنێکی ، ژمارەی ڕۆژێک، کۆمەڵەیەکی نمرە. دەتوانیت زنجیرەیەکی درێژی if … elseif … elseif … بنووسیت، بەڵام ڕوونتر دەیڵێت. MATLAB ئەو دوای switch یەک لە دوای یەک لەگەڵ هەر caseـێک بەراورد دەکات؛ otherwise هەموو ئەو دەگرێت کە هیچ case یەکسانیان نەبوو:
switch expression
case value1
% statements for value1
case value2
% statements for value2
otherwise
% statements when nothing matched
endExplain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1
The expression must give one number or one piece of text (MathWorks: a scalar or a character vector).
دەبێت یەک ژمارە یان یەک پارچە دەق بدات (MathWorks: یان character vector).
- Lineدێڕی 6
otherwiseis optional; without it, an unmatched value simply does nothing.otherwiseئارەزوومەندانەیە؛ بەبێ ئەو، کە یەکسانی نەبێت هیچ ناکات.
One value, many pathsیەک نرخ، چەندین ڕێگا
Only the first match runsتەنها یەکەم یەکسانبوون جێبەجێ دەبێت
When a case matches, MATLAB runs its statements and leaves the switch. MathWorks puts it plainly: the MATLAB switch does not fall through like a switch in C. In C, Java or JavaScript, a matching case keeps running into the cases below it unless you write break — that is a famous source of bugs. MATLAB needs no break, and if two cases could match the same value, only the first one ever runs.
کاتێک case یەکسان دەبێت، MATLAB جێبەجێ دەکات و switch جێدەهێڵێت. MathWorks بە ڕوونی دەیڵێت: switchـی MATLAB وەک switch لە C ناکات. لە C، Java یان JavaScriptـدا، caseـێکی یەکسان بەردەوام دەبێت بۆ ناو caseـەکانی خوارەوەی، مەگەر break بنووسیت — ئەو سەرچاوەیەکی ناسراوی . MATLAB پێویستی بە break نییە و ئەگەر دوو case بتوانن یەکسان بن لەگەڵ هەمان ، تەنها یەکەمیان جێبەجێ دەبێت.
n = 2;
switch n
case 2
disp('two')
case {1, 2, 3}
disp('small')
otherwise
disp('other')
endtwo
At a taxi garage the dispatcher shouts the destination and each line of cars waits for one city: Erbil, Duhok, Kirkuk. A passenger walks to the first line whose sign matches and gets in; nobody tries the other lines after that. A passenger for a city with no line goes to the general taxis — that is otherwise.
لە گەراجی تاکسیدا بانگکەرەکە شوێنی مەبەست هاوار دەکات و هەر ڕیزێک ئۆتۆمبێل چاوەڕێی یەک شار دەکات: هەولێر، دهۆک، کەرکووک. سەرنشینێک دەچێت بۆ یەکەم ڕیز کە تابلۆکەی یەکسانە و سوار دەبێت؛ دوای ئەوە کەس ڕیزەکانی تر تاقی ناکاتەوە. سەرنشینێک بۆ شارێک کە ڕیزی نییە دەچێت بۆ تاکسییە گشتییەکان — ئەوە otherwiseـە.
Where the analogy stops workingسنووری ئەم نموونەیە
A switch only compares for equality. A passenger who says “any city more than 100 km away” cannot be matched by a case; that needs an if.
switch تەنها یەکسانبوون بەراورد دەکات. سەرنشینێک کە دەڵێت «هەر شارێک لە 100 کیلۆمەتر دوورتر» بە case یەکسان ناکرێت؛ ئەوە پێویستی بە ifـە.
A number in, a day name outژمارەیەک دێتە ژوورەوە، ناوی ڕۆژێک دەچێتە دەرەوە
A lab question asks for the name of the day from its number, counting Sunday as day 1 (MATLAB's own weekday function counts the same way). Each number is one case; any other number goes to otherwise:
پرسیارێکی تاقیگە ناوی ڕۆژ لە ژمارەکەیەوە دەخوازێت، بە ژماردنی یەکشەممە وەک ڕۆژی 1 ( weekdayـی MATLAB خۆیشی بەم شێوەیە دەژمێرێت). هەر ژمارەیەک یەک caseـە؛ هەر ژمارەیەکی تر دەچێت بۆ otherwise:
day = input('Day number (1-7): ');
switch day
case 1
disp('Sunday')
case 2
disp('Monday')
case 3
disp('Tuesday')
case 4
disp('Wednesday')
case 5
disp('Thursday')
case 6
disp('Friday')
case 7
disp('Saturday')
otherwise
disp('Unknown day number')
endDay number (1-7): 3 Tuesday
| Numberژمارە | Printedچاپ دەکرێت | In Kurdishبە کوردی |
|---|---|---|
| 1 | Sunday | Sundayیەکشەممە |
| 2 | Monday | Mondayدووشەممە |
| 3 | Tuesday | Tuesdayسێشەممە |
| 4 | Wednesday | Wednesdayچوارشەممە |
| 5 | Thursday | Thursdayپێنجشەممە |
| 6 | Friday | Fridayهەینی |
| 7 | Saturday | Saturdayشەممە |
| 9 | Unknown day number | — |
Switching on text: a small calculatorswitch لەسەر دەق: حاسیبەیەکی بچووک
A switch also works with text. For text, MATLAB compares like strcmp, so the match must be exact — and it is : 'Add' does not match case 'add'. Pass the text through lower first and the user may type Add, ADD or add. Here is a four-operation calculator with a text :
switch لەگەڵ دەقیش کار دەکات. بۆ دەق، MATLAB وەک strcmp بەراورد دەکات، بۆیە یەکسانبوون دەبێت تەواو بێت — هەروەها : 'Add' لەگەڵ case 'add' یەکسان نییە. سەرەتا دەقەکە بە lowerـدا تێپەڕێنە و دەتوانێت Add، ADD یان add بنووسێت. ئەمە بۆ چوار کاری ژمێرکاری، بە دەقی:
op = input('Operation (add, sub, mul, div): ', 's');
a = input('First number: ');
b = input('Second number: ');
r = NaN; % no result yet
switch lower(op)
case 'add'
r = a + b;
case 'sub'
r = a - b;
case 'mul'
r = a * b;
case 'div'
if b == 0
disp('Cannot divide by zero')
else
r = a / b;
end
otherwise
disp('Unknown operation')
end
if ~isnan(r)
fprintf('%g %s %g = %g\n', a, op, b, r)
endOperation (add, sub, mul, div): mul First number: 6 Second number: 7 6 mul 7 = 42
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1
The choice is text, so
inputneeds's'.هەڵبژاردنەکە دەقە، بۆیە
inputپێویستی بە's'ـە.- Lineدێڕی 4
NaNmarks “no result yet”; it is tested withisnanat the end.NaNنیشانەی «هێشتا ئەنجام نییە»یە؛ لە کۆتاییدا بەisnanدەپشکنرێت.- Lineدێڕی 13-17
An
ifinside a case: a switch and an if work well together.ifـێک لەناو caseـێکدا: switch و if پێکەوە باش کار دەکەن.
Operation (add, sub, mul, div): div First number: 5 Second number: 0 Cannot divide by zero
Switching on text: a small calculatorswitch لەسەر دەق: حاسیبەیەکی بچووک
Several values in one caseچەند نرخێک لە یەک caseـدا
To give several values the same action, list them in curly braces: case {6, 7} matches 6 or 7, and case {'sat', 'sun'} matches either text. The braces build what MATLAB calls a cell array; MathWorks says a cell-array case matches when any one of its elements matches. In the Kurdistan Region the weekend is Friday and Saturday — days 6 and 7 in the lab's numbering:
بۆ ئەوەی چەند هەمان کار وەربگرن، لەناو کەوانەی لووتکەداردا لیستیان بکە: case {6, 7} لەگەڵ 6 یان 7 یەکسان دەبێت و case {'sat', 'sun'} لەگەڵ هەر یەکێک لە دوو دەقەکە. کەوانەکان ئەوە دروست دەکەن کە MATLAB پێی دەڵێت cell array؛ MathWorks دەڵێت caseـێکی cell array کاتێک یەکسان دەبێت کە هەر یەکێک لە یەکسان بێت. لە هەرێمی کوردستاندا پشووی کۆتایی هەفتە هەینی و شەممەیە — ڕۆژەکانی 6 و 7 بە ژماردنی تاقیگەکە:
day = input('Day number (1-7): ');
switch day
case {1, 2, 3, 4, 5}
disp('Working day')
case {6, 7}
disp('Weekend')
otherwise
disp('Unknown day number')
endDay number (1-7): 6 Weekend
From a mark to a grade with fixلە نمرەوە بۆ پلە بە fix
A switch compares equal values, but marks come in ranges: 90–100 is A, 80–89 is B, and so on. The lab's trick turns each range into one value: fix(mark/10) divides by 10 and cuts off the decimals (fix rounds toward zero), so and . Each group of ten marks becomes one whole number, and that number can be a case. Here is the lab's program:
switch بەراورد دەکات، بەڵام پلەکان بەپێی دەدرێن: 90–100 دەبێتە A، 80–89 دەبێتە B و هتد. فێڵەکەی تاقیگە هەر دەکات بە یەک : fix(mark/10) بەسەر 10دا دابەش دەکات و ژمارەکانی دوای دەبڕێت (fix بەرەو سفر نزیک دەکاتەوە)، بۆیە و . هەر کۆمەڵەیەکی دە نمرەیی دەبێتە یەک و ئەو ژمارەیە دەتوانێت case بێت. ئەمە تاقیگەکەیە:
a = input('enter your mark:');
m = fix(a/10);
switch (m)
case 9
disp('A')
case 8
disp('B')
case 7
disp('C')
case 6
disp('D')
case 5
disp('E')
otherwise
disp('F')
endenter your mark:80 B
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
fix(80/10)is 8.fix(80/10)دەکاتە 8.- Lineدێڕی 3
Brackets around the switch value are allowed but not needed.
کەوانە لە دەوری switch ڕێگەپێدراوە بەڵام پێویست نییە.
Watch four marks go through it. Three of them land correctly; the fourth, a perfect 100, does not:
سەیری چوار نمرە بکە کە پێیدا تێدەپەڕن. سێیان بە دروستی دەگەن؛ چوارەمیان، 100ـی تەواو، ناگات:


From a mark to a grade with fixلە نمرەوە بۆ پلە بە fix
Two lab versions, one shared bugدوو وەشانی تاقیگە، یەک باگی هاوبەش
| Markنمرە | fix(mark/10) | lab program تاقیگە | Q3 versionوەشانی Q3 | correctedچاککراو |
|---|---|---|---|---|
| 95 | 9 | A | a | A |
| 80 | 8 | B | b | B |
| 79.5 | 7 | C | c | C |
| 59 | 5 | E | un known | E |
| 100 | 10 | F | un known | A |
| 49.5 | 4 | F | un known | F |
| 0 | 0 | F | un known | F |
0 | F | un known | Invalid mark | |
| 105 | 10 | F | un known | Invalid mark |
A second lab question (Q3) does the same with lower-case letters 'a' to 'd' and puts every mark below 60 into otherwise, which prints 'un known' — a strange message for a failing mark, and its variable name stavarege and prompt “give me the rank of student” do not say what is typed. Both versions share the real bug in the table: a perfect 100 gets no A — F in the lab program, 'un known' in Q3 — because fix(100/10) is 10 and no case is 10. Both also accept impossible marks such as or 105.
پرسیارێکی تری تاقیگە (Q3) هەمان کار بە پیتی بچووکی 'a' تا 'd' دەکات و هەموو نمرەیەکی خوار 60 دەخاتە otherwise، کە 'un known' چاپ دەکات — پەیامێکی سەیر بۆ نمرەیەکی دەرنەچوو. ناوی stavarege و «give me the rank of student» نالێن چی دەنووسرێت. هەردوو وەشانەکە هەمان ڕاستەقینەی ناو خشتەکەیان تێدایە: نمرەی تەواوی 100 پلەی A وەرناگرێت — لە تاقیگەدا F و لە Q3ـدا 'un known' — چونکە fix(100/10) دەکاتە 10 و هیچ case نییە بۆ 10. هەردووکیان نمرەی مەحاڵی وەک یان 105ـیش قبووڵ دەکەن.
From a mark to a grade with fixلە نمرەوە بۆ پلە بە fix
One corrected grade programبەرنامەیەکی چاککراو بۆ پلە
One corrected program replaces both lab versions. It first rejects marks outside 0–100 with an if (a switch cannot test a range), then switches on fix(mark/10) with case {9, 10} for A, and prints a clear sentence:
چاککراو جێگەی هەردوو وەشانەکەی تاقیگە دەگرێتەوە. سەرەتا بە if نمرەکانی دەرەوەی 0–100 ڕەت دەکاتەوە (switch ناتوانێت بپشکنێت)، پاشان لەسەر fix(mark/10) switch دەکات بە case {9, 10} بۆ A و ڕستەیەکی ڕوون چاپ دەکات:
% grade.m - letter grade from a mark between 0 and 100
mark = input('Mark (0-100): ');
if mark < 0 || mark > 100
disp('Invalid mark')
else
switch fix(mark/10)
case {9, 10}
grade = 'A';
case 8
grade = 'B';
case 7
grade = 'C';
case 6
grade = 'D';
case 5
grade = 'E';
otherwise
grade = 'F';
end
fprintf('Mark %g gives grade %s\n', mark, grade)
endMark (0-100): 79.5 Mark 79.5 gives grade C
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3
||because each side is one true/false value.||چونکە هەر لایەک یەک ڕاست/هەڵەیە.- Lineدێڕی 7
100 → group 10 → A.
100 دەچێتە کۆمەڵەی 10 و A وەردەگرێت.
- Lineدێڕی 20
%gprints 79.5 without extra zeros.%gژمارەی 79.5 بێ سفری زیادە چاپ دەکات.
Test at the boundaries, where mistakes hide: 100, 90, 89.9, 50, 49.9 and 0, plus one impossible mark on each side. The Python track writes the same job with if/elif (a clean grade program).
لە سنوورەکاندا تاقی بکەرەوە، کە هەڵەکان لەوێ خۆیان دەشارنەوە: 100، 90، 89.9، 50، 49.9 و 0، لەگەڵ نمرەیەکی مەحاڵ لە هەر لایەک. بەشی Python هەمان کار بە if/elif دەنووسێت (بەرنامەیەکی پلەی ڕێکوپێک).
Another range trick: salary classesفێڵێکی تری ماوەکان: پۆلەکانی مووچە
A third lab program puts a monthly salary into a class with the same idea: fix(s/100) turns 100–199 into 1, 200–299 into 2, and so on. Here the salary is in thousands of dinars, so 150 means 150,000 IQD:
سێیەم تاقیگە بە هەمان بیرۆکە مووچەی مانگانە دەخاتە پۆلێکەوە: fix(s/100) 100–199 دەکات بە 1، 200–299 بە 2 و هتد. لێرەدا مووچەکە بە هەزار دینارە، بۆیە 150 واتە 150,000 دینار:
s = input('Monthly salary (thousand IQD): ');
switch fix(s/100)
case 1
disp('C')
case 2
disp('B')
case 3
disp('A')
case 4
disp('A+')
otherwise
disp('undefined')
endMonthly salary (thousand IQD): 250 B
| Salaryمووچە | fix(s/100) | Classپۆل |
|---|---|---|
| 150 | 1 | C |
| 250 | 2 | B |
| 399 | 3 | A |
| 450 | 4 | A+ |
| 99 | 0 | undefined |
| 500 | 5 | undefined |
| 100 | 1 | C |
| 499 | 4 | A+ |
The test values were chosen well: 399 and 450 sit near the top of their groups, and 99 and 500 lie just outside the defined range, so they go to otherwise. Adding 100 and 499 tests the exact edges. The program does not check for negative salaries; that would again need an if before the switch.
تاقیکردنەوە بە باشی هەڵبژێردراون: 399 و 450 لە نزیک سەرەوەی کۆمەڵەکانیانن. 99 و 500ـیش ڕێک دەکەونە دەرەوەی ، بۆیە دەچن بۆ otherwise. زیادکردنی 100 و 499 لێوارە ڕێکەکان تاقی دەکاتەوە. مووچەی سالب ناپشکنێت؛ ئەوە دووبارە پێویستی بە ifـێکە پێش switch.
switch or if?switch یان if؟
Use switch when one value is compared with a list of fixed values — numbers or texts. Use if when you need ranges, several variables, or conditions such as x > 0 && y > 0. A tempting mistake is to put a comparison into a case. MathWorks advises against relational operators such as < or > in a case expression, and the example shows why:
switch بەکار بهێنە کاتێک یەک لەگەڵ چەند جێگیر بەراورد دەکرێت — ژمارە یان دەق. if بەکار بهێنە کاتێک ، چەند یان وەک x > 0 && y > 0ـت پێویستە. هەڵەیەکی سەرنجڕاکێش ئەوەیە بەراوردێک بخەیتە ناو caseـەوە. MathWorks ئامۆژگاری دەکات کە وەک < یان > لە caseـدا بەکار نەهێنرێن و نموونەکە پیشان دەدات بۆچی:
mark = 95;
switch mark
case mark >= 90
disp('A')
otherwise
disp('F')
endF
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3
mark >= 90is worked out first and gives 1. MATLAB then asks: is 95 equal to 1? No — sootherwiseprints F.سەرەتا
mark >= 90هەژمار دەکرێت و 1 دەدات. پاشان MATLAB دەپرسێت: ئایا 95 یەکسانە بە 1؟ نەخێر — بۆیەotherwiseF چاپ دەکات.
| Featureتایبەتمەندی | switch | if / elseif |
|---|---|---|
| comparesبەراورد دەکات | one value for equalityیەک بۆ یەکسانبوون | any conditionهەر |
| ranges | only with a trick such as fix(mark/10)تەنها بە فێڵێکی وەک fix(mark/10) | directly: mark >= 90ڕاستەوخۆ: mark >= 90 |
| textدەق | exact match, like strcmpیەکسانبوونی تەواو، وەک strcmp | needs strcmpپێویستی بە strcmpـە |
| several valuesچەند | case {6, 7}case {6, 7} | d == 6 || d == 7d == 6 || d == 7 |
Common mistakesهەڵە باوەکان
| Mistakeهەڵە | What happensچی ڕوودەدات | Fixچارەسەر |
|---|---|---|
Expecting fall-through (C, Java habit) and adding breakچاوەڕوانیی (ڕاهاتنی C و Java) و زیادکردنی break | Not needed: only the first matching case runs. (break belongs to loops.)پێویست نییە: تەنها یەکەم caseـی یەکسان جێبەجێ دەبێت. (break سەر بە .) | Leave break out of a switch.break لە switch لاببە. |
case 9, 10 without bracescase 9, 10 بەبێ کەوانە | Only 9 matches; 10 becomes a statement that prints ans = 10.تەنها 9 یەکسان دەبێت؛ 10 دەبێتە کە ans = 10 چاپ دەکات. | case {9, 10}case {9, 10} |
case mark >= 90case mark >= 90 | Compares the switch value with 0 or 1. switch لەگەڵ 0 یان 1 بەراورد دەکات. | Use if, or a value trick such as fix(mark/10).if بەکار بهێنە، یان فێڵێکی وەک fix(mark/10). |
Misspelt or wrongly cased text cases: case 'Add' for input addcaseـی دەقی بە ڕێنووس یان قەبارەی پیتی هەڵە: case 'Add' بۆ add | No match; otherwise runs.یەکسانبوون نییە؛ otherwise جێبەجێ دەبێت. | switch lower(op) and lower-case cases.switch lower(op) و caseـی پیت بچووک. |
No otherwiseotherwise نییە | Unexpected values pass silently. بە بێدەنگی تێدەپەڕن. | Add an otherwise with a clear message.otherwiseـێک بە پەیامێکی ڕوون زیاد بکە. |
| Forgetting the 100-mark caseلەبیرچوونی caseـی نمرەی 100 | 100 → F.100 دەبێتە F. | case {9, 10}, and test the boundaries.case {9, 10} و سنوورەکان تاقی بکەرەوە. |
A switch value that is a vector, e.g. switch [1 2] switch کە ، بۆ نموونە switch [1 2] | An error: the value must be one number or one text.: دەبێت یەک ژمارە یان یەک دەق بێت. | Switch on one element, or use if with any/all.لەسەر یەک switch بکە، یان if لەگەڵ any/all بەکار بهێنە. |
Practiceڕاهێنان
Read a month number and print how many days it has (February: 28). Use cell-array cases for the 30-day and 31-day months.
ژمارەی مانگێک بخوێنەوە و چاپ بکە چەند ڕۆژی هەیە (شوبات: 28). بۆ مانگە 30 و 31 ڕۆژییەکان caseـی cell array بەکار بهێنە.
Hintئاماژە
Three groups: {2}, {4, 6, 9, 11} and the seven 31-day months.
سێ کۆمەڵە: {2}، {4، 6، 9، 11} و حەوت مانگە 31 ڕۆژییەکە.
Show answerوەڵامەکە پیشان بدە
m = input('Month number (1-12): ');
switch m
case 2
days = 28;
case {4, 6, 9, 11}
days = 30;
case {1, 3, 5, 7, 8, 10, 12}
days = 31;
otherwise
days = 0;
end
if days == 0
disp('Unknown month')
else
fprintf('Month %d has %d days\n', m, days)
endMonth number (1-12): 9 Month 9 has 30 days
In a leap year February has 29 days — combine this program with the leap-year test of the conditions lesson.
لە ساڵی کەبیسەدا شوبات 29 ڕۆژە — ئەم لەگەڵ پشکنینی ساڵی کەبیسەی وانەی مەرجەکان تێکەڵ بکە.
Read one English letter as text and print whether it is a vowel (a, e, i, o, u) or not. Accept capital letters too.
یەک پیتی ئینگلیزی وەک دەق بخوێنەوە و چاپ بکە کە بزوێنە (a، e، i، o، u) یان نا. پیتی گەورەش قبووڵ بکە.
Show answerوەڵامەکە پیشان بدە
c = input('One letter: ', 's');
switch lower(c)
case {'a', 'e', 'i', 'o', 'u'}
disp('vowel')
otherwise
disp('not a vowel')
endOne letter: E vowel
For the corrected grade.m, what is printed for 50, 49.9, 100 and 101?
بۆ grade.mـی چاککراو، بۆ 50، 49.9، 100 و 101 چی چاپ دەکرێت؟
Show answerوەڵامەکە پیشان بدە
50 → Mark 50 gives grade E; 49.9 → Mark 49.9 gives grade F (fix(4.99) is 4); 100 → Mark 100 gives grade A; 101 → Invalid mark.
بۆ 50: Mark 50 gives grade E؛ بۆ 49.9: Mark 49.9 gives grade F (fix(4.99) دەکاتە 4)؛ بۆ 100: Mark 100 gives grade A؛ بۆ 101: Invalid mark.
Rewrite with a switch: if c == 1 disp('Erbil'), elseif c == 2 disp('Sulaymaniyah'), elseif c == 3 disp('Duhok'), else disp('other city'), end
بە switch دووبارە بینووسەوە: if c == 1 disp('Erbil'), elseif c == 2 disp('Sulaymaniyah'), elseif c == 3 disp('Duhok'), else disp('other city'), end
Show answerوەڵامەکە پیشان بدە
switch c
case 1
disp('Erbil')
case 2
disp('Sulaymaniyah')
case 3
disp('Duhok')
otherwise
disp('other city')
endWhich class do the salaries 199, 200 and 400 get in the salary program? Which salaries between 0 and 600 are undefined?
مووچەکانی 199، 200 و 400 لە مووچەدا کام پۆل وەردەگرن؟ کام مووچانە لە نێوان 0 و 600دا undefinedن؟
Show answerوەڵامەکە پیشان بدە
199 → C (fix(1.99) is 1); 200 → B; 400 → A+. Undefined: every salary below 100 and every salary of 500 or more.
199 دەبێتە C (fix(1.99) دەکاتە 1)؛ 200 دەبێتە B؛ 400 دەبێتە A+. undefined: هەموو مووچەیەکی خوار 100 و هەموو مووچەیەکی 500 و سەرووتر.
Summaryکورتە
switchcompares one value (a number or a text) with eachcasefrom the top; the first match runs and the switch ends — no fall-through, nobreakneeded.otherwisecatches unmatched values; it is optional but good practice.- Text cases match exactly and are case-sensitive;
switch lower(op)accepts any capitals. case {6, 7}(a cell array) gives several values one action;case 6, 7does not.fix(mark/10)turns ranges into values; remembercase {9, 10}for 100 and reject impossible marks with aniffirst.- Ranges and combined conditions belong to
if; never write a comparison inside a case.
switchیەک (ژمارە یان دەق) لە سەرەوە لەگەڵ هەرcaseـێک بەراورد دەکات؛ یەکەم یەکسانبوون جێبەجێ دەبێت و switch کۆتایی دێت — بێ ، بێ پێویستی بەbreak.otherwiseئەو یەکسان نین دەگرێت؛ ئارەزوومەندانەیە بەڵام ڕاهاتنێکی باشە.- caseـی دەقی تەواو یەکسان دەبن و ؛
switch lower(op)هەر قەبارەیەکی پیت قبووڵ دەکات. case {6, 7}(cell array) چەند یەک کار دەدات؛case 6, 7وا ناکات.fix(mark/10)هەر دەکات بە یەک ؛case {9, 10}بۆ 100 لە بیر بێت و سەرەتا بەifنمرەی مەحاڵ ڕەت بکەرەوە.- و سەر بە
ifن؛ هەرگیز بەراورد لەناو caseـدا مەنووسە.
What does MATLAB print for switch 3, case {1, 3}, disp('odd'), case 3, disp('three'), end?MATLAB بۆ switch 3, case {1, 3}, disp('odd'), case 3, disp('three'), end چی چاپ دەکات؟
Only odd: the first matching case runs and the switch ends.
تەنها odd: یەکەم caseـی یەکسان جێبەجێ دەبێت و switch کۆتایی دێت.
Why reject marks outside 0–100 before the switch?بۆچی پێش switch نمرەکانی دەرەوەی 0–100 ڕەت دەکەینەوە؟
A switch cannot test ranges, and fix(105/10) is 10, which would wrongly give A.
switch ناتوانێت بپشکنێت و fix(105/10) دەکاتە 10، کە بە هەڵە A دەدات.
How do you make a text switch accept Yes, YES and yes?چۆن switchـێکی دەقی وا لێدەکەیت Yes، YES و yes قبووڵ بکات؟
Switch on lower(answer) and write the case in lower case: case 'yes'.
لەسەر lower(answer) switch بکە و caseـەکە بە پیتی بچووک بنووسە: case 'yes'.
Lesson complete!وانەکە تەواو بوو!
Well done. Review the key terms, then go on to the next lesson.دەستخۆش. سەیرێکی زاراوە سەرەکییەکان بکەرەوە، پاشان بڕۆ بۆ وانەی داهاتوو.
Key termsزاراوە سەرەکییەکان
for Loops: Sums, Series and Searchingخولەکانی for: کۆکردنەوە، زنجیرە و گەڕان
Related lecturesوانە پەیوەندیدارەکان
- Script Files, input, disp and fprintfفایلەکانی دەسنووس، input، disp و fprintf
- for Loops: Sums, Series and Searchingخولەکانی for: کۆکردنەوە، زنجیرە و گەڕان
- Algorithms, Pseudocode and Flowchartsئەلگۆریزمەکان، نیمچەکۆد و هێڵکارییەکانی ڕەوت
- Making Decisions: if, elif, elseبڕیاردان:
if،elifوelse - Logical Operators and Nested ifکارپێکەرە لۆجیکییەکان و ڕستەی ifـی ناوەکی
References and further readingسەرچاوەکان و خوێندنەوەی زیاتر
This lecture is a first edition. The Kurdish text and terminology are waiting for review by Kurdish IT teachers.ئەم وانەیە چاپی یەکەمە. دەقە کوردییەکە و زاراوەکانی چاوەڕێی پێداچوونەوەی مامۆستایانی کوردی بواری IT ن.