Slide Set - 09

Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

Slide Set – 09

CP & CPSA
17ME & 17PG
Contents

• endl manipulator (LL 02)


• Escape sequences (LL 02)
• Comments (LL 02)
• Types of comments (LL 02)
• Single-Line comment (LL 02)
• Multi-Line comment (LL 02)

• Comments example (LL 02)


LL 02 = Learning Level 02 – Comprehension
endl Manipulator

endl End Line

• The endl is one of the manipulator which changes the format of cout
statement.
• It brings the text on new line.
• It is used with in cout statement.
• The name endl is included in std namespace.
endl Manipulator
#include<iostream>
#include<conio.h>

using namespace std;

int main()
{
cout<<“17-Batch"<<endl;
cout<<"MUET"<<endl;
cout<<"Jamshoro";
getch();
return 0;
}
endl Manipulator
Output on monitor screen
endl Manipulator
Same program can also be written as

#include<iostream>
#include<conio.h>

using namespace std;

int main()
{
cout<<“17-Batch"<<endl<<"MUET"<<endl<<"Jamshoro";
getch();
return 0;
}
endl Manipulator
Same program can also be written as

#include<iostream>
#include<conio.h>

int main()
{
std::cout<<“17-Batch"<<std::endl;
std::cout<<"MUET"<<std::endl;
std::cout<<"Jamshoro";
getch();
return 0;
}
Escape Sequence

• Escape sequence refers to a combination of characters beginning with a back


slash ( \ ) followed by letters or digits.
• Escape sequence is two or more characters that often begin with an escape
character ( \ ) that tells the computer or software program to perform a
function or command.
• Escape sequences represent non-printable and special characters in
character literal or string literal.
Escape Sequence

• When ever we write character literal it is always enclose in single quotes ( ‘ ),


string in double quotes ( “ ) and the escape sequence always start with
escape character ( \ ).
• So ( ‘ ), ( “ ) and ( \ ) have special meaning that is why when we want to print
single quote, double quote or slash itself, we have to use escape sequence.
• To print single quote use ( \‘ ), to print double quote use ( \” ) and to print
slash use ( \\ ).
Escape Sequence
Escape Sequence Description

\' Single Quote


\" Double Quote
\? Question Mark
\\ Backslash
\b Backspace
\n Line Feed - New Line
\r Carriage Return
\t Horizontal Tab
Escape Sequence ( \n )
#include<iostream>
#include<conio.h>

using namespace std;

int main()
{
cout<<“17-Batch\nMUET\nJamshoro";
getch();
return 0;
}
Escape Sequence ( \n )
Same program can also be written as
#include<iostream>
#include<conio.h>

using namespace std;

int main()
{
cout<<“17-Batch\n";
cout<<"MUET\n";
cout<<"Jamshoro";
getch();
return 0;
}
Escape Sequence ( \n )
Escape Sequence ( \t )
#include<iostream>
#include<conio.h>

using namespace std;

int main()
{
cout<<“17-Batch\tMUET\tJamshoro";
getch();
return 0;
}
Escape Sequence ( \t )
Escape Sequence ( \’ ) ( \” ) ( \\ )
#include<iostream>
#include<conio.h>

using namespace std;

int main()
{
cout<<"\“17-Batch\" \n";
cout<<"\'MUET\'\t \\Jamshoro";
getch();
return 0;
}
Escape Sequence ( \’ ) ( \” ) ( \\ )
Escape Sequence

cout Statement Output


cout<<"17-Batch"; 17-Batch
cout<<"\“17-Batch\""; “17-Batch”
cout<<"\‘17-Batch\'"; ‘17-Batch’
cout<<“17-Batch\tMUET"; 17-Batch MUET
cout<<“17-Batch\\MUET"; 17-Batch\MUET
17-Batch
cout<<“17-Batch\nMUET";
MUET
Comments

• Comment is the text that is used to explain the code inside the program.
• A comment is a line (or multiple lines) of text that are inserted into the source code
to explain what the code is doing.
• The programmer writes the comments in the program to make is easier to read for
other persons (who want to read it).
• The comments are always ignored (not executed) by the compiler.
• The comments are non-executable statements in the program.
• Compiler does not consider comments as part of the program.
• Comments are also helpful during debugging the program.
Types of Comments

• There are two types of comments in C++


Single-Line Comment

• Single-Line comment, comments out entire line of the code.


• It starts with double slash // .
• Any text written after // is ignored by the compiler and is considered as the
comment.

For Example:
getch(); //gets single character from the keyboard
Multi-Line Comment
• Multi-Line comment, comments out multiple lines of the code.
• It starts with slash asterisk /* .
• It ends with asterisk slash */.
• Any text written in between /* and */ is ignored by the compiler and is
considered as the comment.
For Example:
/*This program displays string on screen
It is written by Anam Memon
It is a basic C++ program */
Comments Example
/*This program displays string on screen
It is written by Anam Memon
All the text in GRAY
It is a basic C++ program */ color is comment

//These are header files


#include<iostream>
#include<conio.h>

using namespace std;

//Every C++ program starts with main function


int main()
{
cout<<"\“17-Batch\" \n"; //Prints string on screen
cout<<"\'MUET\'\t \\Jamshoro"; //Prints string on screen
getch(); //Gets single character from the keyboard
return 0; //Returns 0 to the operating system
}
//Program ends here

You might also like