Handout 2

// To add comments to a C++ program, you include them after // or within /* */

// You can include comments anywhere in the program.

// It's up to you whether to use // or /* and */

// Notice that all the slashes you have are forward slashes not backward.

// Every program must include the following 2 statements.

/* For now just include these 2 statements at the beginning of your program. We'll explain what they mean later. */

/* Your future programs may have to include similar statements to the first or other kinds of statements. */

// Here is the first statement.

#include <iostream>

// And here is the second.

using namespace std;

// Notice that C++ ignores empty lines.

/* Notice also that C++ is case sensitive. So, you cannot change the letters in the previous 2 statements to upper case. */

/* Also every C++ program must contain the following line. We'll explain what it means later. */

int main()

// Next you must open left braces.

{

// Now you start writing what you want the program to do.

/* For example, let's ask our program to display on the screen "Welcome to" on a

line and then "SUNY Fredonia." on a second line. Here's how to do it:*/

cout<<"Welcome to";

// When the program is executed, the above statement will display Welcome to

// Notice that "cout<<" means "display on the screen"

/* Notice also that you need the thing you want displayed to be included within double

quotess. Everything inside the double quotes will be displayed exactly as it is. For

example, lower case letters will be displayed as lower case, upper case letters will be

displayed as upper case, space will be displayed exactly as it is, and so on.*/

/* Notice also that we need the semicolon at the end of the previous statement and so is in

every position where it appears. The semicoln means that the statement ends there.*/

// Now we want to jump to a second line. How to do it? Here is how:

cout<<endl;

/* "cout<<endl;" means "jump to a new line". If we want to continue writing on the same

line, then we don't include "cout<<endl;". If we want to leave one empty line, then we

must have "cout<<endl;" twice (in a consecutive order). Or you can instead include

"cout<<endl<<endl;" once. The result of the execution of this statement is the same as

the result of executing 2 statements of "cout<<endl;". Similarly, if you want to leave 2

empty lines, then you'll have to include the statement "cout<<endl;" 3 consecutive times.

Or instead you can just include "cout<<endl<<endl<<endl;" once. */

/* Now let's ask the computer to display on the screen "SUNY Fredonia.".

Here's how to do it: */

cout<<"SUNY Fredonia.";

/* once again notice that the lower case letters in main, cout, and endl cannot be changed to upper case. */

// Notice also that in "cout<<", you cannot leave space between the two <

// "<<" is taken as a unit. It does not mean "less than".

// Now include another "cout<<endl". I’ll explain why when we run (execute) the program.

cout<<endl;

// When done, you must include the following line and then a right braces.

return 0;

/* "The return statement signals the end of the program. Notice there is space between

return and 0.*/

}