[go: up one dir, main page]

0% found this document useful (0 votes)
28 views29 pages

Chapter 3

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views29 pages

Chapter 3

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

Faculty of Computer Science

Introduction to Programming

Lecturer: Lutfullah “Haqnesar”


Introduction to Programming

Chapter 3

Syntax of C++
Introduction to Programming

Learning outcomes:

C++ basic Input/Output


Cout, Cin, Endl
C++ Comments
Variables
Data Types
Introduction to Programming

C++ Basic Input/Output


• C++ I/O operation is using the stream concept. Stream is the sequence of bytes or
flow of data.
• If bytes flow from main memory to device like printer, display screen, or a network
connection, etc, this is called as output operation.
• If bytes flow from device like printer, display screen, or a network connection, etc
to main memory, this is called as input operation.
Introduction to Programming

I/O Library Header Files

<iostream> It is used to define the cout and cin objects, which


correspond to standard output stream and standard
input stream respectively.
Introduction to Programming

Standard output stream (cout):

• The cout is a predefined object of ostream class.


• It is connected with the standard output device, which is usually a display screen.
• The cout is used in conjunction with stream insertion operator (<<) to display the
output on a console.
Introduction to Programming
• Let's see the simple example of standard output stream (cout):

#include <iostream>
using namespace std;
int main( ) {
cout << “Welcome to C++ Programming";
}
Introduction to Programming

Standard input stream (cin):

• The cin is a predefined object of istream class.


• It is connected with the standard input device, which is usually a keyboard.
• The cin is used in conjunction with stream extraction operator (>>) to read the
input from a console.
Introduction to Programming
Let's see the simple example of standard input stream (cin):

#include <iostream>
using namespace std;
int main( ) {
int age;
cout << "Enter your age: ";
cin >> age;
cout << "Your age is: " << age;
}
Introduction to Programming
Standard end line (endl):

• The endl is a predefined object of ostream class. It is used to insert a new line characters.
• Let's see the simple example of standard end line (endl):
#include <iostream>
using namespace std;
int main( ) {
cout << "C++ Programming";
cout << " is easy"<<endl;
cout << “and interesting"<<endl;
}
Introduction to Programming

C++ Comments:
• The C++ comments are statements that are not executed by the compiler.
• The comments in C++ programming can be used to provide explanation of
the code, variable, method or class.
• By the help of comments, you can hide the program code also.
There are two types of comments in C++.
1. Single Line comment
2. Multi Line comment
Introduction to Programming

1. C++ Single Line Comment


• The single line comment starts with // (double slash) and end with end of line.
• Example of single line comment:
#include <iostream>
using namespace std;
int main()
{
int x = 11; // x is a variable
cout<<x;
}
Introduction to Programming

2. C++ Multi Line Comment:

• The C++ multi line comment is used to comment multiple lines of code.
• It is surrounded by slash and asterisk (/∗ ..... ∗/).
Introduction to Programming
• Example of multi line comment in C++:

#include <ostream>
using namespace std;
int main()
{
/* declare and
print variable in C++. */
int x = 35;
cout<<x; }
Introduction to Programming

C++ Variable

• A variable is a name of memory location and it is used to store data. Its value
can be changed and it can be reused many times.
• It is a way to represent memory location through symbol so that it can be easily
identified.
Syntax to declare a variable:
Data_type variable_name;
Introduction to Programming
The example of declaring variable is given below:
int x;
float y;
char z;
Here, x, y, z are variables and int, float, char are data types.
We can also provide values while declaring the variables which is called as
variable initialization:
int x = 5, b = 10; //declaring 2 variable of integer type
float f = 30.8;
char c = 'A';
Introduction to Programming

Rules for defining variables

A variable can have alphabets, digits and underscore.


A variable name can start with alphabet and underscore only.
A variable name can't start with digit.
No white space is allowed within variable name.
A variable name must not be any reserved word or keyword e.g. char, float etc.
Introduction to Programming
Valid and invalid variable names:

• int a; (…)
• int _ab; (…)
• int 4; (…)
• int x y; (…)
• int a30; (…)
• int double; (…)
Introduction to Programming

C++ Data Types


A data type specifies the type of data that a variable can store such as integer,
floating, character etc.
There are various types of data types in C++ language.

No Types Data Types


1 Basic or Built-in Data Type int, char, float, double, etc
2 Derived Data Type array, pointer, function
3 User Defined Data Type Structure, union, class
Introduction to Programming

Basic or Build-in Data Types:


This data type specifies the size and type of information the variable
will store.
Data Types Memory Size
char 1 byte
boolean 1 byte
short 2 byte
int 4 byte
long 4 byte
float 4 byte
double 8 byte
Introduction to Programming

Numeric Types:

• Use int when you need to store a whole number without decimals, like
35 or 1000.
• Use float or double when you need a floating point number (with
decimals), like 9.99 or 3.14515.
Introduction to Programming
Numeric Types example:

#include <iostream>
using namespace std;
int main () {
int myNum1 = 1000;
float myNum2 = 5.75;
cout << myNum1;
cout << myNum2;
}
Introduction to Programming

Boolean Types:

A boolean data type is declared with the bool keyword and can only
take the values true or false.
When the value is returned, true = 1 and false = 0.
Introduction to Programming

Boolean Types example:

#include <iostream>
using namespace std;
int main() {
bool isCodingFun = true;
bool isDifficult = false;
cout << isCodingFun << "\n";
cout << isDifficult;
}
Introduction to Programming

Character Types:
The char data type is used to store a single character. The character must be
surrounded by single quotes, like 'A' or 'c':

#include <iostream>
using namespace std;
int main () {
char myGrade = 'B';
cout << myGrade;
}
Introduction to Programming

String Types:

The string type is used to store a sequence of characters (text). String


values must be surrounded by double quotes:
To use strings, you must include an additional header file in the source
code, the <string> library.
Introduction to Programming

Example:

#include <iostream>
#include <string>
using namespace std;
int main() {
string greeting = "Hello";
cout << greeting;
}
Introduction to Programming

C++ Constants
In C++, we can create variables whose value cannot be changed. For that,
we use the const keyword.
Example:
const int SPEED = 299792458;
• Now trying to change the value of variable SPEED result in an error in the
program.
• SPEED = 2500 // Error! SPEED is a constant.

You might also like