Introduction To Programming CS1133: Engr. Rabia Afzal Minhas
Introduction To Programming CS1133: Engr. Rabia Afzal Minhas
CS1133
• Dev C++
Comments
• Two types of comments
1. Single line comment //….
2. Multi-line (paragraph) comment /* */
• When it sees /*, it scans for the text */ and ignores any
text between /* and */
Special Symbols
+ - * /
. ; ? ,
<= != == >=
Special characters
• Text string special characters (Escape
Sequences)
• \n = newline
• \r = carriage return
• \t = tab
• \" = double quote
• \? = question
• \\ = backslash
• \' = single quote
• Examples:
cout << "Hello\t" << "I\’m Ali\n";
cout << "123\nabc “;
C++ Reserve Words
• Reserved words are also called keywords. The letters that
make up a reserved word are always lowercase
x = a + b;
Speed_Limit = 90;
Names
Valid Names
• Start with a letter
• Contains letters
• Contains digits
• Contains underscores
• Examples…
cout and endl
• cout (console output) and the operator
• << referred to as the stream insertion operator
• << “Inserts values of data-items or string to screen.”
• >> referred as stream extraction operator, extracts
value from stream and puts into “Variables”
• Whitespace characters:
• space,
• tab,
• newline {enter key}
rvalue and lvalue
• Are the two occurrences of “a” in this expression the
same?
a = a + 1;
One on the left of the assignment refers to the location
of the variable (whose name is a, or address);
One on the right of the assignment refers to the value of
the variable (whose name is a);
int i, j;
i = 7;
7 = i;
j * 4 = 7;
Data Types
Three basic PRE-DEFINED data types:
1. To store whole numbers
• int, long int, short int, unsigned int
3. Characters
• char
Data Types
• Integral, which is a data type that deals with integers, or
numbers without a decimal part
• Floating-point, which is a data type that deals with
decimal numbers
• Enumeration, which is a user-defined data type
Data Types
Types and literals
• Built-in types Literals
• Boolean type • Boolean: true, false
• bool
• Character types
• Character literals
• char
• 'a', 'x', '4', '\n', '$', ' '
• Integer types
• int
• and short and • Integer literals
long • 0, 1, 123, -6,
• Floating-point types
• double • Floating point literals
• and float • 1.2, 13.345, 0.3, -0.54,
Examples:
int marks;
double Pi;
int suM;
char grade;
Marks
FE07
Declaration and initialization
int a = 7; 7
int b = 9; 9
char c = 'a'; a
Hello, world
string s1 = "Hello, world";
1.2
string s2 = "1.2";
Using Variables: Initialization
• Variables may be given initial values, or initialized, when
declared. Examples:
length
int length = 7 ;
7
diameter
float diameter = 5.9 ; 5.9
initial
char initial = ‘A’ ;
‘A’
string type
• Special data type supports working with “strings”
#include <string>
string <variable_name> = “string literal”;
Examples:
• char grade;
• unsigned char WeekNumber= 200;
• char cGradeA = 65;
• char cGradeAA = ‘A';
int type
• 16 bits (2 bytes) on Windows 16-bits
• int -32,768 to 32,767
• unsigned int 0 to 65,535
• Also on Turbo C++, 2 bytes for int
• short int
• reserves 16 bits (2 bytes) of memory
• signed short int -32,768 to 32,767
• unsigned short int 0 to 65,535
int (long and short)
• Examples:
long int light_speed=186000;
unsigned long int seconds= 604800;
short int Height = 30432;
unsigned short int Width = 50000;
Home Work-1
• Use Visual C++ on Windows and get information for
following data types:
• int
• short
• long int
• short int
• char
• Use ( cout << sizeof( intVar ); ) operator to get
this information, Example:…
Real Values
• float
• Reserves 32 bits (4 bytes) of memory
+38
• + 1.180000x10 , 7-digit precision
• Example: float radius= 33.4221;
• double
• Reserves 64 bits (8 bytes) of memory
+308
• + 1.79000000000000x10 , 15-digit precision
• Example: double Distance = 257.5434342;
• long double
• Reserves 80 bits (10 bytes) of memory , 18-digit precision
• Example: long double EarthMass = 25343427.53434233;
Home Work-2
• Use Visual C++ on Windows and get information for
following data types:
• float
• double
• long double
• Literal values:
• true
• false