溪边树

象一棵树,栽在溪水旁。
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Common Programming Errors -- Chapter 2

Posted on 2010-01-21 15:28  溪边树  阅读(212)  评论(0编辑  收藏  举报

All the following words are from the book "C++ How to Program, Fifth Edition", H.M. Deitel. Any commercial using is firmly denied.

Common Programming Errors 2.1

The syntax of a programming language specifies the rules for creating a proper program in that language. A syntax error occurs when the compiler encounters code that violates C++'s language rules (i.e. its syntax). The compiler normally issues an error message to help the programmer locate and fix the incorrect code. Syntax errors are also called compiler errors, compile-time errors or compilation errors, because the compiler detects them during the compilation phase. You will be unable to execute your program until you correct all the syntax errors in it.

Common Programming Errors 2.2

Confusing the equality operator == with the assignment operator = resutls in logic errors. The equality operator should be read "is equal to", and the assignment operator should be read "gets" or "gets the value of" or "is assigned the value of." Some people prefer to read the equality operator as "double equals." Confusing these operators may not necessarily cause an eay-to-recognize syntax error, but may cause extremly subtle logic errors. 

Common Programming Errors 2.3

Placing a semicolon immediately after the right parenthesis after the condition in an if statement is often a logic error (although not a syntax error). The semicolon causes the body of the  if statement to be empty, so the  if statement performs no action, regardless of whether or not its condition is true. Worse yet, the original body statement of the  if statement now would become a statement in sequence with the  if statement and would always execute, often causing the program to produce incorrect results.