You are on page 1of 6

OUTPUT STATEMENTS

Subject Code CS 2 Computer Science 2


Module Code 3.0 Programming Basics
Lesson Code 3.5.1 Output Statements
Time Frame 30 minutes

Components Tasks TA ATA


(min) (min)b
a

Target By the end of this learning guide module, the students should be able to: 1

1. Understand the input output stream


2. Properly use output statements in a program
3. Create a basic code that is able to output text and variables.

Hook Every time you use the computer, you are using some form of input and output. 2
From your previous lessons about computers, remember that input can come from
devices such as your mouse, keyboard, joysticks or controllers while output is
sent to your device screens or printers.

When you log in to your accounts, you input your username and password and
only then will the website output the pertinent information you want to access.
When playing games, you input your username and the game displays it as you
play. When using Google, you input your keywords then the site outputs the
websites relevant to your search. There are many different ways in which you use
input and output.

This module describes the basic method of using output in C++.

CS 2 Page 1 of 6
Ignite C++ handles input and output through a stream. Streams are a source or 15
destination of characters and are a mode of means of data transmission.
Input stream: The flow of the data is from the user/device (ex. keyboard) to the
main memory. This topic will be discussed in the next lesson.
Output stream: The flow of the data is from the main memory to the user/device
(ex. display screen).
The Input/output (I/O) features of C++ are in a library called iostream. To use the
functionality of this library, we need to include the iostream header at the top of
the the code.

Standard Output (cout)


This is used to produce output on the device, which is usually the display screen.
The keyword used is cout which stands for Console OUTput, meaning you are
sending data to the console (device/display screen) to output or display. (This is
usually pronounced as (see-out).
The cout keyword is paired with the insertion operator ( << ). Strings, or series
of characters that you want to display are placed inside quotation marks (“ ”).
When text is placed inside quotation marks, it is displayed literally.
The example below sends the phrase Hello World! to the device to be displayed.

Output:
Hello World!

CS 2 Page 2 of 6
The insertion operator (<<) can also be used multiple times in one statement to
concatenate or combine multiple things to output.

Output:
This is going to print as a single line

Using multiple cout statements doesn’t necessarily mean you will display
multiple lines of output. For example, if you run the following code, you will
only get one line of output.

Output:
Hello World!Have a nice day!

Notice that there is no space between World! and Have. This is because text
inside quotation marks is displayed as is, so if there is no space indicated in the
text, there will also be no space in the output.
If you want to display multiple lines of output, you can use the endl keyword to
separate your text with a line break. The endl keyword signifies the end of the
line. You may also use the new-line character ( \n ) to insert a new line. The
new-line character is placed inside the double quotation marks.

Output:
Hello World!
Have a nice day!
See you later!

CS 2 Page 3 of 6
It is usually considered good programming practice to add endl or \n to the end
of your display lines.
You may also choose to use multiple insertion operators (<<) in a single line of
code to display multiple output lines.

Output:
Hello World!
Have a nice
day!
To print the values of variables, you simply place the variable name after the
insertion operator WITHOUT quotation marks.

Output:
I am 13 years old.
I am age years old.

The first line displays the contents of the variable age while the second line
literally takes the word age and prints it as is.

CS 2 Page 4 of 6
Navigate 8
Exercises

Part 1. What is the expected output of the following snippets?

1.

2.

3.

4.

Part 2. Write a code snippet that will display the following expected output.
Declare variables as needed. You may test your code on an IDE or online
compiler.

1. The PSHS in us will grow.


ABC
2. def
123

3. I was born on 2006. 2006 is the value of an integer


variable named year.
and grow as we wander oer crests is the value of a string
4. the crests and troughs variable named objectA and
of the sea of life that flows troughs is the value of a string
variable named objectB.

CS 2 Page 5 of 6
Knot Summary: 4

1. To use input and output functions, the library iostream needs to be


included at the top of the code.
2. An input stream means you are getting data from the user usually via a
keyboard
3. An output stream means you are passing data to the user via a display
screen
4. The keyword cout is used to output in C++ and it is used with the
insertion operator ( << ).
5. Text inside quotation marks are treated literally while text outside
quotation marks are recognized as variables
6. It is possible to print or display multiple lines by separating lines with
endl

a
TA – time allocation suggested by the teacher
b
ATA- actual time allocation spent by the student (for information purposes only)

References: using APA format (7th edition)

1. Albarico, J.M. (2013). THINK Framework. Based on Ramos, E.G. and N. Apolinario. (n.d.)
Science LINKS. Quezon City: Rex Bookstore Inc
2. THINK icons and blue car images by Clker-Free-Vector-Images from Pixabay
3. Basic Input/Output. (n.d.). Retrieved July 13, 2020 from
http://www.cplusplus.com/doc/tutorial/basic_io/
4. Basic Input Output (n.d.). Retrieved July 13, 2020 from https://www.geeksforgeeks.org/basic-
input-output-c/
5. Gaddis, T., & Manna, M. M. (2015). Chapter 2 Introduction to C++. In Starting out with C++:
From control structures through objects. Harlow, Essex: Pearson Education Limited.

Prepared by: Reviewed by:

Edlen Mari M. Sanchez Trextan Thaddeus Sanchez


Special Science Teacher II SST III
PSHS-MC PSHS- SMC

CS 2 Page 6 of 6

You might also like