bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/C++/C++ Tutorial
C++•C++ Tutorial

C++ New Lines

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind C++ New Lines?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

#___ <iostream>
3Order

Put the learning moves in the order that makes the concept easiest to apply.

Another way to insert a new line, is with the endl manipulator:
You can also use another << operator and place the \n character after the text, like this:
To insert a new line in your output, you can use the \n character:

New Lines

To insert a new line in your output, you can use the \n character:

Example

#include <iostream>
using namespace std;
int main() {
  cout << "Hello World! \n ";
  cout << "I am learning C++";
  return 0;
}

You can also use another << operator and place the \n character after the text, like this:

Example

#include <iostream>
using namespace std;
int main() {
  cout << "Hello World!"
  << "\n";
  cout << "I am learning C++";
  return 0;
}

Tip

Two \n characters after each other will create a blank line:

Example

#include <iostream>
using namespace std;
int main() {
  cout << "Hello World!" << " \n\n ";
  cout << "I am learning C++";
  return 0;
}

Another way to insert a new line, is with the endl manipulator:

Example

#include <iostream>
using namespace std;
int main() {
  cout << "Hello World!" << endl ;
  cout << "I am learning C++";
  return 0;
}

Both \n and endl are used to break lines. However, \n is most used.

The newline character ( \n ) is called an escape sequence , and it forces the cursor to change its position to the beginning of the next line on the screen. This results in a new line.

Examples of other valid escape sequences are:

Escape SequenceDescription
\tCreates a horizontal tab
\\Inserts a backslash character (\)
\"Inserts a double quote character

Previous

C++ Output Numbers

Next

C++ Comments