Professional Documents
Culture Documents
Preparing to program
Determine the objectives of the program
Determine the methods you want to use in writing the program
Create the program to solve the program
Run the program to see the result
4 The Basic of a C++ Program
Basic Structure
12
13 } // end function main
Right brace } end function body
6 The Basic of a C++ Program
Escape sequences
7 Data Types
C++ Data Type
Arithmetic operators
11 Arithmetic operators and Operator Precedence
Operators precedence
12 Identifier
An identifier is the name to denote labels, types, variables, constants or functions,
in a C++ program.
C++ is a case-sensitive language. (Work is not work )
Identifiers should be descriptive (Using meaningful identifiers is a good
programming practice)
Identifiers must be unique
Identifiers cannot be reserved words (keywords)
i.e.: double main return
Identifier must start with a letter or underscore, and be followed by zero or more
letters (A-Z, a-z), digits (0-9), or underscores
Valid (age_of_student, StudentAge, Age123)
Invalid (age#, Age-of-dog, 2000Age, main)
13 Identifier
C++ Keyword (cannot be used as identifier)
asm do if return typedef
auto double inline short typeid
bool dynamic_cast int signed typename
break delete long sizeof union
case else mutable static unsigned
catch enum namespace static_cast using
char explicit new struct virtual
class extern operator switch void
const false private template volatile
const_cast float protected this wchar_t
continue for public throw while
default friend register true union
delete goto reinterpret_cast try unsigned
14 Class Activity
Explain why the given identifiers are invalid, and please give the valid
identifiers
2morrow
Min Time
Age-70
Cost_in _$
int
Five*two
Name’s
short
15 Variable
A variable is a memory address where data can be stored and
changed.
Declaring a variable means specifying both its name and its data
type.
A declaration tells the compiler to allocate enough memory to hold
a value of this data type, and to associate the identifier with this
location.
All variables must be declared before use.
16 Variable
Variable declaration:
Example int age;
Data type Identifier ( variable’s name)
Commas are used to separate identifiers of the same type.
Example int number, age;
Can be initialized to a starting value when the are declared.
Example int age = 1;
int count, age = 0;
17 Variable
Variable Value
Assignment operator
converts x to double and then does mixed division, not integer division.
static_cast<int> (z) - will truncate z.
24 Decision Making: Equality & Relational
Operators
Usually used in “ if statement”, allows a program to take alternative action based
on whether a condition is true or false.
Condition can be formed using equality operators and relational operators.
The relational operators all have the same level of precedence and associate left to
right.
The equality operators both have the same level of precedence, which is lower
than that of the relational operators, and associate left to right.
25 Decision Making: Equality & Relational
Operators
Relational & Equality operators
26 Precedence and associativity of the operators
(as discussed above)
27 Increment & decrement operators
Always used in repetition structures
Increment operator ++
Post increment x++;
Pre increment ++x;
Decrement operator --
Post decrement x--;
Pre decrement --x;
28 Increment & decrement operators
29 Increment & decrement operators
Semantic
The meaning of instructions written in programming language.
35 Syntax, Semantic and Errors
Types of error
Syntax error
Occurs when a rule is not followed. Also known as compile time error.
Examples of syntax error:
Semantic error
Occurs when a statement written in the program is not meaningful to the compiler.
Example:
void main(){
int a, b, c;
a+b=c;
}
Logical error
Occurs when the desired output is not obtained.
Run-time error
Occurs during program execution after successful compilation. E.g. divison by a zero.
37 Class Activity
Case 1 Case 2
Answer
Case 1 – Syntax errors ({ : , )
Case 2 – Run time error ( a/b = /0 )
Case 3 – Logical error (while loop cant be executed)
Case 4 – Semantic error ((s/u)+a=r)