Unit 1 Introductory Fortran Concept
Total Page:16
File Type:pdf, Size:1020Kb
UNIT 1 INTRODUCTORY FORTRAN CONCEPT Structure 1.0 Introduction 1.1 Objectives 1.2 Character Set, Constants and Variables 1.2.1 Character Set 1.2.2 Constant and Variables 1.2.3 Numerical Constants and Variables 1.3 Data Types 1.3.1 Integer Dua Types 1.3.2 Real Data Types 1.3.3 Double Precision Data Types 1.3.4 Complex Data Types 1.3.5 Logical Data vpes 1.3.6 Character Data Types 1.4 Subscripted Variables and Simple Fortran Functims 1.4.1 Subscripted Variables 1.4.2 Simple FORTRAN Functions 1.5 FORTRAN Expressions 1.5.1 Design and Evaluation of Arithmetic Expressions 1.5.2 Integcr Mode Expressions 1.5.3 Real Mode Expressions 1.5.4 Complex Mode Expressions 1.5.5 Mixed Mode Expressions 1.5.6 Logical Expressions L .7 Relational Logical Expressions Xaming FORTRAN hograrnmes I 4ssignmcnt Statement I ., FORTRAN Commands 1.8.1 Format froe READ Command 1.8.2 Format Free PRINT Command 1.8.3 Naming Constants 1.8.4 COMMENT Command 1.8.5 Ending Programs 1.8.6 Character Expressions and Assignment 1.9 Summary 1.10 Model Answers I 1.0 INTRODUCTION You know that data may be characterized as numeric or non-numeric. Numeric entities -such as constants, variables, functions ..- used in arithmetic computations. FORTRAN 77 allows a wide variety of numeric data types, such as integer, real. double precision and complex. Similarly, .datatypcs, such as logical (bolean) and character are available for non-numaic computing. Character data type is vcry useful for text-processing applications. Here, we shall present different data types, constants and variable as available in FORTFUN 77.- FORTRAN permits the use of a wide class of quantities, like integer, real, complex. double precision, logical and character. The type of constant., variables and functions which constitute the expression define its type. We shdl explain the definition consuuction and evaluation of expression and design of statements. After reading this unit, you will be able to use FORTRAN Character Set to &fine constants and variables FORTRAN 77 and FORTRAN 90 use FORTRAN character Set to define subscripted variables and other data types use FORTRAN expressions viz.: Arithmetical, Logical and Character name a FORTRAN Program give simple Input/Output Commands. 1.2 CHARACTER SET, CONSTANTS AND VARIABLES we will learn about the notations and characters which are specially meant for and the way in which the constant and variables are defined in FORTRAN 77. 1.2.1 The FORTRAN 77 Character set is : Alphabetic characters A-Z Numeric characters 0-9 Special characters (Decimal) . (Comma) ( (Left Parenthesis) ) (Right parenthesis) + (Plus) $ (Currency.~ymbol,dollar) Blank (A ) 8 (Quote) - (Equals) (Colon) # (Space) The alphabetic and numeric characters are collectively referred to as Alphanumeric characters. All FORTRAN quantities, names, statements and data are constructed using characters from the character set with the help of certain predefined rules of FORTRAN. There are certain I words which identify statements and FORTRAN supplied functions. Such words are ref- to as Key words. Examples of Keywords are: I&AD, JF, DO,DOUBLE PRECISION. and so on. I Keywords must be spelt and written as defined by the language. FORTRAN key words are not reserved words (reserved words are used only for specific purposes). They are recognized as key words by the compiler only when they appear at their proper place and context. Otherwise they may be used as ordinary FORTRAN names. 1.2.2 Constants and Variables Variables and constants are defined similarly in FORWas was in BASIC language. However, rules of defining are specif- to FORTRAN,though similarity also exists. 'Ihese are : A variable must have a name. The variable name refers to specific memory locations where its value is stored. The name of a variable may consist of upto six characters (some implementations may allow more than six characters). The fmt character must be alphabetic Special characters (such as &, #, $, etc.) are not allowed to appear in the variable name. Blank spaces in a name are ignored. A vuiablc cannot be used in computations unless it has been assigned a value i.e. initialized. A conslult is 3 quantity which remains unchanged during computations.in a program. FORTRAN 77 pcnnits the following kinds of constants and variables : Nuillcrical Logical Character Jnboductoy FORTRAN 1.2.3 Numerical constants and variables Concept Constants and variables used in arithmetic expressions are referred to as Numeric. They are of two kinds: (a) Integer (b) Real Single precision Double predision Complex I 1.3 DATA TYPES t ' Data in FORTRAN could be classifitxi into six type viz. Integer. Real, Double Precision, Complex, Lagical and Character. This &tion shows how constant and variables of each data type is represented in FORTRAN. 1.3.1 Integer Data Types Integer data type signifies a class of entities that are integral in nature. They can be constants, variables or expressions. Integer Constant: An integer constant is a number without a decimal. It may be positive, negative or zero. Positive sign may or may not appear before the number but the negative sign always precedes the number. The magnitude of the number must not exceed the allowed I maximum which varies from computer to computer, In systems with word size = 32 bits. 1 maximum value allowed for a constant is (z3'- 2) = 2147483641. No commas are allowed to appear in constants. Examples of integer constants are: Integer Variables :An integer variable is an entity which can assume only integer constant as its value at any point in the calculations. For a variable to be integer, the first character of the name should begin with either of the letters : I, J, K. L, M. N, (This is a FORW convention and can be overridden.) Examples of integer variables are: MATRIX N123A INTGR MAM Another way to dkclare variables extlicitly as integer is via the declaration INTEGER. Its syntax is INTEGER list Where list is name of variables that has been specified as INTEGER type. An example is INTEGER RP, DEPTH, PT Now variables R, DEFTH and PT,though do not begin with any of the letters I - N, are treated as integer. 1.3.2 Real Data Types Real data types refers to a class of entities that are not integral in nature. They may be constants, variables or expressions. ' Real Constants :A real constant is a number with a decimal point which may appear anywhere in the number. The number may be positive, negative or zero. Unsigned constant is taken as positive. Cornmasare not allowed to appear in the constant. Example are: The presence of a decimal informs the system that the number is real. Real constants are also written with an exponent Q. For instance 12.34 = 0.1234~lo2 = 1.234 x 10' = 1234.x The decimal part is called the mantissa and power part is call@+the exponent. In FORTWIN, these numbers are written as: 12.34 = 0.1234B2~1.234E+1=1234&2 and so on. Here E+" stands for 10'" .When the decimal is on the extreme left (as in 0.1234~lo2) the number is said to be in the 'normalized' form. All real numbers are stored in the computer storage in this form. The need for writing numbers in exponent form arises because FORTRAN dots not allow subscripts or superscripts to appear with numbers. Only exponent is permitted. The value of exponent (n) must always be integer or zero. The magnitude of the exponent varies from computer to computer and depends on the word size. Real Variables :A real variable can assume only a real constant as its value. The name of a real variable begins with any of the letters A - H and 0 - Z. This is by convention and can be over ridden. Examples of real variables are : AIT BIM ALPHA T32QR PRITI The declaration REAL can be used to declare variables of type real as: REAL LIST Where list is the name of variables that are to be declared as type real. An example is : REAL JK. BANGLE. UP 1.3.3 Double Precision Data Types FORTRAN Defines both constants and variables having precision twice that of the nonnal real data types. These are explained below : Dduble Precision Constants :These are real constants which are coded in the exponential form with a letter D, instead of letter E. As for example: 12.357894213D+07, -0.321456987D-1-02, +.35724 D - 20 and so on. The number of digits in a double precision constant is always greater than that for a real single precision constant. The maximum magnitude of a double precision constant also varies from computer to computer. We shall assume that the double precision constant consists of 14 places of decimals. Some examples of double precision mode constants are: Double hecision Variables : These are real variables which occupy double the memory than the single precision variables. For a variable to be double precision, information has to bt: supplied. This is done by using the specification DOUBLE PRECISION which is followed by the list of variables which we want to be of double precision mode. This is done as follows : DOUBLE PRECIS1,ON list where DOUBLE PRECISION are keywords, must be written as and list represents variable names, each separated from the other by a comma. Variables which follow DOUBLE PRECISION words are treated as of double precision mode, irrespective of whether they begin with letters I -N or A-H, 0-Z. Consider DOUBLE PRECISION A, I, J, RUM Variables A, I, J, RUM have been declared to be of double precision mde. As many variables as necessary may be included in a single specification.