Errors and comments
In this lesson: you will read an error message and fix the error; you will write a comment in a program.
Lesson 2
In this lesson: you will read an error message and fix the error; you will write a comment in a program.
In this lesson: you will read an error message and fix the error; you will write a comment in a program.
All programmers make mistakes. What matters is that you can read the error message and fix the error. This program has an error. Run it:
1тағ синну = 15;2агр (синну > 14) {3чоп("Шумо метавонед бозӣ кунед");4}
The playground (in Tajik) will say:
Хато дар сатри 2:
Дар ин ҷо `{` интизор набуд: шояд пеш аз он `;` намерасад.
2 | агр (синну > 14) {
| ^
Маслиҳат: Шояд `агар`-ро дар назар доштед?
Барнома компайл нашуд: 1 хато.This is how to read the message:
^ sign show where exactly in the line.агар ("if") is spelled wrong: агр.The playground page is in Tajik and Russian. If you switch it to Russian, the messages will be in Russian too. Replace агр with агар and run it again.
Some errors appear only while the program is running. They are called runtime errors (Хатои иҷро):
1тағ номҳо = ["Алӣ", "Зарина", "Фарҳод"];2чоп(номҳо[5].дарозӣ);
The playground will say "Хатои иҷро дар сатри 2" ("runtime error on line 2"), tell you what happened, and give a hint. We will understand errors like this better in the next lessons.
A comment is text that the computer does not run. It is written for people:
1// This program prints a greeting2чоп("Салом!"); // a comment can be at the end of a line34/* A comment5on several lines */
// starts a comment that goes to the end of the line./* … */ is a comment on several lines.| Mistake | Message |
|---|---|
чоп("Салом); | Нохунаки пӯшандаи " намерасад (no quote mark) |
чоп("Салом"; | Қавси пӯшандаи ) намерасад (no bracket) |
агр (х > 1) {} | Маслиҳат: Шояд агар-ро дар назар доштед? |
чоп(ном); without тағ ном | Номи ном эълон нашудааст (not declared) |
This program has a mistake. Fix it so that two lines are printed.
Input: None.
Output: Салом, дӯстон! and Ман хатоҳоро ислоҳ мекунам. on two lines.
This program has two mistakes with brackets. Fix them so that Як, Ду and Се are printed on three lines.
Input: None.
Output: Як, Ду, Се, each on its own line.
In this program, two keywords are spelled wrong. Fix them.
Input: None.
Output: Калон