Presentation is loading. Please wait.

Presentation is loading. Please wait.

2.4 The Assignment Statement Values are assigned to variables using the assignment statement : variable = expression Examples: Ex.1: REAL :: XCoordinate,

Similar presentations


Presentation on theme: "2.4 The Assignment Statement Values are assigned to variables using the assignment statement : variable = expression Examples: Ex.1: REAL :: XCoordinate,"— Presentation transcript:

1 2.4 The Assignment Statement Values are assigned to variables using the assignment statement : variable = expression Examples: Ex.1: REAL :: XCoordinate, YCoordinate INTEGER :: Number, Term XCoordinate = 5.23 YCoordinate = 25.0 Number = 17 Term = Number / 3 + 2 XCoordinate = 2.0 * XCoordinate Print*, Xcoordinate,Ycoordinate,Number,Term

2 Ex.2: INTEGER :: N REAL :: Alpha, Beta N = 9 Alpha = 3 Beta = (N + 3) / 5 Print*,N,Alpha,Beta Ex.3: INTEGER :: I, Mu, Kappa REAL :: X X = 5.75 I = 3.14159 Kappa = X / 2.0 Mu = 1.0 / X Print*,I,Mu,Kappa,X

3 2.5 Numeric Functions FORTRAN provides some common functions that are encountered often. See Table 2.2 (Course Note) Examples on Numeric Functions: 1. SQRT(7.0) 2. SQRT(B ** 2 – 4 * A * C) 3. X ** (1.0 / REAL (N)) 4. REAL(NUM) ** (1.0 / REAL(N)) See Remarks on pp 18.

4 Character Operations The // is used to combine two character values. Example: “centi” // “meters”  “centimeters” if SquareUnit is a character variable =“square” then SquareUnit // “centi” // “meters”  “square centimeters”

5 Substrings Examples: CHARACTER(15) :: Course Course = “Engineering” Course (: 6)  “Engine” Course (8 : )  “ringbbbb” Course (3:6)  ”????” ”gine”

6 2.6 Input/Output Two types of input/output statements: List- directed and formatted list-directed Type: Read*,Input_list PRINT *, output_list WRITE (*, *) output_list

7 Examples: 1. PRINT *, “At time”, Time, “seconds”  At time 4.5000000 seconds 2. PRINT *, “the vertical velocity is”, Velocity, “m / sec”  the vertical velocity is 45.8700752 m/sec See Rules on pp21

8 2.7 Program Composition and Format FORTRAN Program Form: 1.Heading 2.Specification part 3.Execution part 4.Subprogram part 5.END program statement

9 1. program heading, has the form: PROGRAM name  name is a legal FORTRAN identifier.  This statement marks the beginning of the program and gives it a name. opening documentation explains the purpose of the program, clarifies the choice of variable names, and provides other information about the program. 2. specification part of a program must appear next. The first statement in this part should be: IMPLICIT NONE

10 3. execution part Includes ALL statements that specify actions to be performed during program execution. Examples: - assignment statements - input/output statements 4. END Program Statement: indicates to the compiler the end of the program

11 Program Format main rules of FORTRAN90 programming are: A line may have a maximum of 132 characters. A line may contain more than one statement, separated by (;). An (&) must be placed at the end of each line that is to be continued to the next line. At most 39 continuation lines are permitted. If a character string must be continued from one line to the next, an (&) must be placed at the end of the line containing 1st part of the string, and another (&) must be placed before the 1st character of the continuation of the string: example: PRINT *, “Enter the initial height (m) and & &the initial velocity (m/sec): “

12 Any characters after (!)—except within a string constant—form a comment. Example: INTEGER :: Number! Number of data values. If a statement requires a statement label, this label must precede the statement and must be separated from it by at least one blank. Statement labels must be integers in the range 1 through 99999.

13 2.8 Types of Errors Syntax errors or compile-time errors: Errors in the program’s syntax, such as incorrect punctuation or misspelling. will be detected during compilation Run-time errors: such as an attempt to divide by zero in an arithmetic expression. may not be detected until execution of the program Logic errors arise in the design of the algorithm or in the coding of the program that implements the algorithm. more subtle and difficult to be identified

14 Tutorials CW for Lab 1 (1 and 2) Lab#1 (1) Write a program that reads your full name, age and your car’s color, then write out your age and car’s color.

15


Download ppt "2.4 The Assignment Statement Values are assigned to variables using the assignment statement : variable = expression Examples: Ex.1: REAL :: XCoordinate,"

Similar presentations


Ads by Google