Basic Program Construction (Lecture-4)
Basic Program Construction (Lecture-4)
Basic Program Construction (Lecture-4)
Lecture 4
1
A (Very) Simple C++ Program
Probably the best way to start learning a programming language is by writing a program.
Therefore, the above shall be our first program.
2
Structure of a C++ Program
This is a comment line. All lines beginning with two slash signs (//) are
considered comments and do not have any effect on the behavior of the
program. The programmer can use them to include short explanations or
observations within the source code itself. In this case, the line is a brief
description of what our program is.
3
Structure of a C++ Program
Lines beginning with a hash sign (#) are preprocessor directives. They are
not regular code lines with expressions but indications for the compiler's
preprocessor. In this case the directive #include<iostream> tells the
preprocessor to include the iostream standard file. This specific file
(iostream) includes the declarations of the basic standard input-output
library in C++, and it is included because its functionality is going to be
used later in the program.
4
Structure of a C++ Program
All the elements of the standard C++ library are declared within what is
called a namespace, the namespace with the name std. So in order to
access its functionality we declare with this expression that we will be using
these entities.
5
Structure of a C++ Program
The return statement causes the main function to finish. return may be
followed by a return code (in our example is followed by the return code
with a value of zero). A return code of 0 for the main function is generally
interpreted as the program worked as expected without any errors during
its execution. This is the most usual way to end a C++ console program.
8
Structure of a C++ Program
You may have noticed that not all the lines of this program perform
actions when the code is executed. There were lines containing only
comments (those beginning by //). There were lines with directives for the
compiler's preprocessor (those beginning by #). Then there were lines that
began the declaration of a function (in this case, the main function
9
The program has been structured in different lines in
order to be more readable, but in C++, we do not
have strict rules on how to separate instructions in
different lines. For example, instead of
All in just one line and this would have had exactly the same
meaning as the previous code.
10
In C++, the separation between statements is specified with an ending
semicolon (;) at the end of each one, so the separation in different
code lines does not matter at all for this purpose.
We can write many statements per line or write a single statement that
takes many code lines. The division of code in different lines serves
only to make it more legible and schematic for the humans that may
read it.
11
What are the differences and similarities between the
following programs?
13
Special Characters
14
Special Characters(Escape Sequences)
15
Fix this program & Add “AAiT” on
new line
16