PP Template

PROGRAMMING POINTERS, LESSON 2

 

 

Syntax/correctness issues

 

2-1        Watch out for the results of the division operator (/).  The answer of 22/4 is 5, not 5.5 as you might expect.  If you want floating point division to occur, one of the two operands must be a float or you need to use a type conversion like this:

 

             int  a = 22, b = 4;

             float  answer;

 

             answer = float (a)/b;

 

2-2        The modulus operator (%) can only be used with integer operands.

 

             7 % 2 results in 1

             7.5 % 2  compiler error

 

2-3        Do not use a reserved word as an identifier.

            

2-4        Reserved words must be used in lower case text only.

 

2-5        Declared variables are uninitialized until you initialize them.  All uninitialized variables start with garbage information.

 

 

Formatting suggestions

 

2-6        When declaring multiple variables on the same line separated by commas, add a space after each comma to make the line more readable.

 

             int      num1,  num2,  num3;                  // easier to read with spaces

             float   value1,value2,value3;     // harder to read without spaces

 

2-7        You must be consistent in your use of lower case and upper case letters with identifiers.  You might consider the convention used in the curriculum guide:

 

             single word identifiers - all lower case                             number, sum, value

             multiple word identifiers

                  first word all lower case

                  subsequent words begin with upper case                    capLetter, dayOfWeek, sizeOfList

 

 

 

 

 

 

Software engineering

 

2-8        It is probably best to declare all variables at the beginning of the function.  A blank line should be inserted after the declaration lines and before the first executable line.  This helps to separate variable declarations from the executable statements.

 

2-9        If you prefer to declare variables in the midst of executable statements, add a blank line above and below the declaration line to highlight it.