Terms &
Programming Style in COMP 150
(Revised 01/06/2007)
Important and Useful Terms
syntax vs. semantics
- syntax = structure
- semantics = meaning
variable : variables are implemented as
named storage location in memory (see Data Types)
- In C++ variable declarations: storage for variables
must be declared before they are used; in Python variables are implicitly
declared when first used in an expression or assignment statement.
- uninitialized variables: values should be assigned to
variable before they are used in expressions
identifiers
- syntax : underscore or letter followed by zero or more
letters, digits or underscores
- in Python and C++ all identifiers are case sensitive
- keywords or reserved words : identifier which have
fixed meanings which cannot be changed - e.g. if, for, def, print etc.,
- C++ only: pre-defined identifiers are identifiers
defined in libraries required by C++ language
data type:
set of values plus operations on those values
- Python is very loosely typed; a variable conforms to
the type of value assigned to it; C++ requires that all variable to are
typed
- different data types require different amounts of
storage
- different data types use different 0/1 encodings
I/O streams:
streams of ASCII characters separated by newline characters
- Python I/O uses var = input(“ “)
to input values; print arg_list to output
values
- C++ I/O uses cin >> arg_list to input
values and cout << arg_list to output values
escape sequences (C++ only)
- '\n' - newline character
- '\n' is same as endl
- '\t' - tab
- '\0' - null character
numeric types
- int - whole numbers; no
decimal points - requires 4 bytes of storage
- float - decimal point - requires 4 bytes of storage
(stores exponent and fraction)
- double - decimal point - requires 8 bytes of storage
- 7 is an integer; 7.0 is a floating point; they have
different 0/1 representations
floating point types (type double)
- range
- with int, expressed as largest positive and largest
negative values representable
- with float, expressed
as largest and smallest positive values representable
- precision: number of significant digits; eg. 3.14159 is pi accurate to 7 decimal places
other types
- char - e.g 'A' - note single
quote; characters encoded a numeric ASCII codes
- strings e.g. "Wittenberg" - note double quote
- note '\n' is a
character type while "\n" is a string type.
- bool - only two values :
true and false
type compatability
- don't mix types as results may be unpredictable (be
careful of integer division)
- automatic type conversion rules
- int types easily promoted
to float type
- float types truncated
to int types
- char and bool types can be treated as byte length int types
assignment statements
- syntax : variable = expression;
- semantics: the expression on the right is evaluated and
its value is assigned to the variable on the left; there is a time
component to the semantics of an assignment statement.
arithmetic operations
- +,-,*,/,%.,** (Python only)
- with integer arguments,
/ returns an integer quotient
- with integer argument,
% return the integer remainder after integer division
- operator precedence
- anything in parentheses
- ++
(increment), -- (decrement)
- !
(not), - (unary minus)
- *, /, % multiplication, division, modulo left to right
- +. - addition, subtraction left to right
- <,
<=, >, >=
- ==,
!=
- &&
(and)
- ||
(or)
- difficulties with division operators / and %
- 17/5 returns 3
- 17.0/5.0 returns 3.4
- 17.0/5 and 17/5.0
return 3.4 via automatic type conversion of int
to float.
- avoid mixed mode expressions
- assignment operators : +=, -+, *=, /=, %=
Programming
Style Requirements - Level 1
General Rule: Form Follows
Function
1. Use meaningful identifier names for variables and
constants
A.
An identifier name should describe its use
B. Use lower case letters for variable identifiers
C. Use underscore or capital letters ("camel"
notation) to separate words in two word identifiers
example:
gross_pay or grossPay
D.
Use upper case letters for named constants (C++ only)
Example: const double PAY_RATE = 9.95;
E.
Declare constants before variables (C++ only)
2.
Code Layout: Indenting
A.
Indent the bodies of “while” and “for” loops at least 4 spaces
B. Indent statements subordinate to "if" and
"else" at least 4 spaces
C. Place braces that define the bodies of loops or
subordinate statements on separate lines (C++ only)
D. Indenting levels are cumulative!
3.
Code Layout: Vertical Spacing
A.
A group of statements that perform a well defined task is called a logical
block. Skip a line before and after logical block to separate them.
B. Skip lines before and after comments
C. An “if else” statement requires at least four lines since
the "if (condition)" and "else" appear on separate lines
4.
Comments
A.
Comments describe "what" is being done - not how
B. Comments must be clear, concise, correct and complete
C. Do not comment the obvious
D. Logical blocks of code should be commented
E. Attach comments to statements only if the code is obscure.
Return
to COMP 150 HomePage