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

for Loops: Sums, Series and Searchingخولەکانی for: کۆکردنەوە، زنجیرە و گەڕان

Repeat a block for every value of a vector with for … end, understand the loop variable, add series with an accumulator (∑i/i!→e\sum i/i! \to e) and an alternating sign (J = -J), trace the lab's loops to find the factorial(n) slip, search for the largest and smallest input, put if and switch inside a loop, and fill a table with nested loops after preallocating it with zeros.

بە for … end بلۆکێک بۆ هەر دووبارە بکەرەوە، لە تێبگە، زنجیرە بە (∑i/i!→e\sum i/i! \to e) و نیشانەی ئاڵوگۆڕ (J = -J) کۆ بکەرەوە، تاقیگە بەدوادا بچۆ بۆ دۆزینەوەی هەڵەی ⁦factorial(n)⁩، بەدوای گەورەترین و بچووکترین بگەڕێ، if و switch بخەرە ناو و دوای بە zeros، خشتەیەک بە پڕ بکەرەوە.

17 min read17 خولەک خوێندنەوە · 15 stepsهەنگاو

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

  • Write for loops over ranges, countdowns and lists, and predict the value of the loop variable in each turn.
  • Add up series with an accumulator, including an alternating sign, and write the same sum in one vectorised line.
  • Trace a loop with a trace table and find mistakes such as factorial(n) instead of factorial(i).
  • Find the largest and smallest of several inputs, starting from the first value.
  • Put if and switch inside loops, and fill a preallocated matrix with nested loops.
  • بەسەر زنجیرە، ژماردنی بەرەو خوار و لیستدا بنووسیت و لە هەر پێشبینی بکەیت.
  • بە زنجیرە کۆ بکەیتەوە (لەوانە بە نیشانەی ئاڵوگۆڕ) و هەمان کۆ لە یەک دێڕدا بێ بنووسیت.
  • بە بەدوادا بچیت و هەڵەی وەک ⁦factorial(n)⁩ لە جیاتی ⁦factorial(i)⁩ بدۆزیتەوە.
  • گەورەترین و بچووکترینی چەند بدۆزیتەوە، بە دەستپێکردن لە یەکەم .
  • if و switch بخەیتە ناو و پێشتر ئامادەکراو بە پڕ بکەیتەوە.

Repeating a block: forدووبارەکردنەوەی بلۆکێک: for

Repetition is the third building block of every algorithm. A repeats a block of commands once for each value of a vector: for k = 1:5 … end runs its body five times, with k equal to 1, then 2, then 3, 4 and 5. k is the ; each repetition is one (one turn). Like if, every for ends with end.

دووبارەکردنەوە سێیەم بەردی بناغەی هەموو . بلۆکێک لە بۆ هەر جارێک دووبارە دەکاتەوە: for k = 1:5 … end ناوەڕۆکەکەی پێنج جار جێبەجێ دەکات، k یەکەم جار 1ـە، پاشان 2، پاشان 3، 4 و 5. k ؛ هەر دووبارەبوونەوەیەک (یەک جار). وەک if، هەموو forـێک بە end کۆتایی دێت.

The 7 times table, first five linesخشتەی لێکدانی 7، پێنج دێڕی یەکەمMATLABType in a terminalلە تێرمیناڵدا بینووسە
for k = 1:5
    fprintf('%d x 7 = %d\n', k, 7*k)
end
A for-loop flowchart next to a short loop. From start, a diamond asks whether the vector is empty; yes goes straight to 'after the loop'. No leads to 'k = the first value', then the highlighted box 'the loop body runs', then a diamond 'Was that the last value?'. No leads to 'k = the next value' and back to the body; yes leads to 'after the loop'. On the right, the code for k = [3 7 1], disp(k^2), end and its three turns: turn 1 k = 3 prints 9, turn 2 k = 7 prints 49, turn 3 k = 1 prints 1 — three turns for three values.هێڵکاریی ڕەوتی خولی for لە تەنیشت خولێکی کورت. لە دەستپێکەوە ئەڵماسێک دەپرسێت ئایا ئاڕاستەبڕەکە بەتاڵە؛ بەڵێ ڕاستەوخۆ دەچێت بۆ «دوای خولەکە». نەخێر دەچێت بۆ «k = یەکەم نرخ»، پاشان بۆ چوارگۆشە ڕەنگکراوەکەی «بلۆکی کۆدەکە جێبەجێ دەبێت»، پاشان ئەڵماسی «ئایا ئەوە دوایین نرخ بوو؟». نەخێر دەچێت بۆ «k = نرخی دواتر» و دەگەڕێتەوە بۆ ناوەڕۆکەکە؛ بەڵێ دەچێت بۆ «دوای خولەکە». لای ڕاست کۆدی for k = [3 7 1]، disp(k^2)، end و سێ خولانەوەکەی: خولانەوەی یەکەم k = 3 و 9 چاپ دەکات، خولانەوەی دووەم k = 7 و 49، خولانەوەی سێیەم k = 1 و 1 — سێ نرخ، سێ خولانەوە.
How a for loop runs: one turn for each value چۆن جێبەجێ دەبێت: بۆ هەر CêIT

The flowchart (redrawn from the course's Kurdish guide) shows a detail: if the vector is empty — for example 1:0 — the body never runs. If you know Python: there you write for k in range(1, 6) and the end is left out (range); in MATLAB 1:5 includes 5. All code here also runs in GNU Octave and the free version of MATLAB Online (free ways to practise).

(کە لە ڕێبەرە کوردییەکەی وانەکەوە دووبارە کێشراوەتەوە) وردەکارییەک پیشان دەدات: ئەگەر بەتاڵ بێت — بۆ نموونە 1:0 — ناوەڕۆکەکە هەرگیز جێبەجێ نابێت. ئەگەر Python دەزانیت: لەوێ for k in range(1, 6) دەنووسیت و کۆتاییەکە دەردەکرێت (range)؛ لە MATLABـدا 1:5 5ـیش دەگرێتەوە. هەموو کۆدی ئێرە لە ⁦GNU Octave⁩ و وەشانی بێبەرانبەری ⁦MATLAB Online⁩ـیشدا کار دەکات (ڕێگە بێبەرانبەرەکانی ڕاهێنان).

Quick checkپشکنینی خێراHow many times does the body of for k = 3:7 … end run?ناوەڕۆکی for k = 3:7 … end چەند جار جێبەجێ دەبێت؟

MATLAB Crash Course for BeginnersfreeCodeCamp.org · Englishئینگلیزی · 1.7M viewsبینین · 37K likesلایک · Partبەشی 1:28:20–1:40:09 (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 'For Loops' (1:28:20-1:40:09): for i = 1:10 ... end, then a loop with an if inside that counts the 3s in a vector.

بەشی For Loops (لە ⁦1:28:20⁩ تا ⁦1:40:09⁩): for i = 1:10 ... end، پاشان کە ifـێکی تێدایە و ژمارەی 3ـەکانی ناو دەژمێرێت.

Repeating a block: forدووبارەکردنەوەی بلۆکێک: for

Any vector can drive a loopهەر ئاڕاستەبڕێک دەتوانێت خولێک بەڕێوە ببات

The values after = can be any row vector: a range with a step (colon ranges), a range that counts down, or a list of numbers with uneven gaps. MathWorks' own example loops over [1 5 8 17].

ئەو دوای = دەتوانن هەر ڕیزی بن: زنجیرەیەک بە هەنگاو (زنجیرەکانی دوو خاڵ)، زنجیرەیەک کە بەرەو خوار دەژمێرێت، یان لیستێک ژمارە کە دووریی نێوانیان یەکسان نییە. نموونەکەی خودی MathWorks بەسەر [1 5 8 17]ـدا دەخولێتەوە.

Counting down, and uneven valuesژماردن بەرەو خوار و نرخەکانی نایەکسانMATLABType in a terminalلە تێرمیناڵدا بینووسە
for k = 10:-2:0
    fprintf('%d ', k)
end
fprintf('\n')

for x = [3 7 1]
    fprintf('%d squared is %d\n', x, x^2)
end

One surprise: MATLAB takes the vector column by column. A row vector has one value per column, so all is well. A column vector such as [3; 7; 1] is a single column, so the loop runs once, with x equal to the whole column. MathWorks' advice: to loop over a column vector, transpose it first, for x = v'.

سەرسوڕهێنەرییەک: MATLAB ستوون بە ستوون وەردەگرێت. ڕیزی بۆ هەر ستوونێک یەک هەیە، بۆیە هەموو شتێک باشە. ستوونی وەک [3; 7; 1] تەنها یەک ستوونە، بۆیە یەک جار جێبەجێ دەبێت و x یەکسانە بە هەموو ستوونەکە. ئامۆژگاریی MathWorks: بۆ خولانەوە بەسەر ستوونیدا، سەرەتا بیگۆڕە بۆ ڕیز: for x = v'.

A column vector: one turn onlyئاڕاستەبڕێکی ستوونی: تەنها یەک خولانەوەMATLABType in a terminalلە تێرمیناڵدا بینووسە
for x = [3; 7; 1]
    x
end
Quick checkپشکنینی خێراWhat does for k = 5:-1:1, fprintf('%d', k), end print?for k = 5:-1:1, fprintf('%d', k), end چی چاپ دەکات؟

Repeating a block: forدووبارەکردنەوەی بلۆکێک: for

The loop variable belongs to the loopگۆڕاوی خول هی خولەکەیە

At the start of each turn MATLAB gives the loop variable the next value of the vector, whatever happened before. Two consequences: a value the variable had before the loop is simply overwritten (the course guide shows the same idea with i := 20 before a loop), and changing the variable inside the body does not change the next turn — MathWorks says the for statement overrides such changes. After the loop the variable keeps its last value.

لە سەرەتای هەر MATLAB دواتری دەدات بە ، هەرچی پێشتر ڕوویدابێت. دوو دەرەنجام: ئەو پێش هەیبوو تەنها دەسڕدرێتەوە (ڕێبەرەکەی وانەکە هەمان بیرۆکە بە i := 20 پێش پیشان دەدات). هەروەها گۆڕینی لەناو ناوەڕۆکەکەدا دواتر ناگۆڕێت — MathWorks دەڵێت ڕستەی for ئەو گۆڕانکارییانە پشتگوێ دەخات. دوای ، دوایین دەهێڵێتەوە.

The old value 20 is overwrittenنرخە کۆنەکەی ⁦20⁩ دەسڕدرێتەوەMATLABType in a terminalلە تێرمیناڵدا بینووسە
k = 20;
for k = 1:3
    a = k;
    b = k^2;
    fprintf('%d, %d\n', a, b)
end
k
Changing k inside the body does not skip turnsگۆڕینی k لەناو ناوەڕۆکدا خولانەوە تێناپەڕێنێتMATLABType in a terminalلە تێرمیناڵدا بینووسە
for k = 1:3
    k = k*10;
    disp(k)
end
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

This changes k only for the rest of this turn; the next turn still starts with 2, then 3.

ئەمە تەنها بۆ باقیی ئەم k دەگۆڕێت؛ دواتر هەر بە 2 دەست پێدەکات، پاشان 3.

Quick checkپشکنینی خێراAfter for k = 2:2:8, end, what is k?دوای for k = 2:2:8, end، k چەندە؟

Adding up in a loop: the accumulatorکۆکردنەوە لەناو خولێکدا: گۆڕاوی کۆکردنەوە

To add many terms, keep a running total in a variable — an . Set it to 0 before the loop, add one term in each turn, and read it after the loop. A loop that adds terms is exactly a sum written with (a loop is a sigma). A lab program adds the terms ii!\frac{i}{i!}:

بۆ کۆکردنەوەی زۆر بڕ، کۆیەکی بەردەوام لە هەڵبگرە — . پێش بیکە بە 0، لە هەر یەک بڕ زیاد بکە و دوای بیخوێنەوە. کە بڕەکان کۆ دەکاتەوە ڕێک ئەو کۆیەیە کە بە دەنووسرێت (خولێک سیگمایەکە). تاقیگە بڕەکانی ii!\frac{i}{i!} کۆ دەکاتەوە:

total=∑i=1nii!=11!+22!+33!+⋯\text{total} = \sum_{i=1}^{n}\frac{i}{i!} = \frac{1}{1!}+\frac{2}{2!}+\frac{3}{3!}+\cdots
The accumulator pattern (run with n=5n = 5)شێوازی گۆڕاوی کۆکردنەوە (جێبەجێکردن بە n=5n = 5)sum_series.mMATLABType in a terminalلە تێرمیناڵدا بینووسە
total = 0;
n = input('Enter the last number: ');
for i = 1:n
    total = total + i/factorial(i);
end
total
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 1

Start at 0 — before the loop, never inside it.

بە 0 دەست پێبکە — پێش ، هەرگیز لەناویدا نا.

Lineدێڕی 4

New total = old total + this turn's term. factorial(i) uses the loop variable.

کۆی نوێ = کۆی کۆن + بڕی ئەم . factorial(i) بەکار دەهێنێت.

Watch the total grow. Since ii!=1(i−1)!\frac{i}{i!}=\frac{1}{(i-1)!}, the sum is 1+1+12+16+⋯1+1+\frac12+\frac16+\cdots, which approaches the number e, e≈2.71828e \approx 2.71828: six terms already give 2.7167.

سەیری گەشەکردنی کۆکە بکە. لەبەر ئەوەی ii!=1(i−1)!\frac{i}{i!}=\frac{1}{(i-1)!}، کۆکە دەبێتە 1+1+12+16+⋯1+1+\frac12+\frac16+\cdots کە لە ژمارەی e (e≈2.71828e \approx 2.71828) نزیک دەبێتەوە: شەش بڕ پێشتر ⁦2.7167⁩ دەدەن.

Animation in six frames: a bar chart of the running total of i/i! for i = 1 to 6, next to a dashed line at e = 2.71828. Each frame adds one bar: 1.0000, 2.0000, 2.5000, 2.6667, 2.7083, 2.7167, and shows the term added, from + 1/1! = 1 down to + 6/6! = 0.0083. The last bar almost touches the e line.
Animation in six frames: a bar chart of the running total of i/i! for i = 1 to 6, next to a dashed line at e = 2.71828. Each frame adds one bar: 1.0000, 2.0000, 2.5000, 2.6667, 2.7083, 2.7167, and shows the term added, from + 1/1! = 1 down to + 6/6! = 0.0083. The last bar almost touches the e line.
The running total of i/i! approaches eکۆی بەردەوامی ⁦i/i!⁩ لە e نزیک دەبێتەوەCêIT
Quick checkپشکنینی خێراWhat is total after the loop for n=3n = 3?بۆ n=3n = 3، دوای total چەندە؟

MATLAB for Engineers - Introduction to for Loops (Part 2 of 4): Updating Variables Inside a LoopSpartan Professor · Englishئینگلیزی · 60K viewsبینین · 1.1K likesلایک

A for loop adds up a series: a running total is updated in every turn, shown first as a flowchart, then as a MATLAB script.

کۆی چەند ژمارەیەک دەدۆزێتەوە: لە هەر نوێ دەکرێتەوە؛ سەرەتا وەک ، پاشان وەک MATLAB.

Problem (0:00), loop structure (2:03), flowchart (3:15), MATLAB script (6:24).

کێشەکە ⁦(0:00)⁩، پێکهاتەی ⁦(2:03)⁩، ⁦(3:15)⁩ و ⁦MATLAB (6:24)⁩.

Adding up in a loop: the accumulatorکۆکردنەوە لەناو خولێکدا: گۆڕاوی کۆکردنەوە

Spot the mistake: factorial(n)هەڵەکە بدۆزەرەوە: ⁦factorial(n)⁩

A second lab program should add 1!+2!+⋯+n!1!+2!+\cdots+n!. It contains one small slip — find it before reading on:

دووەمی تاقیگە دەبێت 1!+2!+⋯+n!1!+2!+\cdots+n! کۆ بکاتەوە. هەڵەیەکی بچووکی تێدایە — پێش ئەوەی بخوێنیتەوە بیدۆزەرەوە:

The lab's program (with total instead of sum)بەرنامەی تاقیگە (بە total لە جیاتی sum)MATLABType in a terminalلە تێرمیناڵدا بینووسە
total = 0;
n = input('Enter the last number: ');
for i = 1:n
    total = total + factorial(n);
end
total

The answer should be 1+2+6+24=331 + 2 + 6 + 24 = 33, but the program prints 96. A (checking with a trace table) shows why: the term never changes, because it uses n (always 4) instead of the loop variable i. The program adds 4!=244! = 24 four times.

وەڵامەکە دەبێت 1+2+6+24=331 + 2 + 6 + 24 = 33 بێت، بەڵام ⁦96⁩ چاپ دەکات. (پشکنین بە خشتەی بەدواداچوون) هۆکارەکەی پیشان دەدات: بڕەکە هەرگیز ناگۆڕێت، چونکە n (هەمیشە ⁦4⁩) بەکار دەهێنێت لە جیاتی i. 4!=244! = 24 چوار جار کۆ دەکاتەوە.

Trace table for n=4n = 4 بۆ n=4n = 4
ifactorial(n)total (wrong)total (هەڵە)factorial(i)total (right)total (دروست)
1242411
2244823
3247269
424962433
Quick checkپشکنینی خێراA loop body that never uses the loop variable…ناوەڕۆکی کە هەرگیز بەکار ناهێنێت…

An alternating series: J = -Jزنجیرەیەکی ئاڵوگۆڕ: J = -J

Some series change sign: plus, minus, plus, minus. The first lab program does this with a sign variable J that starts at 1 and is flipped with J = -J at the end of every turn. The loop variable counter runs over the odd numbers 1,3,5,…,2n+11, 3, 5, \ldots, 2n+1, so the program computes

هەندێک زنجیرە نیشانە دەگۆڕن: کۆ، کەم، کۆ، کەم. یەکەم تاقیگە ئەمە بە نیشانەی J دەکات کە بە 1 دەست پێدەکات و لە کۆتایی هەر بە J = -J هەڵدەگەڕێتەوە. counter بەسەر ژمارە تاکەکانی 1,3,5,…,2n+11, 3, 5, \ldots, 2n+1ـدا دەڕوات، بۆیە ئەمە هەژمار دەکات:

y=∑k=0n(−1)k x2k+1(2k+2)!=x2!−x34!+x56!−⋯y=\sum_{k=0}^{n}(-1)^k\,\frac{x^{2k+1}}{(2k+2)!}=\frac{x}{2!}-\frac{x^3}{4!}+\frac{x^5}{6!}-\cdots
Lab program 1, re-typed (run with n=2n = 2, x=1x = 1)بەرنامەی یەکی تاقیگە، دووبارە نووسراوەتەوە (جێبەجێکردن بە n=2n = 2، x=1x = 1)series1.mMATLABType in a terminalلە تێرمیناڵدا بینووسە
n = input('n=');
x = input('x=');
J = 1;
y = 0;
for counter = 1:2:2*n+1
    y = y + J*((x^counter)/factorial(counter+1));
    J = -J;
end
disp(y);
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 3

J holds the sign of the next term.

J نیشانەی بڕی دواتر هەڵدەگرێت.

Lineدێڕی 5

1:2:2*n+1 gives 1, 3, 5 when n=2n = 2: three turns.

1:2:2*n+1 بۆ n=2n = 2 دەبێتە 1، 3، 5: سێ .

Lineدێڕی 7

Flip the sign for the next turn.

نیشانەکە بۆ دواتر هەڵبگەڕێنەوە.

Trace table for n=2n = 2, x=1x = 1 بۆ n=2n = 2، x=1x = 1
TurncounterJ in this turnJ لەم Term addedبڕی زیادکراوy after the turny دوای
1111⋅12!=+0.50001\cdot\frac{1}{2!} = +0.50000.5000
23−1-1−1⋅14!=−0.0417-1\cdot\frac{1}{4!} = -0.04170.4583
3511⋅16!=+0.00141\cdot\frac{1}{6!} = +0.00140.4597

So y=12!−14!+16!=0.4597y = \frac{1}{2!} - \frac{1}{4!} + \frac{1}{6!} = 0.4597. With more terms the sum approaches 1−cos⁡xx\frac{1-\cos x}{x}; for x=1x = 1 that is 0.45970, so three terms already give four correct decimals.

کەواتە y=12!−14!+16!=0.4597y = \frac{1}{2!} - \frac{1}{4!} + \frac{1}{6!} = 0.4597. بە بڕی زیاتر کۆکە لە 1−cos⁡xx\frac{1-\cos x}{x} نزیک دەبێتەوە؛ بۆ x=1x = 1 ئەوە ⁦0.45970⁩ـە، بۆیە سێ بڕ پێشتر چوار ژمارەی دروستی دوای دەدەن.

Quick checkپشکنینی خێراWhat is J during the fourth turn (if n were 3)?لە چوارەمدا J چەندە (ئەگەر n=3n = 3 بوایە)؟

An alternating series: J = -Jزنجیرەیەکی ئاڵوگۆڕ: J = -J

The MATLAB way: the same sum in one lineڕێگای MATLAB: هەمان کۆ لە یەک دێڕدا

MATLAB was built for whole arrays, so a sum like this is often written without a loop: make the vector of all k values, compute every term at once with the element-by-element operators .^, .* and ./, and add them with sum. factorial also works on a whole vector. (The element-by-element operators are taught in array operations and sum in functions for arrays.)

MATLAB بۆ تەواو دروست کراوە، بۆیە کۆیەکی وەک ئەمە زۆرجار بێ دەنووسرێت: هەموو k دروست بکە، هەموو بڕەکان بەیەکەوە بە بە .^، .* و ./ هەژمار بکە و بە sum کۆیان بکەرەوە. factorialـیش لەسەر تەواو کار دەکات. ( بە لە کارەکانی ڕیزەک و sum لە کردارەکانی ڕیزەکدا دەخوێنرێن.)

No loop, same answerبێ خول، هەمان وەڵامMATLABType in a terminalلە تێرمیناڵدا بینووسە
n = 2; x = 1;
k = 0:n;
y = sum((-1).^k .* x.^(2*k+1) ./ factorial(2*k+2))
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

k is [0 1 2].

k دەبێتە [0 1 2].

Lineدێڕی 3

The three terms are made at once and added.

سێ بڕەکە بەیەکەوە دروست دەکرێن و کۆ دەکرێنەوە.

Both versions are correct. The loop shows each step and is easy to trace; the one-line version is shorter and usually faster in MATLAB. Learn to read both.

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

Quick checkپشکنینی خێراWhy is .^ used instead of ^ in (-1).^k?بۆچی لە (-1).^kـدا .^ بەکارهاتووە لە جیاتی ^؟

Trace the loop: lab program 5خولەکە بەدوادا بچۆ: بەرنامەی پێنجی تاقیگە

Some programs are best understood with pencil and paper. Trace this lab program for n=1n = 1, x=1x = 1 before you look at the output: write one row per turn with counter, b and y.

لە هەندێک بە قەڵەم و کاغەز باشتر تێدەگەیت. پێش ئەوەی سەیری بکەیت، ئەم تاقیگە بۆ n=1n = 1، x=1x = 1 بەدوادا بچۆ: بۆ هەر دێڕێک بنووسە بە counter، b و y.

Lab program 5, re-typed (run with n=1n = 1, x=1x = 1)بەرنامەی پێنجی تاقیگە، دووبارە نووسراوەتەوە (جێبەجێکردن بە n=1n = 1، x=1x = 1)series5.mMATLABType in a terminalلە تێرمیناڵدا بینووسە
n = input('n=');
x = input('x=');
y = 0;
b = n;
for counter = 1:2:2*n+1
    y = y + 1 + (sin(x^b))^counter/factorial(counter)
    b = n + 1;
end
disp(y);
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 6

No semicolon: y is printed in every turn.

نییە: y لە هەموو چاپ دەکرێت.

Lineدێڕی 7

b changes only once: from n to n + 1, and then it stays n + 1.

b تەنها جارێک دەگۆڕێت: لە n بۆ n + 1 و دوای ئەوە n + 1 دەمێنێتەوە.

Trace table for n=1n = 1, x=1x = 1 بۆ n=1n = 1، x=1x = 1
TurncounterbTermبڕy
1111+sin⁡(1)11!=1.84151 + \frac{\sin(1)^1}{1!} = 1.84151.8415
2321+sin⁡(1)33!=1.09931 + \frac{\sin(1)^3}{3!} = 1.09932.9408
ExerciseڕاهێنانWhat did the program probably mean?بەرنامەکە لەوانەیە مەبەستی چی بووبێت؟

The output is 2.9408. Name three things in the code that look unintended.

⁦2.9408⁩ـە. سێ شت لە کۆدەکەدا ناو ببە کە وا دیارە مەبەست نەبوون.

Show answerوەڵامەکە پیشان بدە
  • b is n in the first turn and n + 1 in every later turn — a variable that changes only once probably belongs before the loop, or should depend on counter.
  • y + 1 + … adds 1 in every turn, so the result grows by the number of turns; a series usually adds only the term.
  • The missing ; prints every middle value of y — useful for tracing, noisy in a finished program.
  • b لە یەکەمدا n و لە هەموو دواتردا n + 1ـە — کە تەنها جارێک بگۆڕێت لەوانەیە پێش بێت، یان دەبێت پەیوەست بێت بە counter.
  • y + 1 + … لە هەموو 1 زیاد دەکات، بۆیە ئەنجامەکە بە ئەندازەی ژمارەی گەورە دەبێت؛ زنجیرە بە زۆری تەنها بڕەکە زیاد دەکات.
  • ;ـی کەم هەموو ناوەڕاستی y چاپ دەکات — بۆ بەدواداچوون بەسوودە، لە تەواوبوودا ژاوەژاوە.

Because the lab sheet does not state the series, the program's intention cannot be known for sure; tracing is how you find out what code really does.

لەبەر ئەوەی پەڕەی تاقیگەکە زنجیرەکە نانووسێت، مەبەستی بە دڵنیاییەوە نازانرێت؛ بەدواداچوون ڕێگای زانینی ئەوەیە کە کۆد بەڕاستی چی دەکات.

Searching while looping: largest and smallestگەڕان لە کاتی خولانەوەدا: گەورەترین و بچووکترین

A loop can also search. A lab program reads five numbers and keeps the largest and the smallest seen so far — the same idea as the largest of three algorithm, now with a loop. The key line is the start: maxi and mini both begin as the first number. Then the loop reads numbers 2 to 5 and replaces a record whenever a new number beats it:

دەتوانێت بگەڕێتیش. تاقیگە پێنج ژمارە دەخوێنێتەوە و گەورەترین و بچووکترینی تا ئێستا بینراو هەڵدەگرێت — هەمان بیرۆکەی گەورەترینی سێ ژمارە، ئێستا بە . دێڕە گرنگەکە سەرەتاکەیە: maxi و mini هەردووکیان بە یەکەم ژمارە دەست پێدەکەن. پاشان ژمارەکانی 2 تا 5 دەخوێنێتەوە و هەر کاتێک ژمارەیەکی نوێ ڕیکۆردێک تێپەڕێنێت، دەیگۆڕێت:

Largest and smallest of five inputs (prompts corrected)گەورەترین و بچووکترینی پێنج نێوخستە (دەقی داواکردنەکان چاککراون)max_min.mMATLABType in a terminalلە تێرمیناڵدا بینووسە
number = input('First number: ');
maxi = number;
mini = number;
for i = 2:5
    number = input('Next number: ');
    if maxi < number
        maxi = number;
    end
    if mini > number
        mini = number;
    end
end
maxi
mini
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2-3

Start from the first number, not from 0.

لە یەکەم ژمارەوە دەست پێبکە، نەک لە 0.

Lineدێڕی 4

The loop starts at 2 because number 1 has already been read.

لە 2 دەست پێدەکات چونکە ژمارەی 1 پێشتر خوێندراوەتەوە.

Lineدێڕی 13-14

The names maxi and mini wisely avoid hiding MATLAB's max and min.

ناوەکانی maxi و mini بە ژیرییەوە max و minـی MATLAB ناشارنەوە.

Trace for 4,9,−1,7,34, 9, -1, 7, 3 بۆ 4,9,−1,7,34, 9, -1, 7, 3
Turnnumbermaximini
startسەرەتا444
2994
3−1-19−1-1
479−1-1
539−1-1

Why not start maxi at 0? Because for the inputs −4,−9,−1,−7,−3-4, -9, -1, -7, -3 no number is bigger than 0, and the program would wrongly report 0. Starting from the first input is always safe. The lab's prompts (“give me a newe numbr”) were also corrected. When the numbers are already in a vector, the built-in functions do the search in one call: max([4 9 -1 7 3]) is 9 and min(...) is −1-1 (functions for arrays).

بۆچی maxi بە 0 دەست پێنەکات؟ چونکە بۆ −4,−9,−1,−7,−3-4, -9, -1, -7, -3 هیچ ژمارەیەک لە 0 گەورەتر نییە و بە هەڵە 0 ڕادەگەیەنێت. دەستپێکردن لە یەکەم هەمیشە سەلامەتە. تاقیگەکەش («⁦give me a newe numbr⁩») چاک کراونەتەوە. کاتێک ژمارەکان پێشتر لە ، بە یەک بانگکردن گەڕانەکە دەکەن: max([4 9 -1 7 3]) دەکاتە 9 و min(...) دەکاتە −1-1 (کردارەکانی ڕیزەک).

Quick checkپشکنینی خێراThe inputs are −5,−2,−8,−3,−6-5, -2, -8, -3, -6. What does the program report? ئەمانەن: −5,−2,−8,−3,−6-5, -2, -8, -3, -6. چی ڕادەگەیەنێت؟

Decisions inside a loopبڕیاردان لەناو خولێکدا

The body of a loop can hold any commands — also an if or a switch, so each turn can make its own decision. A lab program reads numbers one by one and says whether each is positive, negative or zero (if/elseif/else):

ناوەڕۆکی دەتوانێت هەر هەڵبگرێت — if یان switchـیش، بۆیە هەر دەتوانێت بڕیاری خۆی بدات. تاقیگە ژمارەکان یەک یەک دەخوێنێتەوە و دەڵێت هەریەکەیان موجەبە، سالبە یان سفرە (⁦if/elseif/else⁩):

The sign of each number (a sample run)نیشانەی هەر ژمارەیەک (جارێک جێبەجێکردن)MATLABType in a terminalلە تێرمیناڵدا بینووسە
for i = 1:4
    number = input(['Number ', num2str(i), ': ']);
    if number > 0
        fprintf('%g is positive\n', number)
    elseif number < 0
        fprintf('%g is negative\n', number)
    else
        disp('0 is zero')
    end
end
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 1

The lab reads 15 numbers (for i = 1:15); 4 keep the sample run short.

تاقیگەکە ⁦15⁩ ژمارە دەخوێنێتەوە (for i = 1:15)؛ 4 ژمارە نموونەکە کورت دەکەنەوە.

Lineدێڕی 2

The prompt is built from text and num2str(i), so it shows the turn number.

لە دەق و num2str(i) دروست دەکرێت، بۆیە ژمارەی پیشان دەدات.

Lineدێڕی 4

The lab printed number without ; and then disp('positive') — two lines per number; one fprintf is tidier.

تاقیگەکە numberـی بێ ; چاپ دەکرد و پاشان disp('positive') — بۆ هەر ژمارەیەک دوو دێڕ؛ یەک fprintf ڕێکترە.

Quick checkپشکنینی خێراHow many times does the if test run in this program?لەم پشکنینی if چەند جار جێبەجێ دەبێت؟

Decisions inside a loopبڕیاردان لەناو خولێکدا

A switch inside a loop: three accumulatorsswitch لەناو خولێکدا: سێ گۆڕاوی کۆکردنەوە

Another lab program reads several numbers and finds the sum of the positive ones, the sum of the negative ones and the number of zeros. It uses sign(x), which gives 1, −1-1 or 0 (sign), as the switch value, and three accumulators. Here it is as given, with its output for the inputs 4,−2,0,54, -2, 0, 5:

تری تاقیگە چەند ژمارەیەک دەخوێنێتەوە و کۆی ، کۆی و ژمارەی سفرەکان دەدۆزێتەوە. sign(x) بەکار دەهێنێت، کە 1، −1-1 یان 0 دەدات (sign)، وەک switch، لەگەڵ سێ . ئەمە وەک خۆیەتی، لەگەڵ بۆ 4,−2,0,54, -2, 0, 5:

Lab sheet 2, program 2 (re-typed)پەڕەی تاقیگەی 2، بەرنامەی 2 (دووبارە نووسراوە)MATLABType in a terminalلە تێرمیناڵدا بینووسە
n = input('how many number you want enter:');
s1 = 0;
s2 = 0;
s3 = 0;
ze = 1;
for i = 1:n
    n = input('enter numbers: ');
    m = sign(n);
    switch (m)
        case 1
            s1 = s1 + n;
        case -1
            s2 = s2 + n;
        case 0
            s3 = s3 + ze;
    end
end
disp('summation of +ve numbers:'), s1
disp('summation of -ve numbers:'), s2
disp('mumber of zeros:'), s3

It works, but it is hard to read. n means how many numbers in line 1 and then the current number in line 7. That does not break the loop — 1:n was worked out once, before the first turn (the loop variable belongs to the loop) — but a reader must stop and think. ze is just the number 1, the names s1, s2, s3 say nothing, and the output says “mumber”. The cleaned-up version:

کار دەکات، بەڵام خوێندنەوەی سەختە. n لە دێڕی 1دا واتە چەند ژمارە و پاشان لە دێڕی 7دا واتە ژمارەی ئێستا. ئەمە تێک نادات — 1:n جارێک پێش یەکەم هەژمار کرا (گۆڕاوی خول هی خولەکەیە) — بەڵام خوێنەر دەبێت بوەستێت و بیر بکاتەوە. ze تەنها ژمارە 1ـە، ناوەکانی s1، s2، s3 هیچ نالێن و دەڵێت «mumber». وەشانە ڕێکخراوەکە:

Clear names, one job per variableناوی ڕوون، بۆ هەر گۆڕاوێک یەک کارcount_signs.mMATLABType in a terminalلە تێرمیناڵدا بینووسە
count = input('How many numbers? ');
sum_pos = 0;          % sum of the positive numbers
sum_neg = 0;          % sum of the negative numbers
zero_count = 0;       % how many zeros
for i = 1:count
    x = input('Number: ');
    switch sign(x)
        case 1
            sum_pos = sum_pos + x;
        case -1
            sum_neg = sum_neg + x;
        case 0
            zero_count = zero_count + 1;
    end
end
fprintf('Sum of positive numbers: %g\n', sum_pos)
fprintf('Sum of negative numbers: %g\n', sum_neg)
fprintf('Number of zeros: %d\n', zero_count)
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2-4

Three accumulators, all set to 0 before the loop. Never call one sum or zeros — both are MATLAB functions.

سێ ، هەموویان پێش 0 کراون. هەرگیز ناوی یەکێکیان مەنێ sum یان zeros — هەردووکیان MATLABن.

Lineدێڕی 13

A zero counter just adds 1.

سفر تەنها 1 زیاد دەکات.

Quick checkپشکنینی خێراWhy does the lab program still run 4 turns although n changes inside the loop?بۆچی تاقیگە هێشتا چوار جێبەجێ دەکات هەرچەندە n لەناو دەگۆڕێت؟

A loop inside a loop: filling a tableخولێک لەناو خولێکدا: پڕکردنەوەی خشتەیەک

A runs completely for every turn of the outer loop: for each row i, the inner loop visits every column j. The course guide fills a times table this way. Make the matrix at its final size first, with zeros, and then fill it — this saves MATLAB from growing the matrix again and again, and MathWorks' own for example does the same:

بۆ هەر دەرەوە بە تەواوی جێبەجێ دەبێت: بۆ هەر ڕیزێکی i، ناوەوە سەردانی هەموو ستوونێکی j دەکات. ڕێبەرەکەی وانەکە بەم شێوەیە خشتەیەکی لێکدان پڕ دەکاتەوە. سەرەتا بە zeros بە قەبارەی کۆتایی دروست بکە و پاشان پڕی بکەرەوە — ئەم MATLAB ڕزگار دەکات لە گەورەکردنی جار لە دوای جار و نموونەی forـی خودی MathWorksـیش هەمان کار دەکات:

A 4×64 \times 6 times tableخشتەی لێکدانی 4×64 \times 6MATLABType in a terminalلە تێرمیناڵدا بینووسە
T = zeros(4, 6);       % preallocation: the table first
for i = 1:4             % rows
    for j = 1:6         % columns
        T(i, j) = i*j;
    end
end
T
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 1

zeros(4, 6) makes a 4×64 \times 6 matrix of zeros (see ready-made matrices and reaching one element).

zeros(4, 6) 4×64 \times 6 لە سفر دروست دەکات (بڕوانە ماتریکسەکانی ئامادە و گەیشتن بە یەک توخم).

Lineدێڕی 4

This line runs 4×6=244 \times 6 = 24 times.

ئەم دێڕە 4×6=244 \times 6 = 24 جار جێبەجێ دەبێت.

The Python track builds patterns and tables with the same shape of loops (nested loops).

بەشی Python بە هەمان شێوەی نەخشە و خشتە دروست دەکات (خولە ناوەکییەکان).

Quick checkپشکنینی خێراHow many times does the inner body run for for i = 1:3, for j = 1:5, … end, end?بۆ for i = 1:3, for j = 1:5, … end, end ناوەڕۆکی ناوەوە چەند جار جێبەجێ دەبێت؟

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

MistakeهەڵەWhat happensچی ڕوودەداتFixچارەسەر
An accumulator called sum (or a counter called zeros) بە ناوی sum (یان بە ناوی zeros)MATLAB's function is hidden until clear sum. MATLAB دەشاردرێتەوە تا clear sum.total, sum_pos, zero_counttotal، sum_pos، zero_count
factorial(n) instead of factorial(i)factorial(n) لە جیاتی factorial(i)Every turn adds the same term.هەموو هەمان بڕ زیاد دەکات.Use the loop variable in the term.لە بڕەکەدا بەکار بهێنە.
total = 0 inside the looptotal = 0 لەناو The total is reset every turn; only the last term is left.کۆکە لە هەموو سفر دەکرێتەوە؛ تەنها دوایین بڕ دەمێنێتەوە.Set it once, before the loop.جارێک، پێش ، دایبنێ.
No total = 0 at allهیچ total = 0 نییەAn error, or an old total from the workspace is added to.، یان کۆیەکی کۆن لە زیادی بۆ دەکرێت.Always start the accumulator yourself.هەمیشە خۆت دەست پێبکە.
Starting maxi at 0دەستپێکردنی maxi بە 0Wrong when all numbers are negative.کاتێک هەموو ژمارەکان سالبن هەڵەیە.Start from the first number.لە یەکەم ژمارەوە دەست پێبکە.
for i = 1:n when n was never setfor i = 1:n کاتێک n هەرگیز وەرنەگرتووەAn error, or an old n from the workspace decides the number of turns.، یان nـێکی کۆن لە ژمارەی خولەکان دیاری دەکات.Read or set n before the loop.پێش n بخوێنەوە یان دایبنێ.
Looping over a column vectorخولانەوە بەسەر ستوونیداOnly one turn, with the whole column.تەنها یەک ، بە هەموو ستوونەکەوە.for x = v'for x = v'
Changing the loop variable inside the body to skip turnsگۆڕینی لەناو ناوەڕۆکدا بۆ تێپەڕاندنی No effect on the next turn.هیچ کاریگەرییەکی لەسەر دواتر نییە.Choose the right vector (1:2:9), or use a while loop. دروست هەڵبژێرە (1:2:9)، یان خولی while بەکار بهێنە.

Practiceڕاهێنان

ExerciseڕاهێنانSum of squaresکۆی دووجاکان

Add 12+22+⋯+1021^2+2^2+\cdots+10^2 with a loop and check the answer with the formula n(n+1)(2n+1)6\frac{n(n+1)(2n+1)}{6}.

بە 12+22+⋯+1021^2+2^2+\cdots+10^2 کۆ بکەرەوە و وەڵامەکە بە n(n+1)(2n+1)6\frac{n(n+1)(2n+1)}{6} بپشکنە.

Show answerوەڵامەکە پیشان بدە
MATLABType in a terminalلە تێرمیناڵدا بینووسە
total = 0;
for k = 1:10
    total = total + k^2;
end
total
check = 10*11*21/6
ExerciseڕاهێنانFactorial with a loopفاکتۆریێل بە خولێک

Compute 6! by multiplying in a loop, without factorial. What must the accumulator start at?

⁦6!⁩ بە لێکدان لەناو هەژمار بکە، بێ factorial. دەبێت بە چی دەست پێبکات؟

Show answerوەڵامەکە پیشان بدە
MATLABType in a terminalلە تێرمیناڵدا بینووسە
p = 1;              % a product starts at 1, not 0
for k = 1:6
    p = p*k;
end
p

A sum starts at 0; a product starts at 1. Do not call it prod — that is also a MATLAB function.

کۆ بە 0 دەست پێدەکات؛ لێکدان بە 1. ناوی مەنێ prod — ئەوەش MATLABـە.

ExerciseڕاهێنانApproaching πنزیکبوونەوە لە π

Compute 4∑k=099(−1)k2k+14\sum_{k=0}^{99}\frac{(-1)^k}{2k+1} with a sign variable that flips (J = -J), and compare with pi.

4∑k=099(−1)k2k+14\sum_{k=0}^{99}\frac{(-1)^k}{2k+1} بە نیشانە کە هەڵدەگەڕێتەوە (J = -J) هەژمار بکە و لەگەڵ pi بەراوردی بکە.

Show answerوەڵامەکە پیشان بدە
MATLABType in a terminalلە تێرمیناڵدا بینووسە
total = 0;
J = 1;
for k = 0:99
    total = total + J/(2*k + 1);
    J = -J;
end
approx = 4*total

100 terms give 3.1316 — still 0.01 from π≈3.1416\pi \approx 3.1416. This series is famous but slow.

⁦100⁩ بڕ ⁦3.1316⁩ دەدەن — هێشتا ⁦0.01⁩ لە π≈3.1416\pi \approx 3.1416 دوورە. ئەم زنجیرەیە ناسراوە بەڵام هێواشە.

Exerciseڕاهێنانe from 1/k!e لە ⁦1/k!⁩ـەوە

Add ∑k=0101k!\sum_{k=0}^{10}\frac{1}{k!} in a loop and compare with exp(1).

∑k=0101k!\sum_{k=0}^{10}\frac{1}{k!} لەناو کۆ بکەرەوە و لەگەڵ exp(1) بەراوردی بکە.

Show answerوەڵامەکە پیشان بدە
MATLABType in a terminalلە تێرمیناڵدا بینووسە
total = 0;
for k = 0:10
    total = total + 1/factorial(k);
end
format long
total
exp(1)
format default

Eleven terms agree with e to 7 decimals. (format long shows more digits; see display formats.)

یازدە بڕ تا حەوت ژمارەی دوای لەگەڵ e یەک دەگرنەوە. (format long ژمارەی زیاتر پیشان دەدات؛ بڕوانە شێوەکانی پیشاندان.)

ExerciseڕاهێنانTrace the row operationsکارەکانی ڕیز بەدوادا بچۆ

The course guide shows a loop that subtracts each row from the next one. Trace this version with new numbers and write the final matrix: A = [2 1 3; 4 1 5; 6 2 7]; for j = 2:3, A(j,:) = A(j,:) - A(j-1,:); end

ڕێبەرەکەی وانەکە پیشان دەدات کە هەر ڕیزێک لە ڕیزی دواتر کەم دەکاتەوە. ئەم وەشانە بە ژمارەی نوێ بەدوادا بچۆ و بنووسە: A = [2 1 3; 4 1 5; 6 2 7]; for j = 2:3, A(j,:) = A(j,:) - A(j-1,:); end

Hintئاماژە

A(j,:) means the whole row j. Write the matrix after each turn.

A(j,:) واتە هەموو ڕیزی j. دوای هەر بنووسە.

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

Turn j=2j = 2: row 2 becomes [4 1 5]−[2 1 3]=[2 0 2][4\ 1\ 5] - [2\ 1\ 3] = [2\ 0\ 2]. Turn j=3j = 3: row 3 becomes [6 2 7]−[2 0 2]=[4 2 5][6\ 2\ 7] - [2\ 0\ 2] = [4\ 2\ 5] — it uses the new row 2.

j=2j = 2: ڕیزی 2 دەبێتە [4 1 5]−[2 1 3]=[2 0 2][4\ 1\ 5] - [2\ 1\ 3] = [2\ 0\ 2]. j=3j = 3: ڕیزی 3 دەبێتە [6 2 7]−[2 0 2]=[4 2 5][6\ 2\ 7] - [2\ 0\ 2] = [4\ 2\ 5] — ڕیزی 2ـی نوێ بەکار دەهێنێت.

MATLABType in a terminalلە تێرمیناڵدا بینووسە
A = [2 1 3; 4 1 5; 6 2 7];
for j = 2:3
    A(j,:) = A(j,:) - A(j-1,:);
end
A
ExerciseڕاهێنانCount the passesدەرچووەکان بژمێرە

Read 6 marks with a loop and print how many are 50 or more and the highest mark.

بە شەش نمرە بخوێنەوە و چاپ بکە چەندیان ⁦50⁩ و سەرووترن و بەرزترین نمرە چەندە.

Hintئاماژە

A counter and a running maximum in the same loop; start the maximum from the first mark.

و گەورەترینێکی بەردەوام لە هەمان ؛ گەورەترین لە یەکەم نمرەوە دەست پێبکە.

Show answerوەڵامەکە پیشان بدە
MATLABType in a terminalلە تێرمیناڵدا بینووسە
passed = 0;
best = input('Mark 1: ');
if best >= 50
    passed = passed + 1;
end
for i = 2:6
    m = input(['Mark ', num2str(i), ': ']);
    if m >= 50
        passed = passed + 1;
    end
    if m > best
        best = m;
    end
end
fprintf('%d marks of 50 or more; highest %g\n', passed, best)

Summaryکورتە

  • for k = v … end runs the body once for each value of the row vector v: ranges, countdowns and lists all work; a column vector gives one turn.
  • The loop variable gets the next value at the start of each turn; changes inside the body do not skip turns.
  • An accumulator starts at 0 (a product at 1) before the loop and grows by one term per turn; never name it sum.
  • Use the loop variable in the term (factorial(i), not factorial(n)); J = -J makes signs alternate.
  • Trace tables reveal what a loop really does; the same sum can often be written in one line with .^, ./ and sum.
  • Searching: start the maximum and minimum from the first value; decisions (if, switch) can sit inside a loop.
  • Nested loops fill tables; preallocate with zeros first.
  • for k = v … end ناوەڕۆکەکە بۆ هەر ڕیزیی v جارێک جێبەجێ دەکات: زنجیرە، ژماردنی بەرەو خوار و لیست هەموویان کار دەکەن؛ ستوونی یەک دەدات.
  • لە سەرەتای هەر دواتر وەردەگرێت؛ گۆڕانکاری لەناو ناوەڕۆکدا تێناپەڕێنێت.
  • پێش بە 0 دەست پێدەکات (لێکدان بە ⁦1⁩) و لە هەر بە یەک بڕ گەورە دەبێت؛ هەرگیز ناوی مەنێ sum.
  • لە بڕەکەدا بەکار بهێنە (factorial(i)، نەک factorial(n))؛ J = -J نیشانەکان ئاڵوگۆڕ دەکات.
  • ئاشکرای دەکەن بەڕاستی چی دەکات؛ هەمان کۆ زۆرجار بە .^، ./ و sum لە یەک دێڕدا دەنووسرێت.
  • گەڕان: گەورەترین و بچووکترین لە یەکەم دەست پێبکە؛ بڕیار (if، switch) دەتوانێت لەناو بێت.
  • خشتە پڕ دەکەنەوە؛ سەرەتا بە zeros بکە.
Review questionsپرسیارەکانی پێداچوونەوە
What does for k = 4:-1:1 do with k?for k = 4:-1:1 چی بە k دەکات؟

It takes 4, 3, 2, 1 — four turns, counting down.

دەبێتە 4، 3، 2، 1 — چوار ، بەرەو خوار.

Why must total = 0 come before the loop?بۆچی دەبێت total = 0 پێش خولەکە بێت؟

Inside the loop it would wipe the running total in every turn.

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

The lab's factorial(n) program gives 96 for n=4n = 4. What should it give, and why?بەرنامەی factorial(n)ـی تاقیگە بۆ n=4n = 4 ئەنجامی ⁦96⁩ دەدات. دەبێت چی بدات و بۆچی؟

1!+2!+3!+4!=331! + 2! + 3! + 4! = 33. The term must use i, which changes every turn.

1!+2!+3!+4!=331! + 2! + 3! + 4! = 33. بڕەکە دەبێت i بەکار بهێنێت، کە لە هەموو دەگۆڕێت.

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

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

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

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

while Loops, break and continueخولەکانی while، break و continue

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

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