Loop trace tableخشتەی بەدواداچوونی خول
A shows what happens inside a : one row for every step, one column for every . Step through the same program in , or , like in an , guess each new value before the computer shows it and collect stars.
پیشان دەدات لە ناو چی ڕوودەدات: بۆ هەر هەنگاوێک و بۆ هەر . هەنگاو بە هەنگاو بە ناو هەمان بڕۆ بە ، یان ، وەک لە ، پێش ئەوەی نیشانت بدات هەر نوێ پێشبینی بکە و ئەستێرە کۆ بکەرەوە.
OUTPUT
What a trace table isخشتەی بەدواداچوون چییە
A table you fill in by hand to follow a program or an step by step: one column for each and for the , one row for each step. It shows exactly where a happens.
خشتەیەکە بە دەست پڕی دەکەیتەوە بۆ ئەوەی هەنگاو بە هەنگاو بەدوای یان بچیت: ستوونێک بۆ هەر و بۆ ، هەروەها ڕیزێک بۆ هەر هەنگاوێک. بە وردی پیشان دەدات لە کوێدا ڕوو دەدات.
Make one column for every , plus columns for the line number and the .
بۆ هەر دروست بکە، لەگەڵ ژمارەی دێڕ و .
Run the code in your head, one line at a time, from the top.
کۆدەکە لە مێشکی خۆتدا جێبەجێ بکە، هەر جارە دێڕێک، لە سەرەوە.
Every time a value changes, write a new row with the new value. Never rub out old rows: they show the history.
هەر جارێک دەگۆڕێت، نوێ بە نرخە نوێیەکە بنووسە. مەسڕەوە: مێژووەکە پیشان دەدەن.
At a loop line, write whether the loop runs again (and the new value of its ).
لە دێڕی ، بنووسە کە ئایا دووپاتە دەخولێتەوە یان نا (لەگەڵ نرخی نوێی ).
Compare your table with what the prints. If they differ, you have found the .
خشتەکەت لەگەڵ ئەوەی چاپی دەکات بەراورد بکە. ئەگەر جیاواز بوون، دۆزیوەتەوە.
The first program, tracedیەکەم بەرنامە، بەدواداچوونی بۆ کراوە
total = 0
for i in range(1, 6):
total = total + i
print(total)int total = 0;
for (int i = 1; i <= 5; i++) {
total = total + i;
}
System.out.println(total);<?php
$total = 0;
for ($i = 1; $i <= 5; $i++) {
$total = $total + $i;
}
echo $total;The Python trace is below. In Java and PHP the loop line appears more often: the i <= 5 is checked before every round and i++ runs after it, so at the end i is 6.
خشتەی Python لە خوارەوەیە. لە Java و PHPـدا دێڕی زیاتر دەردەکەوێت: i <= 5 پێش هەموو خولێک دەپشکنرێت و i++ دوای ئەو جێبەجێ دەبێت، بۆیە لە کۆتاییدا i دەبێتە 6.
| Lineدێڕ | i | total | Output |
|---|---|---|---|
| 1 | 0 | ||
| 2 | 1 | ||
| 3 | 1 | ||
| 2 | 2 | ||
| 3 | 3 | ||
| 2 | 3 | ||
| 3 | 6 | ||
| 2 | 4 | ||
| 3 | 10 | ||
| 2 | 5 | ||
| 3 | 15 | ||
| 4 | 15 |
Read more: for loops and range, while loops, nested loops and errors and debugging.
زیاتر بخوێنەوە: خولی for و range، خولی while، خولی ناوەکی و هەڵە و کرمەکوژی.