Responsive Ad Slot

.

Religion

Coding

Global

Tech

Lesson 2 : Structure of a program (Part II)

Monday, 27 July 2015

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 us add an additional instruction to our first program.

This is input:                                                                      This is output:

// my second program in C++
#include <iostream>                                                           Hello World! I'm a C++ program
using namespace std;
int main ()
{
cout << "Hello World! ";
cout << "I'm a C++ program";
return 0;
}

 In this case, we performed two insertions into cout in two different statements. Once again, the separation in different lines of code has been done just to give greater readability to the program, since main could have been perfectly valid defined this way 
EXAMPLE:
int main () { cout << " Hello World! "; cout << " I'm a C++ program "; return 0; }

We were also free to divide the code into more lines if we considered it more convenient
EXAMPLE:
 int main ()
{
cout <<
"Hello World!";
cout
<< "I'm a C++ program";
return 0;
}
And the result would again have been exactly the same as in the previous examples.
Pre-processor directives (those that begin by #) are out of this general rule since they are not statements. They are lines read and processed by the pre-processor and do not produce any code by themselves. Pre-processor directives must be specified in their own line and do not have to end with a semicolon (;).

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.
C++ supports two ways to insert comments.
Which are:
// line comment
/* block comment */

The first of them, known as line comment, discards everything from where the pair of slash signs (//) is found up
to the end of that same line. The second one, known as block comment, discards everything between the /*
characters and the first appearance of the */ characters, with the possibility of including more than one line.
We are going to add comments to our second program:

This is input:                                                                          This is output:

/* my second program in C++
with more comments */
#include <iostream>                                                                Hello World! I'm a C++ program
using namespace std;
int main ()
{
cout << "Hello World! "; // prints Hello
World!
cout << "I'm a C++ program"; // prints I'm a
C++ program
return 0;
}

If you include comments within the source code of your programs without using the comment characters combinations //, /* or */, the compiler will take them as if they were C++ expressions, most likely causing one or several error messages when you compile it.

Thats it for today we will start a new topic tomorrow anyone that wants to ask any questions or make recommendations please feel free to do so.
             

No comments

Post a Comment

Popular Posts