Csc240 -lecture_4

29
CSC240 INTRODUCTION TO PROGRAMMING – I Mr. Dilawar Lecturer, Department of Computer Science, Jahan University Kabul, Afghanistan.

Transcript of Csc240 -lecture_4

Page 1: Csc240  -lecture_4

CSC240INTRODUCTION TO PROGRAMMING – I

Mr. Dilawar

Lecturer,Department of Computer Science,

Jahan UniversityKabul, Afghanistan.

Page 2: Csc240  -lecture_4

Previous Lecture Outline• Introduction to C++ and other popular programming languages

• Historical development of C++

• Writing C++ program

• Structure of C++ program

Page 3: Csc240  -lecture_4

Lecture Outline• Variables• Datatypes• Declaration and Initialization of Variable

• Constants• Defining and Using Constants

Page 4: Csc240  -lecture_4

Elements of C++ LanguageChapter – 2

Page 5: Csc240  -lecture_4

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

Page 6: Csc240  -lecture_4

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.

Page 7: Csc240  -lecture_4

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.

Page 8: Csc240  -lecture_4

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.

Page 9: Csc240  -lecture_4

Variables• Followings are the some examples:

Page 10: Csc240  -lecture_4

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

Page 11: Csc240  -lecture_4

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.

Page 12: Csc240  -lecture_4

Datatypes

Integers Qualifiers

Page 13: Csc240  -lecture_4

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.

Page 14: Csc240  -lecture_4

Datatypes

Real Qualifiers

Page 15: Csc240  -lecture_4

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.

Page 16: Csc240  -lecture_4

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.

Page 17: Csc240  -lecture_4

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.

Page 18: Csc240  -lecture_4

Datatypes

C++ source code for obtaining the memory size of each datatype

Page 19: Csc240  -lecture_4

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;

Page 20: Csc240  -lecture_4

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

Page 21: Csc240  -lecture_4

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};

Page 22: Csc240  -lecture_4

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.

Page 23: Csc240  -lecture_4

Sample Program

C++ source code for I/O operation

Page 24: Csc240  -lecture_4

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.

Page 25: Csc240  -lecture_4

Constants• A constant might be • An integer constant• A real constant• A character constant• A string constant• A Boolean constant

Page 26: Csc240  -lecture_4

Constants• The constants can be define or declared in two shapes:• Using the const qualifier• Using #define directive

Page 27: Csc240  -lecture_4

Constants

Page 28: Csc240  -lecture_4

Summery• Variables• Datatypes• Declaration and Initialization of Variable

• Constants• Defining and Using Constants

Page 29: Csc240  -lecture_4

Thank YouFor your Patience