Basic Programming Concepts

Variables and Constants

Program Structure

•     every program contains two parts

•     declarative part

–   contains definition of the data items used

•     executable part

–   contains definition of operations to be performed on the data

•     declarative part occurs before the executable

Declarations

•     contains several declarative statements

•     declarative statement defines data items

•     data items are named and called variables

•     variable name represents locations which will hold the value of the data item

•     variable name also used to denote the value

•     every variable is also given a type

•     the type of a variable defines its possible values and operations

Intrinsic Types

•     every programming language has some built in types called intrinsic types

•     in Fortran, some of these are integer, real, logical, and character

•     integer and real are numeric types

•     possible values are ‘integers’ and ‘reals’

•     arithmetic operations can be performed on numeric types

 

 

 Declarations in Fortran

integer ::  i, j, k

•     declares 3 variables i, j, k of type integer

real ::  x, y

•     declares 2 variables x and y of type real

•     any number of such declarative statements may be present in a program

•     all of them occur before executable statements

 

Declarative Statement

•     general form of declarative statement

 

type, [attributes] :: list_of_variables

 

•     type defines the type of the variables

•     attributes is a list of optional attributes which define additional properties of the variables

•     list_of variables is a list of variable names which have the type and attributes specified

 

Variables

•     every variable name represents a sequence of consecutive memory locations which will hold its value

•     the number of locations assigned to a variable depend on its type

•     typically, integers and reals use one location

Variable Names in Fortran

•     can contain 1 to 31 characters

•     allowed characters are ‘a…z, A…Z, 0…9’ and the special symbol _

•     variable names must start with a letter

•     blanks are not allowed

•     no distinction between upper and lower case

•     meaningful names should be used

•     reserved words (real,..) should not be used

 

Variable Names

•     valid variable names

–   i, mass, angular_momentum, H_2O

•     invalid variable names

–   today's-date, 2m,

•     equivalent variable names

–   force, Force, FORCE, FoRcE, fOrCe

–   all of these denote the same variable

•     same form should be used throughout

Integer Variables

•     the possible values of an integer variable depend on the computer/ compiler

•     typically, one location containing 32 bits is used for representing an integer

•     thus possible values range between

   -2147483647 to 2147483647 ( 231-1)

•     a possible representation of  -100

 

 

Real Variables

•     are represented using exponents

•     also called floating point representation

•     102.356 would be represented as

•       1.02356*102

 

•     base is usually a power of 2

•     typically, base = 16, 26 bits for mantissa and 6 bits for the exponent ( including sign bits)

 

Approximations in Real Values

•     0.2 cannot be represented exactly

•     0.001100110011001100110011…

•     possible representation

 

•     this value is  0.2*(1-2-24)

•     normalized representation

•     binary point before first bit of mantissa

•     exponent is smallest possible

Real Variables

•     largest real value

= 2128 ~ 3.4*1038

•     smallest positive real value

= 2-149 ~ 1.4*10-45

 

•     can vary with compilers

•     some compilers ensure symmetric range

•     overflow or underflow may occur

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  

•     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

Constants

•     a constant of a particular type is a specific value of that type

•     12, -102, 3456843 are integer constants

•     integer constants are written in decimal

•     -2.04, 1.03e+2, -2.3e-3 are real constants

•     real constants are written in decimal using fixed or floating point

•     real constants must include a decimal point with a digit before and after it

Parameter Attribute

•     a name can be declared to be a constant by giving it the parameter attribute

integer, parameter ::  base = 10

•     defines base to be an integer constant whose value is 10

•     value of base is fixed throughout the program

•     naming of a constant explains its meaning

•     to change its value, only one change needed

 

Initializing Variables

•     variables can be given an initial value when they are declared

integer, parameter :: base = 10

integer :: digit = 5, number = base

real, parameter :: pi = 3.1415926

real :: radius = 1.0

•     base, pi are constants whose value is fixed

•     digit, number, radius are variables which have been assigned an initial value

Summary

•     variables are used to store data

•     every variable is given a name and type

•     variables declared to be integer or real are used to store numerical values

•     upper bound on value of an integer variable

•     real variables can represent  larger numbers

•     loss of accuracy in using real type variables

•     absolute error may be large but relative error is small

Summary

•     constants are values of a specific type

•     real and integer constants written in decimal

•     reals may be written using fixed or floating point representation

•     constants can be given a name using the parameter attribute

•     naming constants makes their meaning clear and simplifies modification of programs