Basic Concepts of Programming
Syntax Rules
•	Program specify operations to be performed by the CPU
•	The specification has to be precise and unambiguous
•	PL specifies elaborate syntactic rules for construction of programs
•	Every PL comes up with a manual describing these rules
•	Any violation is termed syntax error and flagged by the compiler 
•	So one main function of compiler is to detect errors in your programs
•	When no error, it produces the executable or object code
•	Object code executed to get results or outputs

Semantics
•	The semantics of programs also specified by the language 
•	A precise specification of semantics also required 
•	Compiler writers take that as a basis for code generation
•	It is essential the machine code is equivalent to the source code
•	Programs should produce the same result independent of time and place (computers and compilers)
•	Unfortunately, this is not true always!
•	Standardization of Programming Languages
Program Structure
Every Fortran Program (true in most languages too)

–	starts with the keyword Program and a name
•	names are any identifiers starting with an alphabet
–	ends with the keyword end
–	has two parts
–	A Declarative part followed by an Executable part 
–	Each part consists of a sequence of statements
Fortran Statements
•	Every Statement optionally starts with a label 
•	A label is any number between 1 and 99999 used for identification
•	Each line up to 132 (or 128) characters long
•	For longer statements, use &
•	Free source form (type anywhere!)
•	Old Fortran programs input using punch cards and had a strict syntax
–	The first  5 columns reserved for labels
–	Continuation character at the 6th column 
–	The total number of columns is 80
–	etc.
•	Beware: Your compiler may follow some of these restrictions, still
Declarations
•	Each declarative statement defines one or more Data items, called variables
•	Variables are identifiers with the first symbol, an alphabet
•	Variables are abstractions of memory locations
•	Machine programs operate on memory locations 
•	High level programs operate on variables
•	Variables hold data values which can be accessed and changed during execution
•	Variable declarations optionally indicate initial values
Types
•	Variables have definite data types (like integer, real) specified in the declaration
•	The type of a variable determine the values that can be stored 
•	The types also define the operations (eg. +,* for integer and real while // for string variable)
•	Types further determine storage space
•	Each language defines a number of intrinsic or built-in types
•	Fortran 90 has 
		integer,real, character, complex, logical 
•	Fortran 90 allows users to define their own types too!
A General Declarative Statement
                     type_name [,attributes] :: variable_list

•	type_name - name of the intrinsic type or defined type
•	[..] - to denote optional strings
•	attributes - define additional properties of variables
–	More about attributes later
•	variable_list - list of variables defined
•	All the variables in the list declared to be of the specified type
•	Eg.             Integer :: abc, xyz, m01
                       Real  :: x_coord,y_coord

Variables
•	variable names in Fortran contain at most 31 characters
•	Characters can be upper or lower case letters, numerals (0-9) or underscore `_‘
•	name should start with a letter
•	upper and lower case letters not distinguished:
		e.g. Fort and fORT refer to the same variable
•	no blanks allowed
•	meaningful and indicative names be used
•	words like real,  program not to be used
Storage Allocation for Variables
•	Compiler allocates an address in memory for variables
•	The contiguous bits located at the address contain the binary representation of the value stored.
•	The number of bits, dependent on type of the variables and the compiler used
•	For each data type and compiler, a default value
•	Default values changed using declarations
•	Here are the typical default values:
–	Integer:  32 bits
–	Real:    32 bits
–	Character: 8 bits
–	Logical:   1 bit
Integer Representation
•	How many integers are there?
•	But memory is finite
•	Only finitely many numbers can be stored!
•	Fixed no. of bits are allocated for integers
•	One of the bits represents the sign, called sign bit 
•	The usual size of integer is 32 bits
Integer Representation
Sign bit
Overflow & Underflow
•	Largest  representable value for 32 bits is (why?)
•	 ( 231  - 1) = 214,74,83,647
•	Overflow occurs when an integer exceeds this 
•	Smallest value is: - 231
•	Underflow occurs when an integer is less than this value
KIND Parameter
•	The compiler assigns a default size for 		             
                   INTEGER :: ABCD
•	The size can be changed by KIND parameter:
             INTEGER(KIND=1) :: ABCD 
•	or equivalently
		INTEGER(1) :: ABCD
•	KIND parameter for integer may have values 1,2 or 3; 3 is the default
•	Varying sizes for different kind values (machine dependent)
•	1 is of low precision (usually 8 bits); 3 high precision
Real Numbers
•	What are real numbers?
       5/8, 22/7, 0.001, 3.141, , 2,1234567890.00023
•	How many of them are there? infinite (uncountable)
•	Only finitely many real numbers can be represented in a computer
•	Fixed no. of bits allocated for real numbers
•	For any n, there ia a real number requiring more than n bits
•	There are numbers which require infinite number of digits!
•	So, only finite approximations possible for many such numbers
Mantissa-Exponent Notation
•	Many applications involving real numbers require representation of large range of numbers
–	Astronomy, Physics calculations
•	Common scientific representation found useful: 
                0.2   1034,  0.09   10-26 
•	0.2, 0.09 are called Mantissa
•	34,  -26 are exponents 
•	Base is 10
•	This is a compact representation: small number of digits with large range of representable values
Floating Point Representation
•	Mantissa-Exponent notations is used in computers
•	Floating point representation, in computer parlance 
•	Fixed number of bits for mantissa and exponent
•	The base is usually  2; sometimes it is  16 
•	Two kinds of representation
–	Single Precision
–	Double Precision
•	No. of bits for double precision more

Real Representation
Sign bit
Approximations in Real Values
•	0.2 cannot be represented exactly
             0.0011 0011 0011 0011 0011 0011 . . .

•	possible representation in 25 bit mantissa
        0.0011 0011 0011 0011 0011 0011 0

•	this value is  0.2   (1 - 2-24)   How?

Normalized Representation
•	IEEE Standard
•	Mantissa
–	An implied 1 bit before the decimal point
–	binary point before first bit in the mantissa
•	Exponent
–	Excess 127 (or 1023) representation
–	All 0 and all 1 disallowed
–	They are used for 0 and infinity
Real Variables
•	largest real value (Single precision, IEEE Std)
                   ~  2128 ~ 1038

•	smallest positive real value 
                   2 -126 ~  10-38

•	overflow or underflow occur when a number falls outside the range
•	Round-off has to be done when exact representation require more bits 
Errors in Real Values
•	round-off errors occur in real values due to finite precision
•	absolute value of error can be large!
•	error in   0.2*2100   is   276  (in 25 bit representation)
•	however, relative error is always small
•	error/absolute value <= 2 -22  
•	(except for very small values)
•	only 7 decimal digits are significant
•	possible to increase precision/range if required
Attributes
•	One of the important attributes is PARAMETER
•	Consider the statement: 
                  REAL parameter :: pi = 3.141592
•	This declares pi as a real variable with specified initial value
•	The attribute PARAMETER indicates that the value of pi will remain unchanged in the program 
•	Any change is illegal
•	The parameter attribute can be used for any variable declaration: 
		eg. integer PARAMETER :: min_value = 25
•	Many more attributes are possible (look in the manual)