0% found this document useful (0 votes)
17 views6 pages

BasicSkills Printing CPP

Uploaded by

matureshorts1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views6 pages

BasicSkills Printing CPP

Uploaded by

matureshorts1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Learning Objectives: Printing

Use the cout command to write text to the console

Add a newline character by using the endl command

Create single-line and multi-line comments by using //


and /* */ respectively
Printing to the Console

Printing to the Console


As you learn C++, you will write code in the text editor to the left. There is
already some code there that we will cover later. For now, use a cout
command to see the output of your program. Copy and paste the code
below into the text editor on the left. Make sure your code is in between the
//add code below this line and //add code above this line comments.
Then click the TRY IT button to see what is outputted by the code.

cout << "Hello world!";

The reason you were able to see the words appear is because of the cout
command followed by the << and finally what you want to print "Hello
world";. cout is short for characters out and is used to output your desired
text.

Change your desired output to look like this and TRY IT again.

"My name is Codio."


Printing

Printing without the Newline Character


The cout command does not add a newline character. The code below will
print the two words on the same line without a space. Copy the code below
into the text editor on the left and then click the TRY IT button below to see
the output.

cout << "Hello";


cout << "world!";

Adding the Newline Character


The text in red shows the endl command which adds the newline
character. (The newline character is what is inserted when you press
“Enter” or “Return”).

.guides/img/NewlineCharacter

Add a second line by using the cout << endl; command under "cout <<
"world!";. Then enter the ouput command to print My name is Codio.
Finally, click the TRY IT button to see the resulting output.

cout << "Hello";


cout << "world!";
cout << endl;
cout << "My name is Codio." << endl;
challenge

What happens if you:


Add a space after Hello and before the closing "
Add << endl; after cout << "Hello ";
Delete cout << endl; under cout << "world!";
Comments

Comments
You may have wondered why a couple of lines of code are a different color
(in the below example, light brown, but it depends on the Theme you have
picked):

.guides/img/Comment

In C++, to write notes in code without affecting its function, we can use //
to make a single-line comment. Click the TRY IT button below to see the
resulting output.

Comments can also be used to help you fix your code. You can “comment
out” lines of code that are not working or you suspect are causing
problems.

challenge

What happens if you:


Change cout << "Red" to cut << "Red"
Add // in front of cut << "Red"

Block Comments
To make a block comment you can either make multiple single-line
comments using // or wrap the set of lines in /* and */. TRY IT below!
/*
This is the start of a multi-line comment.
You can end the comment with a star(*) followed by a forward
slash(/).
*/

//You can also do a multi-line comment


//like this!

cout << "Notice how the comments above are lightly faded.";
cout << "Most IDEs automatically lighten the comments.";
cout << "This is a common feature known as syntax
highlighting.";

What is an IDE?
An integrated development environment, or IDE, is a computer program that
makes it easier to write other computer programs. They are used by
computer programmers to edit source code, and can be easier to use than
other text editors for new programmers. They can have compilers so
programmers don’t have to open other programs to compile the source code.
They also often have syntax highlighting. … It also may have predictive
coding that can finish lines with syntax such as brackets or semicolons and
can suggest variables to be used. It also may have debuggers that can step
through lines, take breaks and inspect variables.
Source: Simple Wikipedia

You might also like