Dealing with Syntax Errors


Syntax errors

You might have found already that you tend to make errors when writing programs. Some errors are just typographical. For example, you might have misspelled a name, or you might have left out a right parenthesis that is needed. Sometimes errors occur because you did something that the programming language does not allow, or because you misunderstood how to do things. Some are errors in judgment about what needs to be done. You are not alone. Everybody makes mistakes when writing computer programs.

If the compiler detects an error, it will give you some indication of what the error was. What it says is not always clear, but it is worth looking at. In particular, at least look at the line that the compiler reports. Look at that line and the line just before that one. If you see the error, fix it and try again.

If you cannot find the error, it is important to ask for help. Do not be embarassed because you made an error. Resist the temptation to try random changes in hopes of making the error go away. You will almost certainly do more harm than good to your program that way, and you will spend a lot of your time in fruitless work. You want to work efficiently. An experienced person can tell you what is wrong very quickly.


Who's the boss?

Sometimes, when you make an error, what the compiler says suggests a way to fix the error. For example, if it says that there is no such thing as variable frug, then you might decide to create variable frug to make it happy.

It is important to keep in mind that compilers are not very smart. A compiler does not know what you are trying to do, and it can only tell you about what appears to be wrong. Often, what the compiler seems to suggest is the wrong thing to do. The right way to fix it depends on what you are trying to do. Maybe, instead of creating variable frug, you should just fix the spelling to frog.

Decide on your algorithm. If you get errors, never let the compiler bully you away from your algorithm. You must be the boss. Think about the right way to fix an error. If you cannot see how to fix an error without being pushed away from what you really want to do, ask for help.


Problems

  1. [solve] What is wrong with the following Let-statement?

      Let w = 100
    

  2. [solve] What is wrong with the following program?

      Let x = 25.
      Let y = (3*(4 + x* x) + 1.
    

  3. [solve] What is wrong with the following program?

      Let fruit = 50.
      Let cake = friut*3.
      Let pie = cake + 1.
    


Summary

Expect to make some errors. Everybody does. When you make them, try to find out what is wrong, and fix it.

Avoid random experimentation to fix an error. Not only is it a waste of your time, it is usually worse than doing nothing.