[go: up one dir, main page]

0% found this document useful (0 votes)
18 views20 pages

Lecture No.04

This document is an introduction to programming concepts for a course taught by Shabir Ahmad at Dunya Institute of Higher Education in Kabul, Afghanistan. It discusses variables, data types, and basic C++ concepts like the assignment operator, integer data type, declaration of variables, and ranges of standard data types. The document contains examples of declaring, assigning, and using integer variables in simple C++ programs.

Uploaded by

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

Lecture No.04

This document is an introduction to programming concepts for a course taught by Shabir Ahmad at Dunya Institute of Higher Education in Kabul, Afghanistan. It discusses variables, data types, and basic C++ concepts like the assignment operator, integer data type, declaration of variables, and ranges of standard data types. The document contains examples of declaring, assigning, and using integer variables in simple C++ programs.

Uploaded by

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

In the Name of

Allah
The Most Merciful and Compassionate the most
gracious and beneficent, Whose help and
guidance we always solicit at every step, at every
moment.
Programming Concepts

Shabir Ahmad Usmany

Dunya Institute of Higher Education Kabul Afghanistan


Introduction To Programming

 Variables
 Assignment Operator
 Data Types

3 By: Shabir Ahmad


Introduction To Programming

 During programming we need to store data. This data is stored


in variables.
 Variables are locations in memory for storing data. The memory
is divided into blocks. It can be viewed as pigeon-holes. You can
also think of it as PO Boxes.
 In post offices there are different boxes and each has an address.
Similarly in memory, there is a numerical address for each
location of memory (block).
 It is difficult for us to handle these numerical addresses in our
programs. So we give a name to these locations. These names
are variables.
 We call them variables because they can contain different values
at different times.

4 By: Shabir Ahmad


Introduction To Programming

 The variable names in C may be started with a character or an underscore


( _ ).
 But avoid starting a name with underscore ( _ ). C has many libraries
which contain variables and function names normally starting with
underscore ( _ ). So your variable name starting with underscore ( _ ) may
conflict with these variables or function.
 Blank spaces are not allowed in a variable name
 Special characters, such as arithmetic operators, #, ^, cannot be used in a
variable name.
 Reserved words cannot be used as variable names.
 Variable name declared for one data type cannot be used to declare
another data-type.
 C++ is case sensitive language. Thus variable name with same spelling but
different cases are treated as different variable names.
 E.g. pay is different from Pay etc.

5 By: Shabir Ahmad


Introduction To Programming

 In a program every variable has


◦ Name
◦ Type
◦ Size
◦ Value
 The variables having a name, type and size (type and size will be
discussed later) are just empty boxes. They are useless until we
put some value in them.
 To put some value in these boxes is known as assigning values to
variables.
 In C language, we use assignment operator for this purpose.

6 By: Shabir Ahmad


Introduction To Programming

 In C language equal-to-sign (=) is used as assignment operator.


 Do not confuse the algebraic equal-to with the assignment
operator.
 In Algebra X = 2 means the value of X is 2
 Whereas in C language X = 2 (where X is a variable name) means
that X is holding or taking the value 2 and put it in the memory
location labeled as X
 Afterwards you can assign some other value to X,
 For Example:
 You can write X = 10, that means now the memory location X
contains the value 10 and the previous value 2 is no more there.

7 By: Shabir Ahmad


Introduction To Programming

 Assignment operator is a binary operator (a binary operator has


two operands). It must have variable on left hand side and
expression (that evaluates to a single value) on right hand side. This
operator takes the value on right hand side and stores it to the
location labeled as the variable on left hand side,
 e.g. X = 5, X = 10 + 5, and X = X +1.
 In C language the statement X = X + 1 means that add 1 to the value
of X and then store the result in X variable. If the value of X is 10
then after the execution of this statement the value of X becomes
11.
 This is a common practice for incrementing the value of the
variable by ‘one in C language. Similarly you can use the statement
X = X - 1 for decrementing the value of the variable by one.

8 By: Shabir Ahmad


Introduction To Programming

 The statement X = X + 1 in algebra is not valid. So do not confuse


assignment operator (=) with equal sign (=) in algebra.
 Remember that assignment operator must have a variable name
on left hand side unlike algebra in which you can use expression
on both sides of equal sign (=).
 For example, in algebra, X +5 = Y + 7 is correct but incorrect in C
language. The compiler will not understand it and will give error

9 By: Shabir Ahmad


Introduction To Programming

 A variable must have a data type associated with it


 For example:
 It can have data types like
◦ integer,
◦ decimal numbers,
◦ characters etc.
 The variable of type Integer stores integer values and a
character type variable stores character value. The primary
difference between various data types is their size in memory.
Different data types have different size in memory depending on
the machine and compilers.

10 By: Shabir Ahmad


Introduction To Programming

 The data type int is used to store whole numbers (integers).


 The integer type has a space of 4 bytes (32 bits for windows
operating system) in memory. And it is mentioned as ‘int’ which
is a reserved word of C, so we cannot use it as a variable name.
 In programming before using any variable name we have to
declare that variable with its data type. If we are using an
integer variable named as ‘i’, we have to declare it as
int i ;
 The above line is known as declaration statement.

11 By: Shabir Ahmad


Introduction To Programming

 When we declare a variable in this way, it reserves some space


in memory depending on the size of data type and labels it with
the variable name.
 The declaration statement int i ; reserves 4 bytes of memory
and labels it as ‘i’. This happens at the execution time

12 By: Shabir Ahmad


Introduction To Programming

 Let’s consider a simple example to explain int data type. In this example we take two integers,
add them and display the answer on the screen. The code of the program is written below.
#include <iostream.h>
main( )
{
int x;
int y;
int z;
x = 5;
y = 10;
z = x + y;
cout << “x = “;
cout << x;
cout << “ y=“;
cout << y;
cout << “ z = x + y = “;
cout << z;
}

13 By: Shabir Ahmad


Introduction To Programming

Declaration Name Size in Size in Range


bits bytes
Char or Signed Char 8 1 -128 to 127
Unsigned Char 8 1 0 to 255
Int or Signed int 16 2 -32768 to 32767
Unsigned int 16 2 0 to 65535
Short int or Signed short int 8 1 -128 to 127
Unsigned short int 8 2 0 to 255
Long int or signed long int 32 4 -2147483648 to 2147483647
Unsigned long int 32 4 0 to 4294967295
Float 32 4 3.4 e-38 to 3.4 e+38
Double 64 8 1.7e-308 to 1.7e+308
Long Double 80 10 3.4 e-4932 to 3.4 e+4932

14 By: Shabir Ahmad


Introduction To Programming

Declaration Name Size in bits


Character Char
Unsigned Character unsigned char
Signed Character signed char
Signed Integer signed int (or) int
Signed Short Integer signed short int (or) short int (or) short
Signed Long Integer signed long int (or) long int (or) long
UnSigned Integer unsigned int (or) unsigned
UnSigned Short Integer unsigned short int (or) unsigned short
UnSigned Long Integer unsigned long int (or) unsigned long
Floating Point Float
Double Precision Floating Point Double

15 By: Shabir Ahmad


Introduction To Programming

/*This program uses short data type to store values */

#include <iostream.h>
main()
{
short x;
short y;
short z;
x = 5;
y = 10;
z = x + y;
cout << “x = “;
cout << x;
cout << “ y=“;
cout << y;
cout << “ z = x + y = “;
cout << z;
}

16 By: Shabir Ahmad


Introduction To Programming

/*This program uses float data type to store values */


#include <iostream.h>
main()
{
float x;
float y;
float z;
x = 12.35;
y = 25.57;
z = x + y;
cout << “ x = “;
cout << x;
cout << “ y = “;
cout << y;
cout << “ z = x + y = “;
cout << z;
}

17 By: Shabir Ahmad


Introduction To Programming

/* This program uses char data type to store values */

#include <iostream.h>
main()
{
char x;
x = ’a’;
cout << “The character value in x = “;
cout << x;
}

18 By: Shabir Ahmad


Introduction To Programming

 Use spaces in the coding to make it easy to read and understand


 Reserved words cannot be used as variable names
 There is always a main( ) in a C program that is the starting point of
execution
 Write one statement per line
 Type parentheses ’( )’ and braces ‘{ }’ in pairs
 Use parentheses for clarification in arithmetic expressions
 Don’t forget semicolon at the end of each statement
 C Language is case sensitive so variable names x and X are two different
variables

19 By: Shabir Ahmad


End of Lecture # 04

Thanks
Questions?

You might also like