Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

Lecture 3

Uploaded by

h7ussin3
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lecture 3

Uploaded by

h7ussin3
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

‫جامعة بغداد‪ /‬كلية التربية للعلوم الصرفة ابن الهيثم‪/‬قسم علوم الحاسبات‬

‫المرحلة االولى‪ /‬نظري‬


‫السنة الدراسية ‪2024-2023‬‬

‫‪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.

• The following steps, as shown in figure 1, are necessary to


process a C++ program.
1. You use a text editor to create a C++ program following the rules,
or syntax, of the high-level language. This program is called the
source code, or source program. The program must be saved in a
text file that has the extension .cpp.
Introduction to C++
2. In a C++ program, statements that begin with the symbol # are called
preprocessor directives. These statements are processed by a program
called preprocessor.

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.

As a programmer, you need to be concerned only with Step 1. That is,


you must learn, understand, and master the rules of the programming
language to create source programs
The Basics of a C++ Program
1. A C++ program is a collection of one or more subprograms, called
functions.

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. To write meaningful programs, you must learn the programming


language’s.
The Basics of a C++ Program
• Special symbols, words, syntax rules, and semantic rules.
• Syntax rules tell you which statements (instructions) are legal, or
accepted by the programming language, and which are not.
• Semantic rules tell you which determine the meaning of the
instructions.

4. Function: is a collection of statements, and when it is executed, it


accomplishes something.
Comments in C++ Program
1. It is a notes which putting on your program to explain something.

2. It gives a brief explanation of the program, and explain the meaning


of key statements in a program.

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.

• Whitespaces In a C++ program, whitespaces are used to separate special


symbols, reserved words, and identifiers. Whitespaces are nonprintable in
the sense that when they are printed on a white sheet of paper, the space
between special symbols, reserved words, and identifiers is white. Proper
utilization of whitespaces in a program is important. They can be used to
make the program readable.
• Example:
• int x;
whitespace
Getting Started with C++
• To begin learning C++ let’s examine the structure of C++ Program:
#include<iostream.h>
main ( )
{

// A statements for main program

}
• #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.

You might also like