Identifying and Correcting Errors
導入済み
6:10
創造的開発
Here is a program that should print the average of two numbers. It runs. It does not crash. It prints an answer — and the answer is wrong. Give it four and six…
英語のナレーション・英語と中国語の字幕を切り替え可能
このレッスンの内容
| English | 意味 |
|---|---|
| bug/bʌɡ/ | バグ |
| debugging/ˈdiːbʌɡɪŋ/ | デバッグ |
| syntax error/ˈsɪntæks ˈerə/ | 構文エラー |
| logic error/ˈlɒdʒɪk ˈerə/ | 論理エラー |
| runtime error/ˈrʌntaɪm ˈerə/ | 実行時エラー |
| overflow error/ˌəʊvəˈfləʊ ˈerə/ | オーバーフローエラー |
| edge case/edʒ keɪs/ | エッジケース |
| expected output/ekˈspektɪd ˈaʊtpʊt/ | 期待される出力 |
すべてのプログラムにはバグがある
- チームがいくら慎重であっても、プログラムには間違いが含まれています。
- プログラム内の間違いをバグと呼びます。
- バグを見つけ、直すことをデバッグといいます。
- 高度なデバッグ能力は、プログラマーが身につける最も価値のあるスキルの一つです。
Which kind of error?
A syntax error stops the program running; a logic error runs but gives the wrong answer; a runtime error crashes it partway; an overflow error exceeds storage.
The work of finding and fixing mistakes in a program is called ______.
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
A mistake is a bug; fixing bugs is debugging.
A missing bracket that stops the program from running is a:
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
Breaking the language's rules is a syntax error.
Why are logic errors often the hardest to find?
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
Nothing obviously breaks, so you must compare output to what you expected.
エラーの主な種類
- 構文エラーは言語のルールに違反するもので(括弧の欠落など)、プログラムの実行を拒否します。
- ロジックエラーは実行されますが、指示が計画と一致しないため間違った結果を出力します。
- ランタイムエラーは、実行中にプログラムをクラッシュさせます(ゼロ除算などが該当)。
- オーバーフローエラーは、値が格納スペースを超えて大きくなるときに発生します。

トレース表は各変数の値をプログラム実行中に記録し、バグの特定に役立てます

Testing with edge cases (empty input, very large numbers) helps find hidden bugs.
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
Bugs often hide at the limits of what a program expects.
To spot a bug in a test, you compare the actual output with the:
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
A difference between actual and expected output points to a bug.
見つけ方
- 多くの入力でテストします。異常な入力(非常に大きい数や空の入力)や、限界におけるエッジケースを含めます。
- 実際の出力と期待される出力を比較します。違いがあれば、そこにバグがあります。
- 内部値を確認するために一時的なprint文を追加し、どこから誤りが生じるかを探します。
- 分割して確認します。ある部分が正しいことを確認し、残りの範囲を絞り込みます。
After fixing a bug, you only need to re-test the line you changed.
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
False. A fix can create a new bug elsewhere, so you should re-test the whole program after changing it.
修正後、再度テストする
- ロジックエラーは最も難しいです。明らかに「壊れて」いないのに、答えだけが間違っています。
- 一つのバグを直すと、別のバグを生むことがあります。
- したがって、すべての修正後に、プログラム全体を再テストする必要があります。
- 丁寧かつ繰り返しのテストによって、粗いプロトタイプが信頼できるソフトウェアになります。
「動いた」ことと「正しい」ことは別物です。 ロジックエラーはクラッシュもメッセージも出ずに動作しますが、プログラムは正常に見えますが答えは間違っています。これを見抜く唯一の方法は、単に動作したかを確認するだけでなく、出力を期待通りと比較することです。
このカードは新しいため、翻訳ができるまで英語で表示されます。
Worked example: finding a logic error
A program should find the average of a list of test scores. For the list [80, 90, 70] it prints 60 instead of 80. The code adds each score to a sum, then divides the sum by (length of list + 1). Classify and fix the bug.
- The program runs and prints a result, so it is not a syntax or runtime error: it is a logic error 逻辑错误. Checking by hand: the sum is 240, and $240\div(3+1)=60$, so the divisor is wrong.
- Fix: divide by the length of the list: $240\div3=80$. Then test with more cases, including an edge case such as an empty list, which would cause a runtime error (division by zero) unless the program checks for it first.
A program crashes with "division by zero" when the user enters 0. This is:
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
It happens while the program is running.
Testing only typical inputs is enough to find most bugs.
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
Edge cases and unusual inputs reveal many bugs.
バグとはミスであり、バグを修正することをデバッグといいます。構文エラーはプログラムの実行を停止させ、ロジックエラーは実行して結果が正しくなく、ランタイムエラーは途中でクラッシュさせ、オーバーフローエラーはストレージを超えます。多くの入力(特にエッジケース)に対して期待される出力と比較することでバグを見つけ、修正後必ず再テストしてください。
何が問題でしたか?この内容はサイトの運営チームだけが確認します。