STARTING COMPUTER PROGRAMMING

Variables, Values,  and Types:

MEMORY:


Memory is a collection of locations called variables
In a programming language, we get at the location by using a variable
Each variable has
A name (an identifier)
A type (the kind of information it can contain)

VARIABLE:

§Refer to memory location where a particular value is stored
§Type of data decides the amount of memory  allocated to variables
§Names assigned to variables to store a particular data, help us in retrieving the data as and when required

MEMOMY AND VARIABLE:

Memory is a collection of locations called variables
In a programming language, we get at the location by using a variable
Each variable has
A name (an identifier)
A type (the kind of information it can contain)
Basic types include
int (integers – whole numbers: 17, -42)
double (floating-point numbers with optional fraction and/or exponent: 3.14159, 6.02e23)
char (character data: ‘a’, ‘?’, ‘N’, ‘ ’, ‘9’)
Note: ‘9’ is a character; 9 is an integer – they are different and have different types


DECLARING VARIABLE:

int  months;
Integer variables represent whole numbers:
1, 17, -32, 0  Not 1.5, 2.0, ‘A’
double pi;
Floating point variables represent real numbers:
3.14, -27.5, 6.02e23, 5.0  Not 3
char first_initial, middle_initial, marital_status;
Character variables represent individual keyboard
characters:
'a', 'b', 'M', '0' , '9' , '#' , ' '     Not "Bill"

DATA TYPE:

§Decides the amount of memory to be allocated to a variable to store a particular type of data
§Declaring a variable
úAllocates memory
úPortion of memory is referred to by the variable name
§General form of declaring a variable
údata type (variable name)
§Common data types
  1. úNumeric
  2.                                               Alphanumeric

§Type int


úStores numeric data
úConsists of a sequence of one or more digits
úIncludes only whole numbers

§Type char


úStores a single character
úThe single character is enclosed within two single quotation marks
úDigits can also be stored as characters but cannot be used for calculations

TYPE float:


úStores values containing decimal places
úStores either whole or fractional numbers

§Type double


úStores twice the number of digits than a float type
úOccupies double memory space than a float
§Precise number of digits stored by float and  double types depends upon the particular computer system.




Guidelines for specifying Variable Names:


§Must begin with an alphabet

§First character to be followed by a sequence of letters or digits or special character ‘underscore’

§Avoid using letter O in situations where it can be confused with the number 0 and the lowercase letter l can be mistaken with the number 1

§Uppercase and lowercase letters are treated different
§Name of the variable should be descriptive of the value it holds


RESERVED WORDS:


Certain identifiers have a "reserved" (permanent, special) meaning in C#
We’ve seen int already
Will see a couple of dozen more eventually
These words always have that special meaning,  and cannot be used for other purposes.
Cannot be used names of variables
Must be spelled exactly right
Sometimes also called “keywords”

PROGRAM EXECUTION:

A memory location is reserved by declaring a C# variable
You should give the variable a name that helps someone else reading the program understand what it is used for in that program
Once all variables have been assigned memory locations, program execution begins
The CPU executes instructions one at a time, in order of their appearance in the program


AN EXAMPLE:

/* calculate and print area of 10x3 rectangle */
void Main(string [] args)
{
  int  rectangleLength
  int  rectangleWidth;
  int  rectangleArea;
  rectangleLength = 10;
  rectangleWidth = 3;
  rectangleArea = rectangleLength * rectangleWidth ;
  Console.WriteLine(“with length {0} and width {1} the area of rectangle is {2}”, rectangleLength
rectangleWidth ,rectangleArea);


CONVERT CLASS:

§Converts a base data type to another base data type.
§Common methods
úConvert.ToInt16
úConvert.ToInt32
úConvert.ToDouble
úConvert.ToBoolean
úConvert.ToChar
úConvert.ToDateTime

/////////////////////////////////////END OF SESSION/////////////////////////////////////////////////


GO TO NEXT CHAPTER "ARITHMETIC EXPRESSIONS"

No comments:

Post a Comment