Lecture 3
Lecture 3
Structured Programming
Lecture3 – C++Programming Language
Introduction to C++
C++: is a programming language.
• Programming language: A set of rules, symbols, and special
words.
• Source program: A program written in a high-level language.
3. The next step is to verify that the program obeys the rules of the
programming language—that is, the program is syntactically correct—
and the compiler translates the program into the equivalent machine
language which is called object program.
Introduction to C++
4. The programs that you write are developed using an integrated
development environment (IDE). The IDE contains many programs that
are useful in creating your program. Once the program is developed
and successfully compiled, you must still bring the code for the
resources used from the IDE into your program to produce a final
program that the computer can execute.
This prewritten code (program) resides in a place called the library. A
program called a linker combines the object program with the
programs from libraries to create the executable code.
Introduction to C++
5. You must next load the executable program into the main memory
for execution. A program called a loader accomplishes this task
6. The final step is to execute the program.
2. Every C++ program has a function called main. Thus, if a C++ program
has only one function, it must be the function main.
3. Comments are for the reader, not for the compiler. So when a
compiler compiles a program to check for the syntax errors, it
completely ignores comments.
Comments in C++ Program
4. There are two common types of comments in a C++ program.
• Single-line comments begin with // and can be placed anywhere in
the line.
Ex: cout << "7 + 8 = " << 7 + 8 << endl; //prints: 7 + 8 = 15
• Multiple-line comments are enclosed between /* and */. The
compiler ignores anything that appears between /* and */.
• For example, the following is an example of a multiple-line comment:
/*
• You can include comments that can occupy several lines.
• */
Token in C++ Program
• The smallest individual unit of a program written in any language is
called a token. C++’s tokens are divided into:
1. Special symbols.
2. Reserved word.
3. Identifiers.
1. Special symbols
The following are some of special symbols:
• Mathematical symbols: include (+ - * /) for addition, subtraction,
multiplication, and division.
Token in C++ Program
• Punctuation marks: include (. ; ? , ). These marks are taken from
English grammar. Note that the comma is also a special symbol. In
C++, commas are used to separate items in a list. Semicolons are used
to end a C++ statement.
• Comparison symbols: include (< =, >, > =, ! =, ==) for less than, less
and equal, greater than, greater and equal, not equal, equal)
• Character set: C++ has the letters and digits, as show below:
• Uppercase: A, B, C, . . ., Z
• Lowercase: a, b, c, . . ., z
• Digits: 0, 1, 2, . . .,9
Reserved words
• Some words are reserved by C++ (are parts of the C++ language) such
as main, for, while, if, int, float,… Reserved words can’t be used as
variable names or constant. Table 1 explains some examples of
reserved words.
Identifiers
• Identifiers are names of things that appear in programs, such as a
variable, a constant, an array, a function, a structure, or a class. Table
2 shows some illegal identifier description.
• Note: C++ is case sensitive—uppercase and lowercase letters are
considered different. Thus, the identifier NUMBER is not the same as the
identifier number. Similarly, the identifiers X and x are different.
}
• #include<iostream.h>
• This line is for pre-processor directive. Any begins with # is processed
before the program is compiled. C++ programs must be start with #include.
• Every group of related functions is stored in a separate library called
(header file). To use the cin and cout, must include the header file
iostream.
main ( ) is the name of C++ function.
• Every C++ program must have a function called main.
{
• indicates the start statements that define the function.
}
• indicates the end of the statements in the function.
• cout <<
• it is a C++ output statement. It causes the computer to evaluate the
expression after the pair of symbols << and display the result on the
screen.
• Note: << is an operator, called the stream insertion operator.
• cin >> The input stream object. It reads the input values from the
keyboard.
• << The stream insertion operator (or send operator).
• >> The stream extraction operator (or get from operator).
• ; , semicolon, the terminator of every C++ statement.
• Note: The endl is used in C++ to represent a new line, also
• \n is a special escape code, also used in C++ to represent a new line,
as shown in the following example:
C++ provides escape sequences for several usages. These escape
sequences are listed below:
Example 3: Write a C++ program to represent all escape sequences in
the above table.