C++ Programming - Structure of a program:
Other Points
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
int main ()
{
cout << " Hello World ";
return 0;
}
|
This also written as:
|
int main () { cout << "Hello World"; return 0; }
|
The both code has same output.
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.
Let modify our first program:
// my first program in C++
#include
using namespace std;
int main ()
{
cout << "Hello World! ";
cout << "This is my first C++ program";
return 0;
}
| Hello World! This is my first C++ program! |
In this case, we performed two insertions into cout in two different statements.
This also written as:
|
int main () { cout << "Hello World"; return 0; }
|
C++ Comments:
Comments are parts of the source code disregarded by the compiler. They simply do nothing. Their purpose is only to allow the programmer to insert notes or descriptions embedded within the source code.
There are two types of comments in C++:
// line comment
/* block comment */
|
Line Comment –
Discards everything from where the pair of slash signs (//) is found up to the end of that same line.
Block Comment –
Discards everything between the /* characters and the first appearance of the */ characters, with the possibility of including more than one line.
Sample of a c++ program that outputs two lines
/* second program in C++
with more comments */
#include
using namespace std;
int main ()
{
cout << "Hello World! "; // prints Hello World!
cout << "I'm a C++ programmer"; // prints I'm a C++ programmer
return 0;
| Hello World! This is my first C++ program! |