Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 31

ELEMENTARY

PROGRAMMING
Lecture # 3
IDENTIFIER
 An identifier is a name for variables,
constants, functions, types, labels, classes
etc.
 It consists of a letter or underscore followed
by any sequence of letters, digits or
underscores
 Names are case-sensitive. The following are
unique identifiers:
Hello, hello, whoami, whoAMI, WhoAmI
 Names cannot have special characters in
them
e.g., X=Y, J-20, #007, etc. are invalid identifiers.

Designed by Prof. Anosha Khan 08/25/2021 2


IDENTIFIER
 C++ reserved words cannot be used as
identifiers.
 Choose identifiers that are meaningful and easy
to remember.
 Can be of anything length, but on the first 31
are significant (too long is as bad as too short).
 Are case sensitive:
 abc is different from ABC
 Which of the following are valid identifiers?
40hrsperweek to day C3PO i
three.onefour name$ 3.14 N/4
SumOfGrades X-Ray Double x2
two_sevenone double R2D2 INT

Designed by Prof. Anosha Khan 08/25/2021 3


KEYWORDS
 Keywords are reserved words of C++ language
and has predefined meaning and purpose
 The list of reserved words:
asm, auto, bool, break, case, catch, char,
class, const, continue, default, delete, do,
double, else, enum, extern, float, for,
friend, goto, if, include, inline, int, long,
namespace, new, operator, private, protected,
public, register, return, short, signed,
sizeof, static, struct, switch, template,
this, throw, try, typedef, union, unsigned,
using, virtual, void, volatile, while

Designed by Prof. Anosha Khan 08/25/2021 4


VARIABLE
 VARIABLE : A location in memory, referenced by
name, where a data value that can be changed is
stored.
 A variable is used by a program to store a calculated
or entered value. The program might need the value
later, and must then be stored in computers working
memory
 The value of variable can be change during
execution of program
 Variable must have a unique name
 A variable must be declared before it can use
 Same rules as identifiers for naming conventions
 Variable name is case sensitive
Designed by Prof. Anosha Khan 08/25/2021 5
VARIABLE TYPES
 Local variable
Local variables are declared within the
body of a function, and can only be used
within that function.
 Global variable
A global variable declaration looks
normal, but is located outside any of the
program's functions. So it is accessible to
all functions.

Designed by Prof. Anosha Khan 08/25/2021 6


VARIABLE DECLARATION
 A declaration introduces a variable’s name and
specify its type
 Declaration assign make an empty block in the
computer memory having the name and type
that is declared
 Syntax:
 <type> <identifier>
 int marks;
 A variable must be declared before it can be
used.
int main(){
x = 5; // illegal: x was not declared
} Designed by Prof. Anosha Khan 08/25/2021 7
VARIABLE DECLARATION
 Several variables of the same type can be
declared in the same declaration (though it
is better to put them on separate lines):
double total_USD, area;
 A variable must have only one type. For
example, a variable of the type int can only
hold integer values.

Designed by Prof. Anosha Khan 08/25/2021 8


VARIABLE INITIALIZATION
 An assignment operation assigns, or copies, a
value into a variable
 When a value is assigned to a variable as part of
the variable’s declaration it is called
initialization
 Assignment syntax:
<identifier> = <expression>;
 For example
 marks = 12 ;
 This statement copies the value 12 into the variable
marks
 This statement is legal if the variable marks is
already declared
Designed by Prof. Anosha Khan 08/25/2021 9
ASSIGNMENT OPERATOR
 The = symbol is called the assignment
operator
 Operator performs operations on data
 The data that operator works with are called
operands
 unitSold = 12;
 This assignment statement has two operands
 unitSold and 12

Designed by Prof. Anosha Khan 08/25/2021 10


ASSIGNMENT STATEMENTS
int NewStudents = 6;
NewStudents 6
int OldStudents = 21;
int TotalStudents; OldStudents 21
TotalStudents -
TotalStudents = NewStudents + OldStudents ;

NewStudents 6
OldStudents 21
TotalStudents 27

Designed by Prof. Anosha Khan 08/25/2021 11


ASSIGNMENT STATEMENTS
Value1 10
int Value1 = 10;
int Value2 = 20; Value2 20
Hold 10
int Hold = Value1;

Value1 20
Value1 = Value2;
Value2 20
Hold 10

Value1 20
Value2 = Hold;
Value2 10
Hold 10

Designed by Prof. Anosha Khan 08/25/2021 12


NAMED CONSTANTS
 Constant is a quantity that cannot be change during program
execution
 Named constants are declared and referenced by identifiers
const double pi = 3.14159;
 They can make a program more readable and maintainable
Constant declaration syntax:
const <type> <identifier> = <constant
expression>;
Examples:
const double US2HK = 7.8;
const double HK2Yuan = 1.
 The reserved word const is used to specify that an identifier is
a constant.
 Constants must be initialized in their declaration, and may not
be assigned a value later.
Designed by Prof. Anosha Khan 08/25/2021 13
NAMED CONSTANTS
 Generally it is better design to use named
constants rather than literal constants:
 the name carries meaning that makes the code
easier to understand.
 if the value is used in more than one place,
only the declaration would need to be changed
if the value of the constant needed to be
updated (e.g., a tax rate).
 It is common practice to choose identifiers
for named constants that are strictly upper-
case. This makes it easy to distinguish the
named constants from variables.
Designed by Prof. Anosha Khan 08/25/2021 14
LITERAL CONSTANTS
 Literal constants are explicit numbers or
characters, such as:
 16 -45.5f "Freddy" 'M' 3.14159
Note that single quotes are used to indicate a character value,
and double quotes are used to indicate a string value.
 Integer constants
 String constants
 Character constants
 Floating point constants

Designed by Prof. Anosha Khan 08/25/2021 15


DEFINING CONSTANTS:

 There are two simple ways in C++ to define


constants:
 Using #define preprocessor.
 Using const keyword.

Designed by Prof. Anosha Khan 08/25/2021 16


 You can use const prefix to declare constants with a specific
type as follows:
 const type variable = value;
 Following example explains it in detail:
#include <iostream>
using namespace std;
int main()
{
const int LENGTH = 10;
const int WIDTH = 5;
int area;
area = LENGTH * WIDTH;
cout << area;
return 0;
}
 When the above code is compiled and executed, it produces
following result:
50 Designed by Prof. Anosha Khan 08/25/2021 17
 Following is the form to use #define preprocessor to define a
constant:
 #define identifier value
 Following example explains it in detail:
#include <iostream>
using namespace std;
#define LENGTH 10
#define WIDTH 5
int main()
{
int area;
area = LENGTH * WIDTH;
cout << area;
return 0;
}
 When the above code is compiled and executed, it produces
following result:
50
Designed by Prof. Anosha Khan 08/25/2021 18
PRIMITIVE DATA TYPES
 There are many different types of data.
Variables are classified according to their
data types, which determine the kind of
information that may be stored in them.
 Basically there are two main data types
 Numbers
 Integers (whole numbers like 12, -35, 157 etc)
 Floating point (decimal point like 23.7, 0.23 etc)
 Characters

Designed by Prof. Anosha Khan 08/25/2021 19


SELECTION OF A DATA TYPE
 Primary considerations for selecting a
numeric data type are
 The largest and smallest number that may be
stored in the variables
 How much memory the variable is uses
 Whether the variable stored signed or unsigned
numbers
 The number of decimal places of precision the
variable has
 The size of variable is the number of bytes of
memory it uses

Designed by Prof. Anosha Khan 08/25/2021 20


INTEGER DATA TYPE

Data types Size Range

short 2 bytes -32,768 to +32,767

Unsigned short 2 bytes 0 to +65,535


-2147483648 to
int 4 bytes +2147483647

Unsigned int 4 bytes 0 to 4294967295


-2147483648 to
long 4 bytes +2147483647

Unsigned long 4 bytes 0 to 4294967295

Designed by Prof. Anosha Khan 08/25/2021 21


CHAR DATA TYPE
 It stores characters
 Its size is 1 byte
 It is an integer data type and used to store
characters because characters are internally
represented by numbers
 ASCII codes are use to encode characters
 When a character is stored in memory its
actually the numeric code that is stored
 65 is code for capital A
 66 is B and so on
 Home task : wchar_t(wide character type)
Designed by Prof. Anosha Khan 08/25/2021 22
SAMPLE PROGRAM
#include <iostream>
Using namespace std;
int main()
{
char letter;
letter= 65;
cout << letter << endl;
letter= 66;
cout << letter << endl;
return 0;
}
Output:
A
B
Designed by Prof. Anosha Khan 08/25/2021 23
SAMPLE PROGRAM
#include <iostream>
Using namespace std;
int main()
{
char letter;
letter= ‘A’;
cout << letter << endl;
letter= ‘B’;
cout << letter << endl;
return 0;
}
Output:
A
B
Designed by Prof. Anosha Khan 08/25/2021 24
SAMPLE PROGRAM
#include <iostream>
using namespace std;
int main()
{
char University[] = "University of the District of Columbia";
char Faculty[]("Computer sciences");
cout << "Welcome to the Student Orientation Program.\n";
cout << "For your studies, we have selected:\n";
cout << "Institution: " << University << "\n";
cout << "Faculty: " << Faculty << "\n";
return 0;
}

Designed by Prof. Anosha Khan 08/25/2021 25


FLOAT & DOUBLE
 float represents a single-precision floating
point number. It is stored in 32-bits (as
defined in IEEE 754-2008) and it can represent
numbers between 1.18*(10^−38) and
3.4*(10^38) with around 7 digits of mantissa.
 double represents a double-precision floating
point number It is stored in 64-bits and it can
represent numbers between
2.2250738585072009*(10^-308) and
1.7976931348623157*(10^308) with
approximately 16 digits of precision.

Designed by Prof. Anosha Khan 08/25/2021 26


DATA TYPES
Name Description Size* Range*
char Character or small 1 byte signed: -128 to 127
integer unsigned: 0 to 255
short int Short integer 2 bytes signed: -32768 to 32767
(short) unsigned: 0 to 65535
int Integer 4 bytes signed: -2147483648 to
2147483647
unsigned: 0 to 4294967295
long int Long integer 4 bytes signed: -2147483648 to
(long) 2147483647
unsigned: 0 to 4294967295
float Floating point 4 bytes 3.4e +/- 38 (7 digits)
number
double Double precision 8 bytes 1.7e +/- 308 (15 digits)
floating point number
long Long double 8 bytes 1.7e +/- 308 (15 digits)
double precision floating
point number

Designed by Prof. Anosha Khan 08/25/2021 27


SAMPLE PROGRAM
This program computes the average of 3 exam scores.
The exam scores are read as integers; the average is
printed as a float.
#include <iostream>
using namespace std;
const int NUM_SCORES = 3;
int main()
{
int score1=56, score2=45, score3=55;
float ave;
ave = float(score1 + score2 + score3) / float(NUM_SCORES);
cout << "The average of " << score1 << ", " << score2 << ",
and "<< score3 << " is " << ave << "." << endl;
return 0;
}
Designed by Prof. Anosha Khan 08/25/2021 28
STRING DATA TYPE
 The string Type
a programmer-defined type
 requires #include <string>
 A string is a sequence of characters
"Hi Mom"
"We're Number 1!"
"75607"

Designed by Prof. Anosha Khan 08/25/2021 29


SAMPLE PROGRAM
#include <iostream>
#include <string>
using namespace std;
int main()
{
string FirstName, LastName;
cout << "Enter first name: ";
cin >> FirstName;
cout << "Enter last name: ";
cin >> LastName;
cout << "\n\nFull Name: " << FirstName << " " << LastName <<
"\n\n";
return 0;
}
Designed by Prof. Anosha Khan 08/25/2021 30
SIZEOF FUNCTION
 sizeof function is used to find the size of the
data type, variable or constants
 For example
 cout<< sizeof(int) << endl
 This statement displays the size of int data type

Designed by Prof. Anosha Khan 08/25/2021 31

You might also like