23
Lecture – 14 Computer Programming 14 Computer Systems Engineering – Second Semester By: Mr. Ali Asghar Manjotho, Lecturer, CSE-MUET

Computer programming lecture – 14

Embed Size (px)

Citation preview

Page 1: Computer programming lecture – 14

Lecture – 14Computer Programming

14 Computer Systems Engineering – Second Semester

By: Mr. Ali Asghar Manjotho, Lecturer, CSE-MUET

Page 2: Computer programming lecture – 14

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

Ali Asghar Manjotho, Lecturer CSE-MUET 2

Page 3: Computer programming lecture – 14

endl Manipulator

• 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.

Ali Asghar Manjotho, Lecturer CSE-MUET 3

endl End Line

Page 4: Computer programming lecture – 14

endl Manipulator

Ali Asghar Manjotho, Lecturer CSE-MUET 4

#include<iostream>

#include<conio.h>

using namespace std;

int main()

{

cout<<"14 Computer Systems"<<endl;

cout<<"MUET"<<endl;

cout<<"Jamshoro";

getch();

return 0;

}

Page 5: Computer programming lecture – 14

endl Manipulator

Ali Asghar Manjotho, Lecturer CSE-MUET 5

Output on monitor screen

Page 6: Computer programming lecture – 14

endl Manipulator

Ali Asghar Manjotho, Lecturer CSE-MUET 6

#include<iostream>

#include<conio.h>

using namespace std;

int main()

{

cout<<"14 Computer Systems"<<endl<<"MUET"<<endl<<"Jamshoro";

getch();

return 0;

}

Same program can also be written as

Page 7: Computer programming lecture – 14

endl Manipulator

Ali Asghar Manjotho, Lecturer CSE-MUET 7

#include<iostream>

#include<conio.h>

int main()

{

std::cout<<"14 Computer Systems"<<std::endl;

std::cout<<"MUET"<<std::endl;

std::cout<<"Jamshoro";

getch();

return 0;

}

Same program can also be written as

Page 8: Computer programming lecture – 14

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.

Ali Asghar Manjotho, Lecturer CSE-MUET 8

Page 9: Computer programming lecture – 14

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 ( \\ ).

Ali Asghar Manjotho, Lecturer CSE-MUET 9

Page 10: Computer programming lecture – 14

Escape Sequence

Ali Asghar Manjotho, Lecturer CSE-MUET 10

Escape Sequence Description

\' Single Quote

\" Double Quote

\? Question Mark

\\ Backslash

\b Backspace

\n Line Feed - New Line

\r Carriage Return

\t Horizontal Tab

Page 11: Computer programming lecture – 14

Escape Sequence ( \n )

Ali Asghar Manjotho, Lecturer CSE-MUET 11

#include<iostream>

#include<conio.h>

using namespace std;

int main()

{

cout<<"14 Computer Systems\nMUET\nJamshoro";

getch();

return 0;

}

Page 12: Computer programming lecture – 14

Escape Sequence ( \n )

Ali Asghar Manjotho, Lecturer CSE-MUET 12

#include<iostream>

#include<conio.h>

using namespace std;

int main()

{

cout<<"14 Computer Systems\n";

cout<<"MUET\n";

cout<<"Jamshoro";

getch();

return 0;

}

Same program can also be written as

Page 13: Computer programming lecture – 14

Escape Sequence ( \n )

Ali Asghar Manjotho, Lecturer CSE-MUET 13

Page 14: Computer programming lecture – 14

Escape Sequence ( \t )

Ali Asghar Manjotho, Lecturer CSE-MUET 14

#include<iostream>

#include<conio.h>

using namespace std;

int main()

{

cout<<"14 Computer Systems\tMUET\tJamshoro";

getch();

return 0;

}

Page 15: Computer programming lecture – 14

Escape Sequence ( \t )

Ali Asghar Manjotho, Lecturer CSE-MUET 15

Page 16: Computer programming lecture – 14

Escape Sequence ( \’ ) ( \” ) ( \\ )

Ali Asghar Manjotho, Lecturer CSE-MUET 16

#include<iostream>

#include<conio.h>

using namespace std;

int main()

{

cout<<"\"14 Computer Systems\" \n";

cout<<"\'MUET\'\t \\Jamshoro";

getch();

return 0;

}

Page 17: Computer programming lecture – 14

Escape Sequence ( \’ ) ( \” ) ( \\ )

Ali Asghar Manjotho, Lecturer CSE-MUET 17

Page 18: Computer programming lecture – 14

Escape Sequence

Ali Asghar Manjotho, Lecturer CSE-MUET 18

cout Statement Output

cout<<"14CS"; 14CS

cout<<"\"14CS\""; “14CS”

cout<<"\'14CS\'"; ‘14CS’

cout<<"14CS\tMUET"; 14CS MUET

cout<<"14CS\\MUET"; 14CS\MUET

cout<<"14CS\nMUET"; 14CSMUET

Page 19: Computer programming lecture – 14

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.

Ali Asghar Manjotho, Lecturer CSE-MUET 19

Page 20: Computer programming lecture – 14

Types of Comments

• There are two types of comments in C++

Ali Asghar Manjotho, Lecturer CSE-MUET 20

Page 21: Computer programming lecture – 14

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

Ali Asghar Manjotho, Lecturer CSE-MUET 21

Page 22: Computer programming lecture – 14

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 Sir Ali

It is a basic C++ program */

Ali Asghar Manjotho, Lecturer CSE-MUET 22

Page 23: Computer programming lecture – 14

Comments Example

Ali Asghar Manjotho, Lecturer CSE-MUET 23

/*This program displays string on screen

It is written by Sir Ali

It is a basic C++ program */

//These are header files

#include<iostream>

#include<conio.h>

using namespace std;

//Every C++ program starts with main function

int main()

{

cout<<"\"14 Computer Systems\" \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

All the text in GRAYcolor is comment