Lesson 2

Errors and comments

Errors and comments

In this lesson: you will read an error message and fix the error; you will write a comment in a program.

The whole lesson on one page

Errors and comments

In this lesson: you will read an error message and fix the error; you will write a comment in a program.

Errors are normal

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:

  1. Хато дар сатри 2 means "error on line 2". The number of this line, to the left of the program, turns red.
  2. Next comes what is wrong.
  3. The line of the program and the ^ sign show where exactly in the line.
  4. Маслиҳат is a hint on how to fix it. Here агар ("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.

Errors while the program runs

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.

Comments

A comment is text that the computer does not run. It is written for people:

1// This program prints a greeting
2чоп("Салом!"); // a comment can be at the end of a line
3
4/* A comment
5 on several lines */
  • // starts a comment that goes to the end of the line.
  • /* … */ is a comment on several lines.

Common mistakes and their messages

MistakeMessage
чоп("Салом);Нохунаки пӯшандаи " намерасад (no quote mark)
чоп("Салом";Қавси пӯшандаи ) намерасад (no bracket)
агр (х > 1) {}Маслиҳат: Шояд агар-ро дар назар доштед?
чоп(ном); without тағ номНоми ном эълон нашудааст (not declared)

Task: The quote mark

This program has a mistake. Fix it so that two lines are printed.

Input: None.

Output: Салом, дӯстон! and Ман хатоҳоро ислоҳ мекунам. on two lines.

Task: The bracket

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.

Task: The keyword

In this program, two keywords are spelled wrong. Fix them.

Input: None.

Output: Калон