Back to Course

Introduction to C++

0% Complete
0/0 Steps
  1. C++: Basics
    5Topics
    |
    6 Quizzes
  2. C++: Compilation and Execution
    5Topics
    |
    6 Quizzes
  3. C++: Variables
    9Topics
    |
    10 Quizzes
  4. C++: Conditions Operators
    5Topics
    |
    6 Quizzes
  5. C++: Logical Operators
    3Topics
    |
    4 Quizzes
  6. C++: Loops and Errors
    8Topics
    |
    9 Quizzes
  7. C++: Vectors
    4Topics
    |
    3 Quizzes
Lesson 1, Topic 4
In Progress

C++: Outputs

Yasin Cakal 26 Oct 2021
Lesson Progress
0% Complete

We have already seen how to print a line of text using the character output stream std::cout. Now, let’s move over to printing patterns and different lines of text.

We can add new lines in the code output by adding \n as shown in the code below.

#include <iostream>

int main()
{
    std::cout<<"Hello, World!\n";
    std::cout<<"Hope you're doing okay.";
}

Output:

Hello, World!
Hope you're doing okay.

As we can see, adding the \n statement has added another line in the output.

To print spaces between lines, we add an actual space between the " ". To practice this, try printing the following code.

#include <iostream>
 
int main()
{
    std::cout<<"a b cd ef";
}

Output:

a b cd ef

As observed, adding spaces in the stream can add spaces in the output. Let us print a few patterns now. Now, try printing the following pattern:

#include <iostream>
 
int main()
{
    std::cout<<"a\n";
    std::cout<<"a b\n";
    std::cout<<"a b c\n";
    std::cout<<"a b c d\n";
}

Output:

a
a b
a b c
a b c d

Try new codes to print new patterns and you will learn better along the way!

%d bloggers like this: