MATLABMATLAB · Chapterبەش 5 · Lectureوانە 5.2

Making Decisions: Relational Operators and if / elseif / elseبڕیاردان: کارپێکەرەکانی بەراوردکردن و ⁦if / elseif / else⁩

Compare values with == ~= < <= > >=, get logical arrays from whole vectors and pick elements with logical indexing, find and any/all, combine conditions with & | ~ xor and the short-circuit && ||, write if, if-else and if-elseif-else blocks (elseif is one word), and debug the lab's quadratic-equation solver.

بە == ~= ⁦< <= > >⁩= بەراورد بکە، لە تەواو وەربگرە و بە ، find و ⁦any/all⁩ هەڵبژێرە، بە & | ~ xor و && ||ـی کورتبڕ پێکەوە ببەستە، بلۆکەکانی if، ⁦if-else⁩ و ⁦if-elseif-else⁩ بنووسە (elseif یەک وشەیە) و هەڵەکانی چارەسەرکەری تاقیگەکە بدۆزەرەوە.

20 min read21 خولەک خوێندنەوە · 17 stepsهەنگاو

What you will learnچی فێر دەبیت

  • Use the six relational operators and read their logical answers.
  • Compare whole vectors, select elements with logical indexing and use find, any, all, isnan, isinf, isfinite, isempty and isreal.
  • Combine conditions with & | ~ xor and with the short-circuit && and ||.
  • Write if, if-else, if-elseif-else and nested if blocks, and explain why elseif is one word.
  • Find and fix the mistakes of a quadratic-equation solver with well-chosen test data.
  • شەش بەکار بهێنیت و وەڵامە لۆجیکییەکانیان بخوێنیتەوە.
  • تەواو بەراورد بکەیت، بە هەڵبژێریت و find، any، all، isnan، isinf، isfinite، isempty و isreal بەکار بهێنیت.
  • بە & | ~ xor و بە && و ||ـی کورتبڕ پێکەوە ببەستیت.
  • بلۆکەکانی if، ⁦if-else⁩، ⁦if-elseif-else⁩ و بنووسیت و ڕوونی بکەیتەوە بۆچی elseif یەک وشەیە.
  • هەڵەکانی چارەسەرکەرێکی بە باش هەڵبژێردراو بدۆزیتەوە و چاکیان بکەیتەوە.

Comparing values: relational operatorsبەراوردکردنی نرخەکان: کارپێکەرەکانی بەراوردکردن

A program decides by asking yes/no questions: is the mark at least 50? is the divisor zero? In MATLAB you ask with a (MathWorks calls them relational operators). The answer is a logical value: 1 for true, 0 for false. MATLAB shows it with the word logical above it.

بە پرسیاری بەڵێ/نەخێر بڕیار دەدات: ئایا نمرەکە لانیکەم ⁦50⁩ـە؟ ئایا دابەشکەرەکە سفرە؟ لە MATLABـدا بە دەپرسیت (MathWorks پێیان دەڵێت ⁦relational operators⁩). وەڵامەکە لۆجیکییە: 1 بۆ ڕاست، 0 بۆ هەڵە. MATLAB بە وشەی logical لە سەرەوەی پیشانی دەدات.

The six relational operatorsشەش
OperatorAsksدەپرسێتExampleنموونەAnswerوەڵام
==equal to?یەکسانە؟7 == 71
~=not equal to?یەکسان نییە؟4 ~= 40
<less than?بچووکترە؟3 < 81
>greater than?گەورەترە؟3 > 80
<=less than or equal to?بچووکترە یان یەکسانە؟50 <= 501
>=greater than or equal to?گەورەترە یان یەکسانە؟49.5 >= 500
Each comparison answers 1 or 0هەر بەراوردێک 1 یان 0 دەداتەوەMATLABType in a terminalلە تێرمیناڵدا بینووسە
5 > 3
7 == 7.0
4 ~= 4

Two warnings. First, = stores a value and == compares — x = 5 gives x the value 5, x == 5 asks a question (“=” in algebra and in programming). Second, not equal is ~= in MATLAB. Octave also accepts != (its manual lists both), but write ~= so your code runs in both programs.

دوو ئاگادارکردنەوە. یەکەم، = هەڵدەگرێت و == بەراورد دەکات — x = 5 5 دەدات بە x، x == 5 پرسیارێک دەکات («=» لە جەبر و لە بەرنامەسازیدا). دووەم، یەکسان نییە لە MATLABـدا ~=ـە. Octave !=ـیش قبووڵ دەکات (ڕێبەرەکەی هەردووکیان ناو دەبات)، بەڵام ~= بنووسە تاکو کۆدەکەت لە هەردوو بەرنامەکەدا کار بکات.

Quick checkپشکنینی خێراWhat does 10 >= 10 give?10 >= 10 چی دەدات؟

Comparing values: relational operatorsبەراوردکردنی نرخەکان: کارپێکەرەکانی بەراوردکردن

Comparing a whole vectorبەراوردکردنی ئاڕاستەبڕێکی تەواو

Because every value in MATLAB is an array, a comparison works element by element. v > 5 compares each element of v with 5 and gives a of the same size: a row of 1s and 0s, sometimes called a mask. Used as an index, that mask picks out the elements where it has a 1 — this is :

لەبەر ئەوەی هەموو لە MATLABـدا ، بەراوردکردن بە کار دەکات. v > 5 هەر v لەگەڵ 5 بەراورد دەکات و بە هەمان قەبارە دەدات: ڕیزێک لە 1 و 0، کە هەندێک جار پێی دەڵێن ماسک. ئەگەر وەک بەکار بێت، ئەو ماسکە ئەو هەڵدەبژێرێت کە 1ـی بەرامبەرە — ئەمە :

Three rows of boxes. Row v holds 3, 8, 1, 9, 6 in positions 1 to 5. Row v > 5 holds 0, 1, 0, 1, 1: each element was compared with 5. Arrows go from the three 1s down to the result v(v > 5), which holds 8, 9, 6 — the elements where the mask is 1.سێ ڕیز لە چوارگۆشە. ڕیزی v ژمارەکانی 3، 8، 1، 9 و 6 لە شوێنەکانی 1 تا 5دا هەڵدەگرێت. ڕیزی v > 5 ژمارەکانی 0، 1، 0، 1 و 1 هەڵدەگرێت: هەر توخمێک لەگەڵ 5 بەراورد کراوە. تیرەکان لە سێ 1ـەکەوە دەچنە خوارەوە بۆ ئەنجامی v(v > 5) کە 8، 9 و 6 هەڵدەگرێت — ئەو توخمانەی ماسکەکەیان 1ـە.
Logical indexing: a comparison makes a mask, the mask picks the elements: بەراوردکردن ماسکێک دروست دەکات و ماسکەکە هەڵدەبژێرێتCêIT
A mask, the chosen elements and their positionsماسک، توخمە هەڵبژێردراوەکان و شوێنەکانیانMATLABType in a terminalلە تێرمیناڵدا بینووسە
v = [3 8 1 9 6];
mask = v > 5
big = v(v > 5)
pos = find(v > 5)
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 3

v(mask) keeps the elements where the mask is 1.

v(mask) ئەو دەهێڵێتەوە کە ماسکەکە بۆیان 1ـە.

Lineدێڕی 4

find gives the positions of the 1s instead of the values.

find لە جیاتی شوێنی 1ـەکان دەدات.

any, all, counting and replacingany، all، ژماردن و گۆڕینMATLABType in a terminalلە تێرمیناڵدا بینووسە
any(v > 8)
all(v > 0)
count = sum(v > 5)
v(v > 5) = 0
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 1

any: is at least one element true? (9 > 8, so yes.)

any: ئایا لانیکەم یەک ڕاستە؟ (⁦9 > 8⁩، بۆیە بەڵێ.)

Lineدێڕی 2

all: are all elements true?

all: ئایا هەموو ڕاستن؟

Lineدێڕی 3

Adding up a mask counts its 1s: 3 elements are above 5. (sum is taught in functions for arrays.)

کۆکردنەوەی ماسکێک 1ـەکانی دەژمێرێت: سێ لە 5 گەورەترن. (sum لە کردارەکانی ڕیزەکدا دەخوێنرێت.)

Lineدێڕی 4

Logical indexing on the left of = replaces the chosen elements.

لە لای چەپی = دەگۆڕێت.

The comparison needs two arrays of the same (or compatible) size, or an array and one number. Indexing itself — v(3), v(2:4), end — is taught in indexing.

بەراوردکردنەکە پێویستی بە دوو بە هەمان قەبارە (یان قەبارەی گونجاو) هەیە، یان و یەک ژمارە. خودی — v(3)، v(2:4)، end — لە وانەی ئیندێکسەکاندا دەخوێنرێت.

Quick checkپشکنینی خێراFor m = [45 72 50 38 91], what does m(m >= 50) give?بۆ m = [45 72 50 38 91]، m(m >= 50) چی دەدات؟

MATLAB Crash Course for BeginnersfreeCodeCamp.org · Englishئینگلیزی · 1.7M viewsبینین · 37K likesلایک · Partبەشی 1:05:29–1:20:24 (ofلە 1:57:45)

A MATLAB beginner course by Phil Parisi: the Command Window, variables, vectors and matrices, indexing, scripts, sections and naming rules, each in short parts.

کۆرسێکی سەرەتایی MATLAB لەلایەن ⁦Phil Parisi: Command Window⁩، ، و ، ، ، بەشەکان و یاساکانی ناونان، هەر یەکەیان بە بەشێکی کورت.

Chapter 'Example 3 - Logic' (1:05:29-1:20:24): single comparisons such as 55 ~= 20 give logical 1 or 0, then y > 0.8 on a million sine values gives a logical array and sum counts its 1s (25.74 %).

بەشی Example 3 - Logic (لە ⁦1:05:29⁩ تا ⁦1:20:24⁩): بەراوردی تاک، وەک 55 ~= 20، ئەنجامی لۆجیکیی 1 یان 0 دەدات، پاشان y > 0.8 لەسەر ملیۆنێک دەدات و sum ژمارەی 1ـەکانی دەژمێرێت (⁦25.74⁩٪).

Comparing values: relational operatorsبەراوردکردنی نرخەکان: کارپێکەرەکانی بەراوردکردن

Test functions: isnan, isinf, isfinite, isempty, isrealکردارەکانی پشکنین: isnan، isinf، isfinite، isempty، isreal

Some questions cannot be asked with an operator. NaN (not a number, the result of 0/0) is not equal to anything — MathWorks notes that NaN is not even equal to another NaN — so x == NaN is always false. The commands list names test functions for such cases; each answers with 1 or 0 for every element:

هەندێک پرسیار بە ناکرێن. NaN (ژمارە نییە، ئەنجامی 0/0) یەکسان نییە بە هیچ شتێک — MathWorks دەڵێت NaN تەنانەت یەکسان نییە بە NaNـێکی تر — بۆیە x == NaN هەمیشە هەڵەیە. لیستی پشکنین بۆ ئەم حاڵەتانە ناو دەبات؛ هەریەکەیان بۆ هەر 1 یان 0 دەداتەوە:

Logical test functions from the commands list پشکنینی لۆجیکی لە لیستی
FunctionTrue whenکەی ڕاستە
isnan(x)the element is NaN NaNـە
isinf(x)the element is Inf or -Inf Inf یان -Infـە
isfinite(x)the element is an ordinary number (not NaN, not ±Inf) ژمارەیەکی ئاساییە (نە NaN، نە ±Inf)
isempty(x)x has no elements at all, like []x هیچ نییە، وەک []
isreal(x)x has no imaginary partx بەشی خەیاڵیی نییە
The tests on four valuesپشکنینەکان لەسەر چوار نرخMATLABType in a terminalلە تێرمیناڵدا بینووسە
x = [1 -Inf NaN 4];
isnan(x)
isinf(x)
isfinite(x)
isempty([])
isreal(3 + 4i)
NaN == NaN
Quick checkپشکنینی خێراA user pressed only Enter at x = input('Number: '), so x is []. Which test notices this? لە x = input('Number: ')ـدا تەنها Enterـی داگرت، بۆیە x بەتاڵە []. کام پشکنین ئەمە دەدۆزێتەوە؟

Combining conditions: logical operatorsپێکەوەبەستنی مەرجەکان: کارپێکەرە لۆجیکییەکان

To ask two things at once, join comparisons with a . They follow the truth tables you know from logic: AND is true only when both sides are true (AND), OR when at least one is (OR), NOT turns true into false, and XOR is true when exactly one side is true.

بۆ پرسینی دوو شت بەیەکەوە، بەراوردەکان بە پێکەوە ببەستە. ئەمانە خشتەکانی ڕاستی دەگرنە بەر کە لە لۆجیکدا دەیانناسیت: AND تەنها کاتێک ڕاستە کە هەردوو لا ڕاست بن (AND)، OR کاتێک لانیکەم یەکێکیان ڕاست بێت (OR)، NOT ڕاست دەکات بە هەڵە و XOR کاتێک ڕاستە کە ڕێک یەک لا ڕاست بێت.

Logical operators
OperatorMeaningواتاUse it forبۆ چی بەکاری بهێنە
&AND, element by elementAND، بە arrays and masks: v > 2 & v < 9 و ماسکەکان: v > 2 & v < 9
|OR, element by elementOR، بە arrays and masks و ماسکەکان
~NOT: turns true into falseNOT: ڕاست دەکات بە هەڵە و هەڵە بە ڕاست~isempty(x), ~(a > b)
xor(a, b)exclusive OR (a function)⁦XOR: OR⁩ـی تایبەت ()“one or the other, not both”«یەکێکیان، نەک هەردووکیان»
&&AND with short-circuitAND بە conditions of if and while (single true/false values) if و while (یەک ڕاست/هەڵە)
||OR with short-circuitOR بە conditions of if and while if و while
Element by element, and a short-circuit testتوخم بە توخم، هەروەها پشکنینێکی کورتبڕMATLABType in a terminalلە تێرمیناڵدا بینووسە
a = [1 0 1 0]; b = [1 1 0 0];
a & b
a | b
~a
xor(a, b)
x = 7;
x > 0 && x < 10
Quick checkپشکنینی خێراWhat does [1 0 1] | [0 0 1] give?[1 0 1] | [0 0 1] چی دەدات؟

Combining conditions: logical operatorsپێکەوەبەستنی مەرجەکان: کارپێکەرە لۆجیکییەکان

Short-circuit && and ||, and bitwise functions&& و ||ـی کورتبڕ، هەروەها کردارەکانی bitwise

Short-circuit means MATLAB stops as soon as the answer is known: in p && q, if p is false, q is never looked at. That makes tests like ~isempty(v) && v(1) > 0 safe — when v is empty, MATLAB never tries to read v(1). MathWorks recommends && and || in the conditions of if and while. Precedence matters too: comparisons come before &, & before |, and ~ binds tighter than a comparison, so write ~(a > b) with brackets.

واتە MATLAB هەر کە وەڵامەکە زانرا دەوەستێت: لە p && qـدا ئەگەر p هەڵە بێت، هەرگیز سەیری q ناکرێت. ئەمە پشکنینی وەک ~isempty(v) && v(1) > 0 سەلامەت دەکات — کاتێک v بەتاڵە، MATLAB هەرگیز هەوڵی خوێندنەوەی v(1) نادات. MathWorks && و || بۆ if و while پێشنیار دەکات. گرنگە: بەراوردەکان پێش & دێن و & پێش | دێت. هەروەها ~ لە بەراورد بەهێزترە، بۆیە ~(a > b) بە کەوانەوە بنووسە.

The Kurdish guide of the course also mentions bitwise operations. Those work on the binary digits of whole numbers and use functions: bitand(12, 10) is 8 (in binary 1100∧1010=10001100 \land 1010 = 1000), bitor(12, 10) is 14 and bitxor(12, 10) is 6. They are not the same as & and |, which only ask true or false.

ڕێبەرە کوردییەکەی وانەکە باسی کارەکانی bitwiseـیش دەکات. ئەوانە لەسەر کار دەکەن و بەکار دەهێنن: bitand(12, 10) دەکاتە 8 (بە دوویی 1100∧1010=10001100 \land 1010 = 1000)، bitor(12, 10) دەکاتە ⁦14⁩ و bitxor(12, 10) دەکاتە 6. ئەمانە وەک & و | نین، کە تەنها ڕاست یان هەڵە دەپرسن.

Quick checkپشکنینی خێراWhich condition is true exactly when t is between 18 and 25, both included?کام ڕێک کاتێک ڕاستە کە t لە نێوان ⁦18⁩ و ⁦25⁩دا بێت، هەردووکیان لەگەڵدا؟

if, else and elseifif، else و elseif

Choosing between paths is selection, one of the three building blocks of every algorithm; a flowchart draws it as a diamond. MATLAB writes it with an in three sizes. Each one ends with the keyword end:

هەڵبژاردنی یەک ڕێگا لە نێوان چەند ڕێگایەکدا پێی دەڵێن selection، یەکێک لە سێ بەردی بناغەی هەموو ؛ وەک چوارگۆشەیەکی لار (ئەڵماس) دەیکێشێت. MATLAB بە لە سێ قەبارەدا دەینووسێت. هەریەکەیان بە end کۆتایی دێت:

The three formsسێ شێوەکەMATLABType in a terminalلە تێرمیناڵدا بینووسە
% 1) if ... end: do something or nothing
if mark >= 50
    disp('Passed')
end

% 2) if ... else ... end: one of two paths
if mark >= 50
    disp('Passed')
else
    disp('Try again')
end

% 3) if ... elseif ... else ... end: one of several paths
if a < b
    j = -1;
elseif a > b
    j = 2;
else
    j = 3;
end
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

The follows if directly; brackets around it are allowed but not needed.

ڕاستەوخۆ دوای if دێت؛ کەوانە لە دەوری ڕێگەپێدراوە بەڵام پێویست نییە.

Lineدێڕی 14-20

MATLAB tests from the top and runs only the first block whose condition is true; else catches everything else.

MATLAB لە سەرەوە دەپشکنێت و تەنها یەکەم بلۆک جێبەجێ دەکات کە ڕاستە؛ else هەموو ئەوانی تر دەگرێت.

A flowchart and its code side by side in matching colours. From start, a blue diamond asks a < b; true leads to the box j = -1. False leads to an orange diamond a > b; true leads to j = 2; false leads to the purple box j = 3. All three paths join and go to end. The code on the right: if a < b (blue), j = -1; elseif a > b (orange), j = 2; else (purple), j = 3; end. Notes: elseif is one word; every if ends with end.هێڵکاریی ڕەوت و کۆدەکەی لە تەنیشت یەک بە ڕەنگی هاوشێوە. لە دەستپێکەوە ئەڵماسێکی شین دەپرسێت a < b؛ ڕاست دەچێت بۆ j = -1. هەڵە دەچێت بۆ ئەڵماسێکی پرتەقاڵی a > b؛ ڕاست دەچێت بۆ j = 2؛ هەڵە دەچێت بۆ چوارگۆشەی مۆری j = 3. هەر سێ ڕێگاکە یەک دەگرنەوە و دەچن بۆ کۆتایی. کۆدەکەی لای ڕاست: if a < b (شین)، j = -1؛ elseif a > b (پرتەقاڵی)، j = 2؛ else (مۆر)، j = 3؛ end. تێبینی: elseif یەک وشەیە؛ هەر if بە end کۆتایی دێت.
if / elseif / else as a flowchart and as MATLAB code⁦if / elseif / else⁩ وەک و وەک کۆدی MATLABCêIT
Two runs of the guide's example, with new numbersدوو جار جێبەجێکردنی نموونەکەی ڕێبەرەکە، بە ژمارەی نوێMATLABType in a terminalلە تێرمیناڵدا بینووسە
a = 6; b = 6;
if a < b
    j = -1;
elseif a > b
    j = 2;
else
    j = 3;
end
j

a = 6; b = 2;
if a < b
    j = -1;
elseif a > b
    j = 2;
end
j
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 1-9

a equals b, so both tests fail and else gives j=3j = 3.

a یەکسانە بە b، بۆیە هەردوو پشکنینەکە سەرناکەون و else ئەنجامی j=3j = 3 دەدات.

Lineدێڕی 11-17

No else here; a > b is true, so j=2j = 2.

لێرەدا else نییە؛ ⁦a > b⁩ ڕاستە، بۆیە j=2j = 2.

Indentation (the spaces before j = -1;) is for people: MATLAB does not need it, but it shows at a glance which lines belong to which branch. The Editor indents for you — select the code and press Ctrl+I (Smart Indent). If you know Python: Python writes elif and uses indentation to end a block (elif); MATLAB writes elseif and ends with end.

(ئەو پێش j = -1;) بۆ خەڵکە: MATLAB پێویستی پێی نییە، بەڵام بە یەک سەیرکردن پیشان دەدات کام دێڕ سەر بە کام لقە. Editor خۆی بۆت دەیکات — کۆدەکە دیاری بکە و ⁦Ctrl+I⁩ دابگرە (⁦Smart Indent⁩). ئەگەر Python دەزانیت: Python elif دەنووسێت و بە بلۆک کۆتایی پێدەهێنێت (elif)؛ MATLAB elseif دەنووسێت و بە end کۆتایی پێدەهێنێت.

Quick checkپشکنینی خێراt = 30; What does this print? if t > 35 disp('hot'), elseif t > 25 disp('warm'), elseif t > 15 disp('mild'), else disp('cold'), endt = 30; ئەمە چی چاپ دەکات؟ if t > 35 disp('hot'), elseif t > 25 disp('warm'), elseif t > 15 disp('mild'), else disp('cold'), end

MATLAB for Engineers - Conditional Statements if, else, and elseifSpartan Professor · Englishئینگلیزی · 98K viewsبینین · 1.9K likesلایک

How if, else and elseif choose a path: each one is drawn as a flowchart first and then written and run in MATLAB.

if، else و elseif چۆن ڕێگەیەک هەڵدەبژێرن: هەر یەکەیان سەرەتا وەک دەکێشرێت، پاشان لە MATLABـدا دەنووسرێت و جێبەجێ دەکرێت.

Three parts: if (0:00), else (4:22), elseif (7:08); each with a flowchart and a MATLAB script.

سێ بەش: if ⁦(0:00)⁩، else ⁦(4:22)⁩، elseif ⁦(7:08)⁩؛ هەر یەکەیان بە و MATLAB.

if, else and elseifif، else و elseif

elseif is one wordelseif یەک وشەیە

The course's Kurdish guide writes else if with a space and closes with a capital End. In MATLAB that is a different program. MathWorks warns that the space creates a nested if — a new if inside the else — and that nested if needs its own end. With only one end, MATLAB reports that an end is missing (a ), and End with a capital E is not a keyword at all.

ڕێبەرە کوردییەکەی وانەکە else if بە دەنووسێت و بە Endـی پیت گەورە دایدەخات. لە MATLABـدا ئەوە ترە. MathWorks ئاگادار دەکاتەوە کە دروست دەکات — ifـێکی نوێ لەناو elseـدا — و ئەو پێویستی بە endـی تایبەتی خۆی هەیە. بە تەنها یەک end، MATLAB ڕادەگەیەنێت کە endـێک کەمە (). هەروەها End بە Eـی گەورە هەرگیز نییە.

else if versus elseif⁦else if⁩ بەرامبەر elseif
As in the guide (fails)وەک ڕێبەرەکە (سەرناکەوێت)Correct: one wordدروست: یەک وشەAlso correct: a real nested ifئەمەش دروستە: ڕاستەقینە
if a < b j = -1; else if a > b j = 2; else j = 3; Endif a < b j = -1; elseif a > b j = 2; else j = 3; endif a < b j = -1; else if a > b j = 2; else j = 3; end end
Quick checkپشکنینی خێراHow many end keywords does the middle column need?ستوونی ناوەڕاست پێویستی بە چەند end هەیە؟

if, else and elseifif، else و elseif

An if inside an ififـێک لەناو ifـدا

A is an if placed inside a branch of another one. The guide shows the idea in pseudocode: if FALSE then NO, else (if TRUE then YES_YES, else YES_NO). Here it is in MATLAB with two real conditions — the inner if has its own end:

ئەو ifـەیە کە لەناو لقێکی ifـێکی تردا دادەنرێت. ڕێبەرەکە بیرۆکەکە بە پیشان دەدات: ئەگەر هەڵە بوو NO، دەنا (ئەگەر ڕاست بوو ⁦YES_YES⁩، دەنا ⁦YES_NO⁩). ئەمە لە MATLABـدا بە دوو ڕاستەقینە — ifـی ناوەوە endـی خۆی هەیە:

The guide's nested pseudocode in MATLABنیمچەکۆدی ناوەکیی ڕێبەرەکە بە MATLABMATLABType in a terminalلە تێرمیناڵدا بینووسە
if 2 > 3
    disp('NO')
else
    if 5 > 1
        disp('YES_YES')
    else
        disp('YES_NO')
    end
end

A practical example: a cinema ticket for Dilan's family. Adults pay 6000 IQD, students 4000 IQD, children under 12 pay 2000 IQD. The first question splits adults from children; only adults are asked whether they study:

نموونەیەکی پراکتیکی: بلیتی سینەما بۆ خێزانەکەی دیلان. گەورەکان ⁦6000⁩ دینار دەدەن، قوتابیان ⁦4000⁩ دینار و منداڵانی خوار ⁦12⁩ ساڵ ⁦2000⁩ دینار. یەکەم پرسیار گەورە و منداڵ لێک جیا دەکاتەوە؛ تەنها لە گەورەکان دەپرسرێت کە ئایا دەخوێنن:

A nested if (a sample run)ڕستەی ifـی ناوەکی (جارێک جێبەجێکردن)MATLABType in a terminalلە تێرمیناڵدا بینووسە
age = input('Age: ');
if age >= 12
    student = input('Student? (y/n): ', 's');
    if strcmp(student, 'y')
        price = 4000;
    else
        price = 6000;
    end
else
    price = 2000;
end
fprintf('Ticket: %d IQD\n', price)
Quick checkپشکنینی خێراIn the ticket program, what is the price for age 8?لە بلیتەکەدا، نرخ بۆ تەمەنی 8 چەندە؟

Spot the mistakes: the quadratic-equation solverهەڵەکان بدۆزەرەوە: چارەسەرکەری هاوکێشەی پلەدوو

A lab of the course asks for a program that solves the ax2+bx+c=0ax^2+bx+c=0. The plan is good: if a=0a = 0 the equation is only first-degree; otherwise compute the discriminant d=b2−4acd = b^2-4ac and decide — one root if d=0d = 0, complex roots if d<0d < 0, two real roots otherwise, with x=−b±d2ax=\frac{-b\pm\sqrt{d}}{2a}. Here is the lab's program, re-typed with its mistakes. Read it slowly before you look further:

تاقیگەیەکی وانەکە دەخوازێت کە ax2+bx+c=0ax^2+bx+c=0 چارەسەر بکات. پلانەکە باشە: ئەگەر a=0a = 0 بێت هاوکێشەکە تەنها پلەیەکە؛ دەنا (discriminant) d=b2−4acd = b^2-4ac هەژمار بکە و بڕیار بدە — یەک ڕەگ ئەگەر d=0d = 0، ڕەگی ئاوێتە ئەگەر d<0d < 0، دەنا دوو ڕەگی ڕاستی، بە x=−b±d2ax=\frac{-b\pm\sqrt{d}}{2a}. ئەمە تاقیگەکەیە، دووبارە نووسراوەتەوە لەگەڵ هەڵەکانی. پێش ئەوەی زیاتر بخوێنیتەوە، بە هێمنی بیخوێنەوە:

The lab's program, mistakes included (test: a=1a = 1, b=−3b = -3, c=2c = 2)بەرنامەکەی تاقیگە، لەگەڵ هەڵەکانی (تاقیکردنەوە: a=1a = 1، b=−3b = -3، c=2c = 2)MATLABType in a terminalلە تێرمیناڵدا بینووسە
a = input('enter the value a=')
b = input('enter the value b=')
c = input('enter the value c=')
if (a ~= 0)
    d = b^2 - 4*a*c;
    if (d == 0)
        x = -b/2*c
    elseif (d < 0)
        disp('complex case')
    else
        disp('we have three case')
        x1 = (-b + sqrt(d))/(2*c)
        x2 = (-b - sqrt(d))/(2*c)
    end
else
    x = -c/b
    disp('first degree eq.')
end
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 1-3

No semicolons, so MATLAB echoes a, b and c after each answer.

نییە، بۆیە MATLAB دوای هەر وەڵامێک a، b و c دەنووسێتەوە.

The test equation is x2−3x+2=0x^2-3x+2=0, which factors as (x−2)(x−1)=0(x-2)(x-1)=0: the roots are 2 and 1. The program prints 1 and 0.5. Follow the run in the animation, then read the list of mistakes.

هاوکێشەی تاقیکردنەوەکە x2−3x+2=0x^2-3x+2=0ـە، کە شیکار دەکرێت بۆ (x−2)(x−1)=0(x-2)(x-1)=0: ڕەگەکان 2 و 1ن. 1 و ⁦0.5⁩ چاپ دەکات. جێبەجێبوونەکە هەنگاو بە هەنگاو لە ئەنیمەیشنەکەدا ببینە، پاشان لیستی هەڵەکان بخوێنەوە.

Animation in six frames of the solver's main lines (the two message lines are left out). 1: a = 1, b = −3, c = 2 are read. 2: the test a ~= 0 is true. 3: d = (−3)^2 − 4·1·2 = 1. 4: d == 0 and d < 0 are both false, so the else block runs. 5: the buggy lines divide by 2c and print x1 = 1 and x2 = 0.5000 in red. 6: the lines are corrected to divide by 2a and print x1 = 2 and x2 = 1 in green; check: (x − 2)(x − 1) = x^2 − 3x + 2.
Animation in six frames of the solver's main lines (the two message lines are left out). 1: a = 1, b = −3, c = 2 are read. 2: the test a ~= 0 is true. 3: d = (−3)^2 − 4·1·2 = 1. 4: d == 0 and d < 0 are both false, so the else block runs. 5: the buggy lines divide by 2c and print x1 = 1 and x2 = 0.5000 in red. 6: the lines are corrected to divide by 2a and print x1 = 2 and x2 = 1 in green; check: (x − 2)(x − 1) = x^2 − 3x + 2.
Tracing the solver with a=1a = 1, b=−3b = -3, c=2c = 2: buggy, then fixedبەدواداچوونی چارەسەرکەرەکە بە a=1a = 1، b=−3b = -3، c=2c = 2: هەڵە، پاشان چاککراوCêIT
Quick checkپشکنینی خێراFor x2−3x+2=0x^2-3x+2=0 the lab's program prints x1 = 1 and x2 = 0.5000. What are the real roots?بۆ x2−3x+2=0x^2-3x+2=0 تاقیگە x1 = 1 و x2 = 0.5000 چاپ دەکات. ڕەگە ڕاستەقینەکان چین؟

Spot the mistakes: the quadratic-equation solverهەڵەکان بدۆزەرەوە: چارەسەرکەری هاوکێشەی پلەدوو

The five mistakesپێنج هەڵەکە

  1. Line 7: -b/2*c is worked out left to right as (−b2)⋅c\left(-\frac{b}{2}\right)\cdot c. The repeated root is −b2a-\frac{b}{2a}: write -b/(2*a). For a=1a = 1, b=−4b = -4, c=4c = 4 the program prints 8 instead of 2.
  2. Lines 12–13: the roots divide by (2*c); the formula divides by (2*a).
  3. Line 11: the message “we have three case” should say two real roots — a quadratic has at most two roots.
  4. Line 9: for d<0d < 0 it only prints “complex case”. MATLAB can give the complex roots directly: sqrt of a negative number is an multiple (complex numbers).
  5. Line 16: when a=0a = 0 and b=0b = 0 there is no equation to solve; -c/b divides by zero and prints -Inf or Inf (or NaN when c is 0 too).
  1. دێڕی 7: -b/2*c لە چەپەوە بۆ ڕاست وەک (−b2)⋅c\left(-\frac{b}{2}\right)\cdot c هەژمار دەکرێت. ڕەگە دووبارەکە −b2a-\frac{b}{2a}ـە: بنووسە -b/(2*a). بۆ a=1a = 1، b=−4b = -4، c=4c = 4 8 چاپ دەکات لە جیاتی 2.
  2. دێڕەکانی ⁦12–13⁩: ڕەگەکان بەسەر (2*c)ـدا دابەش دەکەن؛ بەسەر (2*a)ـدا دابەش دەکات.
  3. دێڕی ⁦11⁩: پەیامی «⁦we have three case⁩» دەبێت بڵێت دوو ڕەگی ڕاستی — زۆرترین دوو ڕەگی هەیە.
  4. دێڕی 9: بۆ d<0d < 0 تەنها «⁦complex case⁩» چاپ دەکات. MATLAB دەتوانێت ڕاستەوخۆ ڕەگە ئاوێتەکان بدات: sqrtـی چەند جارێکی (ژمارە ئاوێتەکان).
  5. دێڕی ⁦16⁩: کاتێک a=0a = 0 و b=0b = 0 بێت هیچ هاوکێشەیەک نییە بۆ چارەسەرکردن؛ -c/b بەسەر سفردا دابەش دەکات و -Inf یان Inf چاپ دەکات (یان NaN ئەگەر cـیش سفر بێت).
Quick checkپشکنینی خێراWhy does x = -b/2*c give 8 for a=1a = 1, b=−4b = -4, c=4c = 4?بۆچی x = -b/2*c بۆ a=1a = 1، b=−4b = -4، c=4c = 4 ئەنجامی 8 دەدات؟

Spot the mistakes: the quadratic-equation solverهەڵەکان بدۆزەرەوە: چارەسەرکەری هاوکێشەی پلەدوو

The corrected solverچارەسەرکەرە چاککراوەکە

The corrected program divides by 2*a, prints honest messages, gives complex roots instead of only a message, and handles the two special cases when a=0a = 0. Test data is chosen so that every branch runs at least once:

بەسەر 2*aـدا دابەش دەکات، پەیامی ڕاست چاپ دەکات، لە جیاتی تەنها پەیامێک ڕەگە ئاوێتەکان دەدات و دوو حاڵەتە تایبەتەکەی کاتێک a=0a = 0 چارەسەر دەکات. وا هەڵبژێردراوە کە هەموو لقێک لانیکەم جارێک جێبەجێ ببێت:

quadratic.m, corrected (run with a=1a = 1, b=−3b = -3, c=2c = 2)⁦quadratic.m⁩، چاککراو (جێبەجێکردن بە a=1a = 1، b=−3b = -3، c=2c = 2)quadratic.mMATLABType in a terminalلە تێرمیناڵدا بینووسە
% quadratic.m - solve a*x^2 + b*x + c = 0
a = input('a = ');
b = input('b = ');
c = input('c = ');
if a ~= 0
    d = b^2 - 4*a*c;            % the discriminant
    if d == 0
        disp('One repeated root')
        x = -b/(2*a)
    else
        if d > 0
            disp('Two real roots')
        else
            disp('Two complex roots')
        end
        x1 = (-b + sqrt(d))/(2*a)
        x2 = (-b - sqrt(d))/(2*a)
    end
elseif b ~= 0
    disp('First-degree equation')
    x = -c/b
elseif c == 0
    disp('Every x is a solution')
else
    disp('No solution')
end
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 6

A % comment can follow code on the same line.

دەتوانێت لە هەمان دێڕدا دوای کۆد بێت.

Lineدێڕی 16-17

For d<0d < 0, sqrt(d) is imaginary, so x1 and x2 come out complex — no special code needed.

بۆ d<0d < 0، sqrt(d) خەیاڵییە، بۆیە ⁦x1⁩ و ⁦x2⁩ ئاوێتە دەردەچن — کۆدی تایبەت پێویست نییە.

Lineدێڕی 19-26

a=0a = 0: if b is not 0 it is a ; if b=0b = 0 too, either every x works (c=0c = 0) or none does.

a=0a = 0: ئەگەر b سفر نەبێت ؛ ئەگەر bـیش سفر بێت، یان هەموو xێک دروستە (c=0c = 0) یان هیچیان.

Test data that reaches every branch کە دەگاتە هەموو لقێک
a, b, ca، b، cBranchلقCorrected output چاککراوThe lab's program تاقیگە
1,−3,21, -3, 2two real rootsدوو ڕەگی ڕاستیx1 = 2, x2 = 1x1 = 1, x2 = 0.5000
1,−4,41, -4, 4one repeated rootیەک ڕەگی دووبارەx = 2x = 8
1,2,51, 2, 5complex rootsڕەگی ئاوێتەx1 = -1.0000 + 2.0000i, x2 = -1.0000 - 2.0000icomplex case
0,2,−60, 2, -6first-degreeپلەیەکx = 3x = 3
0,0,50, 0, 5no solutionهیچ چارەسەر نییەNo solutionx = -Inf
0,0,00, 0, 0every xهەموو xEvery x is a solutionx = NaN

Choosing tests like this is the trace-table habit of the shared lessons: try the normal case, the equal case, the impossible case and the edge cases, not just one happy example.

هەڵبژاردنی تاقیکردنەوەی لەم جۆرە ڕاهاتنی خشتەی بەدواداچوونی وانە هاوبەشەکانە: حاڵەتی ئاسایی، حاڵەتی یەکسان، حاڵەتی مەحاڵ و حاڵەتە سنوورییەکان تاقی بکەرەوە، نەک تەنها یەک نموونەی سەرکەوتوو.

Quick checkپشکنینی خێراWhich input tests the elseif c == 0 branch?کام لقی elseif c == 0 تاقی دەکاتەوە؟

When the condition is an arrayکاتێک مەرجەکە ڕیزەکە

What does if v > 0 mean when v holds several numbers? MathWorks gives the rule: an if condition is true only when its result is not empty and every element is nonzero. So one negative number makes the whole condition false, and an empty array [] is false too. The code below says not all positive although two of the three numbers are positive:

if v > 0 چ واتایەکی هەیە کاتێک v چەند ژمارەیەک هەڵدەگرێت؟ MathWorks یاساکە دەدات: if تەنها کاتێک ڕاستە کە ئەنجامەکەی بەتاڵ نەبێت و هەموو سفر نەبن. بۆیە یەک هەموو هەڵە دەکات و بەتاڵی []ـیش هەڵەیە. کۆدی خوارەوە دەڵێت ⁦not all positive⁩ هەرچەندە دوو لە سێ ژمارەکە موجەبن:

An array conditionمەرجێک بە ڕیزەکMATLABType in a terminalلە تێرمیناڵدا بینووسە
v = [4 -2 7];
if v > 0
    disp('all positive')
else
    disp('not all positive')
end

Code is clearer when it says what it means. Write if all(v > 0) for every element, and if any(v < 0) for at least one. To ask whether two whole arrays are the same, use isequal(A, B): it gives one answer. A == B gives a whole array of answers, and an error when the sizes do not fit together.

کۆد ڕوونترە کاتێک ئەوە بڵێت کە مەبەستیەتی. بۆ هەموو بنووسە if all(v > 0) و بۆ لانیکەم یەکێک بنووسە if any(v < 0). بۆ پرسینی ئەوەی ئایا دوو تەواو وەک یەکن، isequal(A, B) بەکار بهێنە: یەک وەڵام دەدات. A == B تەواو لە وەڵام دەدات و کاتێک قەبارەکان لەگەڵ یەک ناگونجێن دەدات.

Quick checkپشکنینی خێراw = [3 0 5]; Is the condition of if w true?w = [3 0 5]; ئایا if w ڕاستە؟

The flow-control keywords, and errorوشە کلیلەییەکانی کۆنترۆڵی ڕەوت، هەروەها error

The commands list and the guide name the words MATLAB uses to steer a program — its . This lesson teaches if, elseif, else and end; the others come next:

لیستی و ڕێبەرەکە ئەو وشانە ناو دەبەن کە MATLAB بۆ ئاڕاستەکردنی بەکاریان دەهێنێت — . ئەم وانەیە if، elseif، else و end فێر دەکات؛ ئەوانی تر دواتر دێن:

Flow control in the commands list لە لیستی
KeywordsJobئەرکWhereلە کوێ
if, elseif, else, endchoose a path by a conditionهەڵبژاردنی ڕێگایەک بە this lessonئەم وانەیە
switch, case, otherwisechoose a path by a valueهەڵبژاردنی ڕێگایەک بە the switch statementوانەی ڕستەی switch
forrepeat for each value of a vectorدووبارەکردنەوە بۆ هەر for loopsوانەی خولەکانی for
while, breakrepeat while a condition holds; leave a loop earlyدووبارەکردنەوە تا ڕاستە؛ زوو جێهێشتنی while loops and breakوانەی خولەکانی while و break
returnleave a function earlyزوو جێهێشتنی function filesوانەی فایلەکانی کردار
error, warningstop with a message / only warnوەستان بە پەیامێک / تەنها ئاگادارکردنەوەbelowخوارەوە

error('…') stops the program at once and shows your message in red, like any other error. warning('…') prints Warning: and your message, and the program goes on. Use error when continuing would give nonsense:

error('…') دەستبەجێ دەوەستێنێت و پەیامەکەت بە سوور پیشان دەدات، وەک هەر تر. warning('…') Warning: و پەیامەکەت چاپ دەکات و بەردەوام دەبێت. error بەکار بهێنە کاتێک بەردەوامبوون ئەنجامی بێمانا دەدات:

error stops, warning only warns (a run with 0)error دەوەستێنێت، warning تەنها ئاگادار دەکاتەوە (جێبەجێکردن بە ⁦0⁩)check_radius.mMATLABType in a terminalلە تێرمیناڵدا بینووسە
r = input('Radius: ');
if r < 0
    error('The radius cannot be negative.')
elseif r == 0
    warning('The radius is zero, so the area is 0.')
end
area = pi*r^2
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 3

With −2-2, MATLAB would stop here and print the message in red; area would never be computed.

بە −2-2، MATLAB لێرە دەوەستێت و پەیامەکە بە سوور چاپ دەکات؛ area هەرگیز هەژمار ناکرێت.

Quick checkپشکنینی خێراA script must not continue if a divisor is 0. Which call fits? نابێت بەردەوام بێت ئەگەر دابەشکەرێک سفر بێت. کام بانگکردن دەگونجێت؟

The 0 < x < 10 trapتەڵەی ⁦0 < x < 10⁩

In maths, 0<x<100 < x < 10 means x is between 0 and 10. MATLAB reads 0 < x < 10 differently: left to right, as (0 < x) < 10. The first part gives 0 or 1, and both 0 and 1 are less than 10 — so the whole test is always true, even for x=25x = 25. Write two comparisons joined by AND:

لە بیرکاریدا 0<x<100 < x < 10 واتە x لە نێوان 0 و ⁦10⁩دایە. MATLAB 0 < x < 10 بە شێوەیەکی تر دەخوێنێتەوە: لە چەپەوە بۆ ڕاست، وەک (0 < x) < 10. بەشی یەکەم 0 یان 1 دەدات و 0 و 1 هەردووکیان لە ⁦10⁩ بچووکترن — بۆیە هەموو پشکنینەکە هەمیشە ڕاستە، تەنانەت بۆ x=25x = 25. دوو بەراورد بنووسە کە بە AND پێکەوە بەستراون:

Always true versus correctهەمیشە ڕاست بەرامبەر دروستMATLABType in a terminalلە تێرمیناڵدا بینووسە
x = 25;
0 < x < 10
0 < x && x < 10
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

(0 < 25) is 1, and 1 < 10 is 1: wrong.

(0 < 25) دەکاتە 1 و 1 < 10 دەکاتە 1: هەڵەیە.

Lineدێڕی 3

Both conditions must hold: correct.

دەبێت هەردوو ڕاست بن: دروستە.

Python is the exception here: it really does read 0 < x < 10 as two comparisons (chained comparisons). MATLAB, like most languages, does not.

Python لێرەدا جیاوازە: بەڕاستی 0 < x < 10 وەک دوو بەراورد دەخوێنێتەوە (بەراوردی زنجیرەیی). MATLAB، وەک زۆربەی زمانەکان، وا ناکات.

Quick checkپشکنینی خێراWhat does 5 > 3 > 1 give in MATLAB?5 > 3 > 1 لە MATLABـدا چی دەدات؟

Common mistakesهەڵە باوەکان

MistakeهەڵەWhat happensچی ڕوودەداتFixچارەسەر
if x = 5 (one =)if x = 5 (یەک =)An error: = stores, it cannot test.: = هەڵدەگرێت، ناتوانێت بپشکنێت.if x == 5if x == 5
else if with a spaceelse if بە A nested if that needs its own end; usually a missing-end error. کە endـی خۆی پێویستە؛ زۆرجار بە هۆی کەمیی end.elseif as one wordelseif وەک یەک وشە
Forgetting end, or writing Endلەبیرچوونی end، یان نووسینی EndMATLAB reports a missing end; End is not a keyword.MATLAB کەمیی end ڕادەگەیەنێت؛ End نییە.Close every if with lower-case end; let Ctrl+I indent.هەموو ifـێک بە endـی پیت بچووک دابخە؛ با ⁦Ctrl+I⁩ بکات.
if name == 'Hana'if name == 'Hana'An error when the lengths differ. کاتێک درێژییەکان جیاوازن.if strcmp(name, 'Hana')if strcmp(name, 'Hana')
if 0 < x < 10if 0 < x < 10Always true.هەمیشە ڕاستە.if 0 < x && x < 10if 0 < x && x < 10
if v > 0 for a vector, meaning someif v > 0 بۆ ، بە مەبەستی هەندێکیانTrue only if all elements pass.تەنها کاتێک ڕاستە کە هەموو تێپەڕن.any(v > 0) or all(v > 0)any(v > 0) یان all(v > 0)
if x == NaNif x == NaNAlways false: NaN equals nothing.هەمیشە هەڵەیە: NaN یەکسان نییە بە هیچ.if isnan(x)if isnan(x)
0.1 + 0.2 == 0.30.1 + 0.2 == 0.3False because of round-off.هەڵەیە بەهۆی هەڵەی نزیککردنەوە.abs(a - b) < 1e-10abs(a - b) < 1e-10
~a > b meaning not (a > b)~a > b بە مەبەستی نا ⁦(a > b)⁩~ is applied to a first.~ سەرەتا لەسەر a جێبەجێ دەبێت.~(a > b)~(a > b)

Practiceڕاهێنان

ExerciseڕاهێنانEven or oddجووت یان تاک

Read a whole number and print whether it is even or odd. Use mod (remainders).

بخوێنەوە و چاپ بکە کە جووتە یان تاک. mod بەکار بهێنە (پاشماوە).

Hintئاماژە

A number is even when the remainder after dividing by 2 is 0.

ژمارەیەک جووتە کاتێک دابەشکردنی بەسەر 2دا سفر بێت.

Show answerوەڵامەکە پیشان بدە
MATLABType in a terminalلە تێرمیناڵدا بینووسە
n = input('Whole number: ');
if mod(n, 2) == 0
    fprintf('%d is even\n', n)
else
    fprintf('%d is odd\n', n)
end
ExerciseڕاهێنانLeap yearساڵی کەبیسە

A year is a leap year if it divides by 4 but not by 100, or if it divides by 400. Write one if with && and || and test 2024, 2026, 1900 and 2000.

ساڵێک کەبیسەیە ئەگەر بەسەر 4دا دابەش بێت بەڵام بەسەر ⁦100⁩دا نا، یان ئەگەر بەسەر ⁦400⁩دا دابەش بێت. یەک if بە && و || بنووسە و ⁦2024⁩، ⁦2026⁩، ⁦1900⁩ و ⁦2000⁩ تاقی بکەرەوە.

Hintئاماژە

Brackets make the AND part one unit before the OR.

کەوانەکان بەشی AND پێش OR دەکەن بە یەک یەکە.

Show answerوەڵامەکە پیشان بدە
MATLABType in a terminalلە تێرمیناڵدا بینووسە
y = input('Year: ');
if (mod(y, 4) == 0 && mod(y, 100) ~= 0) || mod(y, 400) == 0
    disp('leap year')
else
    disp('not a leap year')
end

2024 → leap year, 2026 → not, 1900 → not (divides by 100 but not by 400), 2000 → leap year.

⁦2024⁩ کەبیسەیە، ⁦2026⁩ نا، ⁦1900⁩ نا (بەسەر ⁦100⁩دا دابەش دەبێت بەڵام بەسەر ⁦400⁩دا نا)، ⁦2000⁩ کەبیسەیە.

ExerciseڕاهێنانMarks above the lineنمرەکانی سەرووی هێڵ

marks = [45 72 50 38 91 66]; In one line each, find: the marks of 50 or more, how many there are, their positions, and whether anyone got more than 90.

marks = [45 72 50 38 91 66]; هەریەکە بە یەک دێڕ بدۆزەرەوە: نمرەکانی ⁦50⁩ و سەرووتر، چەندن، شوێنەکانیان و ئایا کەسێک لە ⁦90⁩ زیاتری هێناوە.

Show answerوەڵامەکە پیشان بدە
MATLABType in a terminalلە تێرمیناڵدا بینووسە
marks = [45 72 50 38 91 66];
passed = marks(marks >= 50)
how_many = sum(marks >= 50)
pos = find(marks >= 50)
any(marks > 90)
ExerciseڕاهێنانPredict jj پێشبینی بکە

Using the program from the guide (if a < b j = -1; elseif a > b j = 2; else j = 3; end), what is j for (a,b)=(1,9)(a, b) = (1, 9), (9,1)(9, 1) and (5,5)(5, 5)?

بە بەکارهێنانی ڕێبەرەکە (if a < b j = -1; elseif a > b j = 2; else j = 3; end)، j چەندە بۆ (a,b)=(1,9)(a, b) = (1, 9)، (9,1)(9, 1) و (5,5)(5, 5)؟

Show answerوەڵامەکە پیشان بدە

(1,9)→−1(1, 9) \to -1; (9,1)→2(9, 1) \to 2; (5,5)→3(5, 5) \to 3.

بۆ (1,9)(1, 9) دەبێتە −1-1؛ بۆ (9,1)(9, 1) دەبێتە 2؛ بۆ (5,5)(5, 5) دەبێتە 3.

ExerciseڕاهێنانThe bigger of two numbersگەورەترینی دوو ژمارە

Read two numbers and print the bigger one, or say they are equal. Then compare your program with the largest of three flowchart.

دوو ژمارە بخوێنەوە و گەورەترینیان چاپ بکە، یان بڵێ یەکسانن. پاشان لەگەڵ گەورەترینی سێ ژمارە بەراورد بکە.

Show answerوەڵامەکە پیشان بدە
MATLABType in a terminalلە تێرمیناڵدا بینووسە
p = input('First: ');
q = input('Second: ');
if p > q
    fprintf('%g is bigger\n', p)
elseif q > p
    fprintf('%g is bigger\n', q)
else
    disp('They are equal')
end

Summaryکورتە

  • == ~= < <= > >= compare and answer with logical 1 or 0; = stores, == compares; write ~= (Octave also knows !=).
  • On arrays, comparisons work element by element and give a logical array; v(v > 5) picks elements, find gives positions, any/all summarise.
  • isnan, isinf, isfinite (not finite), isempty, isreal test special values.
  • &, |, ~, xor work element by element; use && and || (short-circuit) in if conditions.
  • if … elseif … else … end: only the first true branch runs; elseif is one word; every if ends with end.
  • An array condition is true only if it is not empty and all elements are nonzero; 0 < x < 10 is always true — write 0 < x && x < 10.
  • Test every branch: the quadratic solver needed (2*a), honest messages and the a=0a = 0, b=0b = 0 cases.
  • == ~= < <= > >= بەراورد دەکەن و بە 1 یان 0ـی لۆجیکی وەڵام دەدەنەوە؛ = هەڵدەگرێت و == بەراورد دەکات؛ ~= بنووسە (Octave !=ـیش دەناسێت).
  • لەسەر ، بەراوردەکان بە کار دەکەن و دەدەن؛ v(v > 5) هەڵدەبژێرێت، find شوێنەکان دەدات، any/all کورتی دەکەنەوە.
  • isnan، isinf، isfinite (نەک finite)، isempty و isreal دەپشکنن.
  • &، |، ~ و xor بە کار دەکەن؛ لە ifـدا && و || () بەکار بهێنە.
  • if … elseif … else … end: تەنها یەکەم لقی ڕاست جێبەجێ دەبێت؛ elseif یەک وشەیە؛ هەموو ifـێک بە end کۆتایی دێت.
  • بە تەنها کاتێک ڕاستە کە بەتاڵ نەبێت و هەموو سفر نەبن؛ 0 < x < 10 هەمیشە ڕاستە — بنووسە 0 < x && x < 10.
  • هەموو لقێک تاقی بکەرەوە: چارەسەرکەری پێویستی بە (2*a)، پەیامی ڕاست و حاڵەتەکانی a=0a = 0، b=0b = 0 بوو.
Review questionsپرسیارەکانی پێداچوونەوە
Why is if x = 3 an error but x = 3 fine?بۆچی if x = 3 هەڵەیە بەڵام x = 3 دروستە؟

An if needs a question; = is an assignment, not a comparison. Use ==.

if پێویستی بە پرسیارێکە؛ = ، نەک بەراورد. == بەکار بهێنە.

What is the difference between & and &&?جیاوازیی & و && چییە؟

& works element by element on arrays; && takes single true/false values and stops early when the answer is known.

& بە لەسەر کار دەکات؛ && تاکە ڕاست/هەڵە وەردەگرێت و کاتێک وەڵامەکە زانرا زوو دەوەستێت.

How do you get the numbers of v that are negative?چۆن ئەو ژمارانەی v وەردەگریت کە سالبن؟

v(v < 0) — logical indexing with the mask v < 0.

v(v < 0) — بە ماسکی v < 0.

Lesson complete!وانەکە تەواو بوو!

Well done. Review the key terms, then go on to the next lesson.دەستخۆش. سەیرێکی زاراوە سەرەکییەکان بکەرەوە، پاشان بڕۆ بۆ وانەی داهاتوو.

Key termsزاراوە سەرەکییەکان

Next lessonوانەی داهاتوو

The switch Statementڕستەی switch

References and further readingسەرچاوەکان و خوێندنەوەی زیاتر

This lecture is a first edition. The Kurdish text and terminology are waiting for review by Kurdish IT teachers.ئەم وانەیە چاپی یەکەمە. دەقە کوردییەکە و زاراوەکانی چاوەڕێی پێداچوونەوەی مامۆستایانی کوردی بواری IT ن.