1 / 25

Chapter 2 Basic Elements of Fort an

Chapter 2 Basic Elements of Fort an. Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul - Turkey Tel: +90 (212 285 6519) Fax: +90 (212 285 6508) E-mail: takinaci@itu.edu.tr. Fortran Character Set.

oneida
Download Presentation

Chapter 2 Basic Elements of Fort an

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 2 Basic Elements of Fortan Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul - Turkey Tel: +90 (212 285 6519) Fax: +90 (212 285 6508) E-mail: takinaci@itu.edu.tr

  2. Fortran Character Set Fortran is case insensitive. C++ and Java are case sensitive (A and a are different) BASIC ELEMENTS OF FORTRAN

  3. 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 BASIC ELEMENTS OF FORTRAN

  4. 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 nextline) with an ampersand (&) character. For example, the following three Fortran statements are identical: BASIC ELEMENTS OF FORTRAN

  5. 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. BASIC ELEMENTS OF FORTRAN

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. © 2010, Dr. ALİ CAN TAKİNACI BASIC ELEMENTS OF FORTRAN Slide No: 11

  12. 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

  13. 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

  14. Hierarchy of Operations © 2010, Dr. ALİ CAN TAKİNACI BASIC ELEMENTS OF FORTRAN Slide No: 14

  15. 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

  16. 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

  17. 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

  18. 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

  19. INTRINSIC FUNCTIONS © 2010, Dr. ALİ CAN TAKİNACI BASIC ELEMENTS OF FORTRAN Slide No: 19

  20. 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

  21. The term list-directed input means that the types of the variables in the variable list determine the requiredformat 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 listdirected 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

  22. 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

  23. 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

  24. 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

  25. 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 atcompilation 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

More Related