0% found this document useful (0 votes)
68 views8 pages

Learning Outcomes:: What Is Cout Command For? How To Use Cout Command?

The document discusses the cout command in C++. It explains that cout is used for console output and displays text or variable values on the screen. It outputs data by using the output stream operator <<. Examples show how to display text and variables using cout. The endl command prints a new line, while "\n" can also be used to skip lines and format output. The document also introduces printf, which outputs with formatting characters. It includes an exercise to print a name and then a pattern of asterisks representing the first letter.

Uploaded by

Teo Pui Kuan
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)
68 views8 pages

Learning Outcomes:: What Is Cout Command For? How To Use Cout Command?

The document discusses the cout command in C++. It explains that cout is used for console output and displays text or variable values on the screen. It outputs data by using the output stream operator <<. Examples show how to display text and variables using cout. The endl command prints a new line, while "\n" can also be used to skip lines and format output. The document also introduces printf, which outputs with formatting characters. It includes an exercise to print a name and then a pattern of asterisks representing the first letter.

Uploaded by

Teo Pui Kuan
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/ 8

Learning Outcomes:

•What is cout command for?


•How to use cout command?

1
Prepared by Assoc Prof Dr Junita Mohamad Saleh
Console Output Command:
cout
 cout is a C++ command referred to as console output

 It is used to output or display text or values or data of variables on the 
computer screen

 It is used with output stream operator  <<.  Spaces before or after << does 
not matter.

 E.g. to display my name and age on the monitor screen, my C++ statements 
can be like so:
cout << “My name is Junita Mohamad Saleh.”;
cout << “I am “ << “15 years old.”;

The above C++ statements will output the following line:
My name is Junita Mohamad Saleh.I am 15 years old.
Prepared by Assoc Prof Dr Junita Mohamad Saleh 2
Type this and observe the output
#include <iostream>
using namespace std;
int main()
{
cout<<"Learn C++ without fear";
system("PAUSE");
return 0;
}

Prepared by Assoc Prof Dr Junita Mohamad Saleh 3


endl or “\n” command
 Means “end‐of‐line”.  Used to skip a line.

 Used only with cout command to format displayed output as many 
times as needed.

 E.g.  Given C++ statements below:
cout << “My name is Junita Mohamad Saleh.” << endl ;
cout << “I am 15 years old.”;
cout << “\n Notice that one line and one space are skipped”;

 The above C++ statements will output the following line:
My name is Junita Mohamad Saleh.
I am 15 years old.
Notice that one line  and one space are skipped 

Prepared by Assoc Prof Dr Junita Mohamad Saleh 4


Type this and observe the difference in 
output compared to the one before
#include <iostream>
using namespace std;
int main()
{
cout<<"Learn C++ without fear\n";
system("PAUSE");
return 0;
}

Prepared by Assoc Prof Dr Junita Mohamad Saleh 5


Another Console output command
printf
 Used with brackets
 Used with formatting character(s), e.g. “\n”, “\t”

#include <iostream>
using namespace std;
int main()
{
printf ("Learn C++ without fear\n“);
system("PAUSE");
return 0;
}
Prepared by Assoc Prof Dr Junita Mohamad Saleh 6
Exercise 1: Question
 Write a program that prints your full name on one line.  Then use 
asterisk (*) to make a display of the first alphabet of your name.  
Example display:
JUNITA MOHAMAD SALEH

*******
*
*
*
*    *
* *

Prepared by Assoc Prof Dr Junita Mohamad Saleh 7


Exercise 1: Solution
// This program displays my name in capital letters and
// display the pattern of the first alphabet of my name.

#include <iostream.h>
Using namespace std;
void main()
{
cout<<“JUNITA MOHAMAD SALEH”<<endl;
cout<<“ ********”<<endl;
cout<<“ *”<<endl;
cout<<“ *”<< “\n”;
cout<<“ *”<<endl;
cout<<“ * *\n”;
cout<<“ * *”<<endl;
}
Prepared by Assoc Prof Dr Junita Mohamad Saleh 8

You might also like