csc240 -lecture_4
TRANSCRIPT
CSC240INTRODUCTION TO PROGRAMMING – I
Mr. Dilawar
Lecturer,Department of Computer Science,
Jahan UniversityKabul, Afghanistan.
Previous Lecture Outline• Introduction to C++ and other popular programming languages
• Historical development of C++
• Writing C++ program
• Structure of C++ program
Lecture Outline• Variables• Datatypes• Declaration and Initialization of Variable
• Constants• Defining and Using Constants
Elements of C++ LanguageChapter – 2
Elements of C++ Language• Essentials or fundamentals on which C++ program is made and
developed are called elements of C++ language.
• The elements of C++ language are:• Variables• Constants• Identifiers• Operators• Expressions and Statements
Variables• They are symbolic names that can be given a variety of values.
• A quantity whose value may change during the execution of the program.
• When a variable is declared/identified so it demonstrates that a memory location is reserved by the name you mention for variable.
• When a variable is initialized/assigned a value, it describes that the memory has filled up with a value.
Variables• The name of the variable remains fixed throughout the program while
the value is changed by the need of programmer – not fixed.
• Each variable is identified by a specific name that is known as the identifier of the variable.
Variables• The following points must be keep in mind while naming a variable.• First character must be an alphabet or an underscore.• Blank spaces are not allowed in the variable name.• Special characters that performs operations are not allowed.• Reserved words cannot be used as variable name.• The maximum length of the variable name depends upon the compiler of C+
+.• A name specified for one variable cannot be used for another variable.• C++ is case-sensitive language.
Variables• Followings are the some examples:
Datatypes• Every variable can store a specific type of data.
• The type of data that a variable can store is called datatype.
• Datatypes are used to specify the type of the data that which type of value can a variable hold or store.
• Primitive datatypes are divided into the following types:• Integer and real type variable• Character and string type variable• Boolean type variable
Datatypes• Integer type variables are used to store integer data.
• Integer are whole number , i.e. a number without fraction or a decimal point.• 32, 45, -45 and 111 are integer numbers.
• The keyword int is used to specify an integer type data to a variable.• It can store the value from -232 to +232-1.• It reserves or occupies four bytes of memory.
• Integer qualifiers can be used for adjusting the size of memory and scope of values.• short, long, signed, and unsigned are integer qualifiers.
Datatypes
Integers Qualifiers
Datatypes• Real type variables are used to store real or floating type data.• i.e. a number having fraction or a decimal point.• 3.2, 4.5, -4.5 and 1.11 are real numbers.
• The keyword float is used to specify an real type data to a variable.• It can store the value from 3.4x10-38 to 32x10+38.having seven digits precision.• It reserves or occupies four bytes of memory.
• Float qualifiers can be used for adjusting the size of memory and scope of values.
Datatypes
Real Qualifiers
Datatypes• Character type variables are used to store single character.
• The keyword char is used to specify a character type data to a variable.• It can store the only a single character, enclosed in single quotation marks.• i.e. ‘$’, ‘X’, and ‘5’ etc.• It reserves or occupies one byte of memory.
Datatypes• String type variable are used to store sequence of characters such as a
word or sentence.
• The keyword string is used to specify a string type data to a variable.• The header file string.h must be used in the program header.
Datatypes• Boolean type variables are used to declare logical type variables.• They are only two values; 0 or 1.
• The keyword bool is used to specify a Boolean type data to a variable.
Datatypes
C++ source code for obtaining the memory size of each datatype
Declaration of Variable• C++ is a strongly-typed language,
and requires every variable to be declared with its type before its first use.
• Declaration informs the compiler about the memory size to reserve, name of the memory, and about the value which will be store.
• The syntax for declaring variable is given.
datatype variable_name;
int Sum;
float Avg;
char Choice;
bool Flag;
int sub1, sub2, sub3;
Initialization of a Variable• When a variables are declared, they have an undetermined value until
they are assigned a value for the first time.
• A variable can have specific value right from the moment of declaration – initialization of a variable.
• There are three ways in C++ to initialize a variable.• C-Like initialization• Constructor initialization• Uniform initialization
Initialization of a Variable• C-Like Initialization
• datatype variable_name = initial_value;• int Marks = 57;
• Constructor Initialization• datatype variable_name (initial_value);• int Marks (75);
• Uniform Initialization• datatype variable_name {initial value};• int Marks {75};
Initialization of a Variable• A variable can also be initialized after the declaration process with the
help of following syntax.• variable _name = initial_value;• Marks = 57;
• A variable can also be initialized during runtime with the help of common I/O function called cin.• Cin is a standard library streaming function that is used to take the value from
user at runtime.
Sample Program
C++ source code for I/O operation
Constants• Name given to the memory location and can store value with itself
and can be changeable is known as a variable.
• If the memory location is reserved for storing a value and later it cannot be changed, such locations are called constants.
• Constant is a quantity that cannot change its value during the time of execution.
Constants• A constant might be • An integer constant• A real constant• A character constant• A string constant• A Boolean constant
Constants• The constants can be define or declared in two shapes:• Using the const qualifier• Using #define directive
Constants
Summery• Variables• Datatypes• Declaration and Initialization of Variable
• Constants• Defining and Using Constants
Thank YouFor your Patience