You can put as many statements as you like in the main function. For example, to display more than one line of output we could use two cout statements:
Notice that the first print statement has another << after the string it is printing to separate the string from the next piece of data to be output. That data is endl which means "end of line" - it will ensure that anything that whatever comes next in the output will appear on the next line (like hitting Enter while typing). Thus we get two lines of output.
Also notice that the program omits the return 0; at the end of main. In C++, it is optional to include that line - if the programmer does not include it, it is silently added at the end of main. It is also possible to return a value other than 0. When a program returns 0, that indicates to the operating system that the program completed successfully. To indicate an error occurred, the programmer can return any non-zero value. This is a C++ feature we will not be exploring in this book.
Lastly, it also demonstrates that you can put comments at the end of a line as well as on lines all by themselves. The code before the // is not part of the comment.
Without the endl the second print statement will output to the same line as the first one. The output has a space after the comma because there is an extra space inside the quotation marks of the first string. Without that space, the output would say βGoodbye,from C++β.
You can output as many things as you like in one statement of code by chaining together << operators. That can include as many (or few) endl as you like.
Construct a main function that prints βSnap!β on the first line, βCrackle!β on the third line, and βPop!β on the seventh line. You might not use all of endl blocks provided.
Construct a main function that prints βHello, world!β so that βHello,β and βworld!β are printed on two separate lines. You will not use all of the blocks.