Chapter 2 Basic Elements of Fortan

Slides:



Advertisements
Similar presentations
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Advertisements

1 Chapter 2 Basic Elements of Fortran Programming.
Lecture 2 Introduction to C Programming
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Chapter 8 Introduction to Arrays Part II Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul.
Chapter 3 Program Design and Branching Structures Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering.
Chapter 7 Introduction to Arrays Part I Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Chapter 11 Additional Intrinsic Data Types Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul.
Chapter 4 Loops and Character Manipulation Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul.
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
Data types and variables
 2002 Prentice Hall. All rights reserved. 1 Chapter 2 – Introduction to Python Programming Outline 2.1 Introduction 2.2 First Program in Python: Printing.
Chapter 2 Data Types, Declarations, and Displays
Introduction to C Programming
Chapter 9 Introduction to Procedures Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul -
Chapter 6 AN INTRODUCTION TO FILES AND FILE PROCESSING Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering.
Basic Elements of C++ Chapter 2.
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
Fortran- Subprograms Chapters 6, 7 in your Fortran book.
CSCI 1730 January 17 th, 2012 © by Pearson Education, Inc. All Rights Reserved.
Objectives You should be able to describe: Data Types
Java Chapter 2 Creating Classes. Class Defines structure of an object or set of objects; Includes variables (data) and methods (actions) which determine.
A First Book of ANSI C Fourth Edition
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Fortran 1- Basics Chapters 1-2 in your Fortran book.
Chapter 2 Overview of C Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
A First Book of ANSI C Fourth Edition Chapter 3 Processing and Interactive Input.
CH2 – Using Data. Constant Something which cannot be changed Data Type Format and size of a data item Intrinsic Data Types Pg. 47 – Table 2-1 Basic ones.
Input, Output, and Processing
Chapter 2: Using Data.
Computer Programming TCP1224 Chapter 4 Variables, Constants, and Arithmetic Operators.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
CPS120: Introduction to Computer Science Operations Lecture 9.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
CHAPTER 1 INTRODUCTION TO COMPUTING 1.1 Introduction.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 3 – Inventory Application: Introducing Variables,
Chapter 9: Completing the Basics. In this chapter, you will learn about: – Exception handling – Exceptions and file checking – The string class – Character.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
1Object-Oriented Program Development Using C++ Built-in Data Types Data type –Range of values –Set of operations on those values Literal: refers to acceptable.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Chapter 2 Introduction to C++ Programming
Chapter 2 - Introduction to C Programming
Basic Elements of C++.
Chapter 2 - Introduction to C Programming
Basic Elements of C++ Chapter 2.
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Introduction to C++ Programming
Chapter 2 - Introduction to C Programming
A First Book of ANSI C Fourth Edition
Chapter 2 - Introduction to C Programming
Engineering Problem Solving with C++ An Object Based Approach
Chapter 2 - Introduction to C Programming
Primitive Types and Expressions
DATA TYPES AND OPERATIONS
Introduction to C Programming
Data Types and Arithmetic in C
Presentation transcript:

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 (212 285 6519) Fax: +90 (212 285 6508) E-mail: takinaci@itu.edu.tr © 2003, ALİ CAN TAKİNACI

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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