CP 2 - Part 1
CP 2 - Part 1
The smallest individual unit of a program written in any language is called a token
A token in C++ may be the following:
➢ Keyword
➢ Identifier
➢ Constant
➢ Operator
CREATING A C++ PROGRAM
Compiler generates the object code and saved in a file with file extension
.obj
Executable code is produced and saved in a file with the file extension
.exe
PREPROCESSOR DIRECTIVE SYNTAX
Tell the compiler to preprocess the source code before actual compiling
process
All preprocessor commands begin with #
Many functions and symbols needed to run a C++ program are provided
as collection of libraries.
Every library has a name and is referred to by a header file
- , ➢ Include:
• int
* <=
• float
/ !=
• double
. == • char
; >= • return
SEMICOLONS, BRACKETS, COMMAS &
COMMENTS
Commas separate items in a list
All C++ statements end with a semicolon ( ; )
Use of Blanks
➢ One or more blanks separate input numbers
➢ Blanks are also used to separate reserved words and identifiers from
each other and other symbols
Blanks between identifiers in the second statement are meaningless:
➢ int a,b,c;
➢ int a , b , c;
In the statement: inta,b,c;
➢ No blank between the t and a changes the reserved word int and the
identifier a into a new identifier, inta.
IDENTIFIER
Output:
SIMPLE DATA TYPE –INTEGRAL (CONT.)
bool Data Type:
➢ Has two values, true and false
➢ Manipulate logical (Boolean) expressions
➢ true and false are called logical values
➢ bool, true, and false are reserved words
Output:
SIMPLE DATA TYPE –INTEGRAL (CONT.)
char Data Type:
➢ The smallest integral data type
➢ The character variable can store only one character
➢ Used for characters: letters, digits, and special symbols
➢ Some of the values belonging to char data type are: 'A', 'a', '0', '*', '+', '$’, '&'
➢ A blank space is a character and is written ' ', with a space left between the single
quotes
Output:
SIMPLE DATA TYPE – INTEGRAL (CONT.)
Global variable
Local variables
ASSIGNMENT STATEMENT
The assignment statement takes the form:
variable = expression;
Expression is evaluated and its value is assigned to the variable on the
left side
In C++, (=) is called the assignment operator
CONSTANT
Constants refer to fixed values that the program may not alter during
execution
Constants can be any of the basic data types
Defining Constants:
➢ There are two simple ways in C++ to define constants:
➢ Using #define preprocessor
➢ Using const keyword
In C++, const is a reserved word
Constants are treated just like regular variables except that their values
cannot be modified
CONSTANT (CONT.)
Constant Example:
INPUT AND OUTPUT
Output Statement
➢ To display output on screen or to write output into file
➢ Using cout keyword and output stream called insertion operator (<<)
➢ Syntax :
cout << <string | constant | variable | expression > ;
➢ Expression evaluated and its value is printed at current position on the screen
➢ Manipulator: alters output
➢ endl: the simplest manipulator
Output example:
➢ Output of the C++ statement
cout << a;
is meaningful if a has a value
➢ For example, the sequence of C++ statements,
a = 45;
cout << a;
produces an output of 45
INPUT AND OUTPUT (CONT.)
Example:
Output:
ARITHMETIC OPERATION
Example:
Output:
SEMANTICS
Possible to remove all syntax errors in a program and still not have it run
Even if it runs, it may still not do what you meant it to do
For example,
➢ 2 + 3 * 5 and (2 + 3) * 5
Algorithm
Start
1. Read length in feet
2. Read length in inch
3. Calculate the length in centimeter using formula:
totalInches = 12 * feet + inch;
totalCentimeter = 2.54 * totalInches;
4. Display totalCentimeter
End
PROGRAMMING EXAMPLE (CONT.)
Output:
SUMMARY