Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 2 Basic Elements of Fortan

Similar presentations


Presentation on theme: "Chapter 2 Basic Elements of Fortan"— Presentation transcript:

1 Chapter 2 Basic Elements of Fortan
INTRODUCTION AND FLUID PROPERTIES HAND OUTS Chapter 2 Basic Elements of Fortan Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul - Turkey Tel: +90 ( ) Fax: +90 ( ) © 2003, ALİ CAN TAKİNACI

2 INTRODUCTION AND FLUID PROPERTIES
HAND OUTS Fortran Character Set Fortran is case insensitive. C++ and Java are case sensitive (A and a are different) © 2010, Dr. ALİ CAN TAKİNACI BASIC ELEMENTS OF FORTRAN © 2003, ALİ CAN TAKİNACI

3 The structure of a fortran statement
INTRODUCTION AND FLUID PROPERTIES HAND OUTS The structure of a fortran statement 1. The declaration section. This section consists of a group of nonexecutable statements at the beginning of the program that define the name of the program and the number and types of variables referenced in the program. 2. The execution section. This section consists of one or more statements describing the actions to be performed by the program. 3. The termination section. This section consists of a statement or statements stopping the execution of the program and telling the compiler that the program is complete. Upto 31 character length © 2010, Dr. ALİ CAN TAKİNACI BASIC ELEMENTS OF FORTRAN © 2003, ALİ CAN TAKİNACI

4 INTRODUCTION AND FLUID PROPERTIES
HAND OUTS Fortran statements may be entered anywhere on a line, and each line may be up to 132 characters long. If a statement is too long to fit onto a single line, then it may be continued on the next line by ending the current line (and optionally starting the next line) with an ampersand (&) character. For example, the following three Fortran statements are identical: © 2010, Dr. ALİ CAN TAKİNACI BASIC ELEMENTS OF FORTRAN © 2003, ALİ CAN TAKİNACI

5 The structure of a fortran program
INTRODUCTION AND FLUID PROPERTIES HAND OUTS The structure of a fortran program Each Fortran program consists of a mixture of executable and nonexecutable statements, which must occur in a specific order. Almost all Fortran program units is divided into three 1. The declaration section: This section consists of a group of nonexecutable statements at the beginning of the program that define the name of the program and the number and types of variables referenced in the program. 2. The execution section: This section consists of one or more statements describing the actions to be performed by the program. 3. The termination section: This section consists of a statement or statements stopping the execution of the program and telling the compiler that the program is complete. Note that comments may be inserted freely anywhere within, before, or after the program. © 2010, Dr. ALİ CAN TAKİNACI BASIC ELEMENTS OF FORTRAN © 2003, ALİ CAN TAKİNACI

6 Constants and Variables
INTRODUCTION AND FLUID PROPERTIES HAND OUTS Constants and Variables A Fortran constant is a data object that is defined before a program is executed, and that does not change value during the execution of the program. A Fortran variable is a data object that can change value during the execution of a program. (The value of a Fortran variable mayor may not be initialized before a program is executed.) When a Fortran compiler encounters a variable, it reserves a known location in memory for the variable, and then references that memory location whenever the variable is used in the program. Valid variables Invalid variables © 2010, Dr. ALİ CAN TAKİNACI BASIC ELEMENTS OF FORTRAN Slide No: 6 © 2003, ALİ CAN TAKİNACI 6

7 BASIC ELEMENTS OF FORTRAN
INTRODUCTION AND FLUID PROPERTIES HAND OUTS Character Constants and Variables The character data type consists of strings of alphanumeric characters. A character constant is a string of characters enclosed in single (') or double (") quotes. The minimum number of characters in a string is I, while the maximum number of characters in a string varies from compiler to compiler. Valid Characters Invalid Characters © 2010, Dr. ALİ CAN TAKİNACI BASIC ELEMENTS OF FORTRAN Slide No: 7 © 2003, ALİ CAN TAKİNACI 7

8 Default and Explicit Variable Typing
INTRODUCTION AND FLUID PROPERTIES HAND OUTS Default and Explicit Variable Typing There are two possible ways in which the type of a variable can be defined: default typing and explicit typing. If the type of a variable is not explicitly specified in the program, then default typing is used. By default: Any variable names beginning with the letters I, J, K, L, M, or N are assumed to be of type INTEGER. Any variable names starting with another letter are assumed to be of type REAL. © 2010, Dr. ALİ CAN TAKİNACI BASIC ELEMENTS OF FORTRAN Slide No: 8 © 2003, ALİ CAN TAKİNACI 8

9 Keeping Constants Consistent in a Program
INTRODUCTION AND FLUID PROPERTIES HAND OUTS Keeping Constants Consistent in a Program Named constants are created by using the PARAMETER attribute of a type declaration statement. The form of a type declaration statement with a PARAMETER attribute is Note that character length which is 14 has not been defined. © 2010, Dr. ALİ CAN TAKİNACI BASIC ELEMENTS OF FORTRAN Slide No: 9 © 2003, ALİ CAN TAKİNACI 9

10 ASSIGNMENT STATEMENTS AND ARITHMETIC CALCULATIONS
INTRODUCTION AND FLUID PROPERTIES HAND OUTS ASSIGNMENT STATEMENTS AND ARITHMETIC CALCULATIONS Calculations are specified in Fortran with an assignment statement, whose general form is variable_name = expression The assignment statement calculates the value of the expression to the right of the equal sign, and assigns that value to the variable named on the left of the equal sign. Note that the equal sign does not mean equality in the usual sense of the word. Instead, it means: store the value of expression into location variable_name. For this reason, the equal sign is called the assignment operator. A statement like i = i + 1 is complete nonsense in ordinary algebra, but makes perfect sense in Fortran. In Fortran, it means: take the current value stored in variable i, add one to it, and store the result back into variable i . © 2010, Dr. ALİ CAN TAKİNACI BASIC ELEMENTS OF FORTRAN Slide No: 10 © 2003, ALİ CAN TAKİNACI 10

11 BASIC ELEMENTS OF FORTRAN
INTRODUCTION AND FLUID PROPERTIES HAND OUTS © 2010, Dr. ALİ CAN TAKİNACI BASIC ELEMENTS OF FORTRAN Slide No: 11 © 2003, ALİ CAN TAKİNACI 11

12 BASIC ELEMENTS OF FORTRAN
INTRODUCTION AND FLUID PROPERTIES HAND OUTS Integer Arithmetic Integer arithmetic is arithmetic involving only integer data. Integer arithmetic always produces an integer result. This is especially important to remember when an expression involves division, since there can be no fractional part in the answer. If the division of two integers is not itself an integer, the computer automatically truncates the fractional part of the answer. This behavior can lead to surprising and unexpected answers. For example, integer arithmetic produces the following strange results: Because of this behavior, integers should never be used to calculate real-world quantities that vary continuously, such as distance, speed, time, etc. They should only be used for things that are intrinsically integer in nature, such as counters and indices. © 2010, Dr. ALİ CAN TAKİNACI BASIC ELEMENTS OF FORTRAN Slide No: 12 © 2003, ALİ CAN TAKİNACI 12

13 BASIC ELEMENTS OF FORTRAN
INTRODUCTION AND FLUID PROPERTIES HAND OUTS Real Arithmetic Real arithmetic (or floating-point arithmetic) is arithmetic involving real constants and variables. Real arithmetic always produces a real result that is essentially what we would expect. For example, real arithmetic produces the following results: Because of this behavior, integers should never be used to calculate real-world quantities that vary continuously, such as distance, speed, time, etc. They should only be used for things that are intrinsically integer in nature, such as counters and indices. © 2010, Dr. ALİ CAN TAKİNACI BASIC ELEMENTS OF FORTRAN Slide No: 13 © 2003, ALİ CAN TAKİNACI 13

14 Hierarchy of Operations
INTRODUCTION AND FLUID PROPERTIES HAND OUTS Hierarchy of Operations © 2010, Dr. ALİ CAN TAKİNACI BASIC ELEMENTS OF FORTRAN Slide No: 14 © 2003, ALİ CAN TAKİNACI 14

15 BASIC ELEMENTS OF FORTRAN
INTRODUCTION AND FLUID PROPERTIES HAND OUTS The order in which the arithmetic operations are evaluated is: 1. The contents of all parentheses are evaluated first, starting from the innermost parentheses and working outward. 2. All exponentials are evaluated, working from right to left. 3. All multiplications and divisions are evaluated, working from left to right. 4. All additions and subtractions are evaluated, working from left to right. © 2010, Dr. ALİ CAN TAKİNACI BASIC ELEMENTS OF FORTRAN Slide No: 15 © 2003, ALİ CAN TAKİNACI 15

16 Mixed-Mode Arithmetic
INTRODUCTION AND FLUID PROPERTIES HAND OUTS Mixed-Mode Arithmetic The rules governing mixed-mode arithmetic can be confusing to beginning programmers, and even experienced programmers may trip up on them from time to time. This is especially true when the mixed-mode expression involves division. Consider the following expressions: © 2010, Dr. ALİ CAN TAKİNACI BASIC ELEMENTS OF FORTRAN Slide No: 16 © 2003, ALİ CAN TAKİNACI 16

17 BASIC ELEMENTS OF FORTRAN
INTRODUCTION AND FLUID PROPERTIES HAND OUTS Fortran 95/2003 includes five type conversion functions that allow us to explicitly control the conversion between integer and real values. These functions are described in Table 2-3. The REAL, INT, NINT, CEILING, and FLOOR functions may be used to avoid undesirable mixed-mode expressions by explicitly converting data types from one form another. © 2010, Dr. ALİ CAN TAKİNACI BASIC ELEMENTS OF FORTRAN Slide No: 17 © 2003, ALİ CAN TAKİNACI 17

18 BASIC ELEMENTS OF FORTRAN
INTRODUCTION AND FLUID PROPERTIES HAND OUTS INTRINSIC FUNCTIONS In mathematics, a function is an expression that accepts one or more input values and calculates a single result from them. They are called intrinsic functions. Less common functions are not included in the Fortran language, but the user can supply any function needed to solve a particular problem as either an external function or an internal function. External functions will be described in Chapter 7, and internal © 2010, Dr. ALİ CAN TAKİNACI BASIC ELEMENTS OF FORTRAN Slide No: 18 © 2003, ALİ CAN TAKİNACI 18

19 BASIC ELEMENTS OF FORTRAN
INTRODUCTION AND FLUID PROPERTIES HAND OUTS INTRINSIC FUNCTIONS © 2010, Dr. ALİ CAN TAKİNACI BASIC ELEMENTS OF FORTRAN Slide No: 19 © 2003, ALİ CAN TAKİNACI 19

20 LIST-DIRECTED INPUT AND OUTPUT STATEMENTS
INTRODUCTION AND FLUID PROPERTIES HAND OUTS LIST-DIRECTED INPUT AND OUTPUT STATEMENTS An input statement reads one or more values from an input device and stores them into variables specified by the programmer. The input device could be a keyboard in an interactive environment, or an input disk file in a batch environment. An output statement writes one or more values to an output device. The output device could be a display screen in an interactive environment, or an output listing file in a batch environment. © 2010, Dr. ALİ CAN TAKİNACI BASIC ELEMENTS OF FORTRAN Slide No: 20 © 2003, ALİ CAN TAKİNACI 20

21 BASIC ELEMENTS OF FORTRAN
INTRODUCTION AND FLUID PROPERTIES HAND OUTS The term list-directed input means that the types of the variables in the variable list determine the required format of the input data (Figure 2-4). For example, consider the following statements: . The input data supplied to the program must consist of two integers, a real number, and a character string. Furthermore, they must be in that order. The values may be all on one line separated by commas or blanks, or they may be on separate lines. The list directed READ statement will continue to read input data until values have been found for all of the variables in the list. If the input data supplied to the program at execution time is © 2010, Dr. ALİ CAN TAKİNACI BASIC ELEMENTS OF FORTRAN Slide No: 21 © 2003, ALİ CAN TAKİNACI 21

22 BASIC ELEMENTS OF FORTRAN
INTRODUCTION AND FLUID PROPERTIES HAND OUTS The term list-directed output means that the types of the values in the output list of the write statement determine the format of the output data. For example, consider the following statements: The output resulting from these statements is: © 2010, Dr. ALİ CAN TAKİNACI BASIC ELEMENTS OF FORTRAN Slide No: 22 © 2003, ALİ CAN TAKİNACI 22

23 LIST-DIRECTED INPUT AND OUTPUT STATEMENTS
INTRODUCTION AND FLUID PROPERTIES HAND OUTS LIST-DIRECTED INPUT AND OUTPUT STATEMENTS An input statement reads one or more values from an input device and stores them into variables specified by the programmer. The input device could be a keyboard in an interactive environment, or an input disk file in a batch environment. An output statement writes one or more values to an output device. The output device could be a display screen in an interactive environment, or an output listing file in a batch environment. © 2010, Dr. ALİ CAN TAKİNACI BASIC ELEMENTS OF FORTRAN Slide No: 23 © 2003, ALİ CAN TAKİNACI 23

24 INITIALIZATION OF VARIABLES
INTRODUCTION AND FLUID PROPERTIES HAND OUTS INITIALIZATION OF VARIABLES There are three techniques available to initialize variables in a Fortran program: assignment statements, READ statements, and initialization in type declaration statements. © 2010, Dr. ALİ CAN TAKİNACI BASIC ELEMENTS OF FORTRAN Slide No: 24 © 2003, ALİ CAN TAKİNACI 24

25 THE IMPLICIT NONE STATEMENT
INTRODUCTION AND FLUID PROPERTIES HAND OUTS THE IMPLICIT NONE STATEMENT There is another very important nonexecutable statement: the IMP L I C I T NON E statement. When it is used, the IMPLICIT NONE statement disables the default typing provisions of Fortran. A majority of programming errors are simple typographical errors. The IMPLICIT NONE statement catches these errors at compilation time, before they can produce subtle errors during execution. For example, consider the following simple program: In this program, the variable time is misspelled tmi e at one point. When this program is compiled with the Compaq Visual Fortran compiler and executed, the output is "Time = O. OOOOOOE+OO", which is the wrong answer! In contrast, consider the same program with the IMPLICIT NONE statement present: © 2010, Dr. ALİ CAN TAKİNACI BASIC ELEMENTS OF FORTRAN Slide No: 25 © 2003, ALİ CAN TAKİNACI 25


Download ppt "Chapter 2 Basic Elements of Fortan"

Similar presentations


Ads by Google