Fortran: Expressions and Assignments Session Two ICoCSIS.

Slides:



Advertisements
Similar presentations
ISBN Chapter 7 Expressions and Assignment Statements.
Advertisements

1 Chapter 2 Basic Elements of Fortran Programming.
C Language Elements (II) H&K Chapter 2 Instructor – Gokcen Cilingir Cpt S 121 (June 22, 2011) Washington State University.
ISBN Chapter 7 Expressions and Assignment Statements.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
Chapter 2 Basic Elements of Fortan
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
Data types and variables
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
ISBN Chapter 7 Expressions and Assignment Statements.
ISBN Chapter 7 Expressions and Assignment Statements.
JavaScript, Fourth Edition
JavaScript, Third Edition
ISBN Lecture 07 Expressions and Assignment Statements.
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
Objectives You should be able to describe: Data Types
Chapter 2 Getting Started in C Programming
A First Book of ANSI C Fourth Edition
Python  By: Ben Blake, Andrew Dzambo, Paul Flanagan.
1 Week 12 Arrays, vectors, matrices and cubes. Introduction to Scientific & Engineering Computing 2 Array subscript expressions n Each subscript in an.
Fortran 1- Basics Chapters 1-2 in your Fortran book.
Copyright © 1998 by Addison -Wesley Longman, Inc. 1 Chapter 6 Arithmetic Expressions - Their evaluation was one of the motivations for the development.
1-1 University of Hail College of Computer Science and Engineering Department of computer Science and Software Engineering Course: ICS313: Fundamentals.
7-1 Chapter 7: Expressions and Assignment Statements Arithmetic Expressions –the fundamental means of specifying computations in a programming language.
Chapter 7 Expressions and Assignment Statements. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Arithmetic Expressions Arithmetic evaluation.
COMP4730/2002/lec7/H.Melikian Arithmetic Expressions Overloaded Operators Type Conversations Relational and Boolean Expressions Short Circuit Evaluation.
C H A P T E R S E V E N Expressions and Assignment Statements.
Chapter 2 part #4 Operator
2440: 211 Interactive Web Programming Expressions & Operators.
Chapter 3: Data Types and Operators JavaScript - Introductory.
Visual Basic.NET BASICS Lesson 4 Mathematical Operators.
CHAPTER 2 PART #4 OPERATOR 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101 By: Asma Alosaimi Edited.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Input, Output, and Processing
Class Review. Basic Unix Commands list files in a directory: ls list files in a directory: ls remove files: rm remove files: rm rename files: mv rename.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
OPERATORS.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Arithmetic Expressions
C Operators. CONTENTS C OPERATORS TYPES OF OPERATOR UNARY BINARY TERNARY ARITHMATIC RELATIONAL LOGICAL.
ISBN Chapter 7 Expressions and Assignment Statements.
Chapter 7 © 1998 by Addison -Wesley Longman, Inc Arithmetic Expressions - Their evaluation was one of the motivations for the development of the.
ISBN Chapter 7 Expressions and Assignment Statements.
Department of Electrical and Computer Engineering Introduction to C++: Primitive Data Types, Libraries and Operations By Hector M Lugo-Cordero August 27,
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Lecture III Start programming in Fortran Yi Lin Jan 11, 2007.
MATLAB Constants, Variables & Expression Nafees Ahmed Asstt. Professor, EE Deptt DIT, DehraDun.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
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.
7-1/27 Chapter 7 Expressions and Assignment Statements Introduction Arithmetic Expressions Overloaded Operators Type Conversions Relational and Boolean.
Variables, Operators, and Expressions
Expressions and Assignment Statements
Expressions and Assignment Statements
7.2 Arithmetic Expressions
Chapter 7: Expressions and Assignment Statements
Expressions and Assignment Statements
Chapter 7: Expressions and Assignment Statements
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Expressions and Assignment Statements
Operators and Expressions
Expressions and Assignment Statements
Arithmetic operations, decisions and looping
Expressions and Assignment Statements
College of Computer Science and Engineering
C Operators, Operands, Expressions & Statements
Chapter 3 Operators and Expressions
Chapter 7 Expressions and Assignment Statements.
Operator King Saud University
PRESENTED BY ADNAN M. UZAIR NOMAN
Presentation transcript:

Fortran: Expressions and Assignments Session Two ICoCSIS

Outline 1.Introduction: language elements 2.Scalar Numeric Expressions 3.Defined and Undefined Variables 4.Scalar Relational Expressions 5.Scalar Logical Expressions and Assignments 6.Scalar Character Expressions and Assignments 7.Array Expressions and Assignments 8.Pointers in Expressions and Assignments

Language Elements Letters:character set Words:constants, keywords, names (tokens) Phrases:expressions Sentences:statements

Expressions Expressions describe a computation that is to be carried out by the computer. During program execution every expression is evaluated and the value can be assigned to a variable. An expression in Fortran is formed of operands and operators, combined in a way that follows the rules of Fortran syntax. A simple expression involving a dyadic (or binary) operator has the form operand operator operand  x+y and a unary or monadic operator has the form operator operand  -y Parts of expressions without parentheses are evaluated successively from left to right for operators of equal precedencea/b/c  a/(b*c). The operands may be constants, variables, or functions and an expression may itself be used as an operand. The type and kind of the result are determined by the type and kind of the operands and do not depend on their values.

Scalar Numeric Expressions (1) A numeric expression is an expression whose operands are one of the three numeric types: integer, real, complex whose operators are **exponentiation * / multiplication, division + - addition, subtraction These operators are known as numeric intrinsic operators, and are listed here in their order of precedence. Minus sign (-) and the plus sign (+) can be used as unary operators. Note: In integer division, result is truncated: 8/3 is 2; 2**3 is 8, but 2**(-3) is 1/(2**3) is zero.

Scalar Numeric Expressions (2) Mixed-mode expressions: The object of the weaker (or simpler) of the two data types will be converted, or coerced, into the type of the stronger one. The result will also be that of the stronger type. abresults III IRR ICC RIR RRR RCC CIC CRC CCC Type of result a.op. b

Defined and Undefined Variables real :: speed The variable speed has no value, it is undefined and cannot be referenced. To assign a value assignment statement is needed speed = which defines it. For a compound object, all of its sub-objects that are not pointers must be individually defined before the object as a whole is regarded as defined. The pointer association status may be undefined, associated with a target, or disassociated.

Scalar Numerical Assignments variable = expression Type conversion: i = 7.3 ! i of type default integer a = ( , )! a of type default real To i is assigned a value of 7 and to a the value of If a literal constant is assigned to a variable of greater precision, the result will have the accuracy of the constant: Given a variable a of kind long, the result of a = 1.7will be less precise than that of a = 1.7_long

Scalar Relational Expressions The relational operators are: < or.lt. less than <= or.le. less than or equal == or.eq. equal /= or.ne. not equal > or.gt. greater than >= or.ge. greater than or equal If either or both of the expressions are complex, only the operators == and /= (or.eq. and.ne.) are available.

Scalar Logical Expressions and Assignments Logical constants, variables, and functions may appear as operands in logical expressions. The logical operators, in decreasing order of precedence, are: unary operator:.not. logical negation binary operators:.and. logical intersection.or. logical union (inclusive).eqv. logical equivalence.neqv. logical non-equivalence (allows exclusive or) (a.and..not.b.or..not.a.and.b) logical :: i,j,k,l.not.j(unary)j.and. k i.or. l.and..not.j(precedence)(.not.k.and. j.neqv..not.l).or. i

Scalar Character Expressions and Assignments The only intrinsic operator for character expressions is the concatenation operator //. ’AB’//’CD’ is ’ABCD’ word1(4:4)//word2(2:4)

Consider Example 2 implicit none means that all variables must be explicitly declared. Optional, but helps to avoid bugs (problems the “parameter” label indicates that these things have a fixed value that cannot be altered later “!” indicates a comment: everything after it is ignored The do...end do loop construct The if...else if...else...end if construct Comparison operators are, ==, /=, >=, <= Note: in f77 these are.lt..gt..eq..ne..ge..le. Indentation helps see the structure Write your program to do loops: Calculate the sum of all even numbers between 12 and 124.

Array Expressions and Assignments (1) Given the array declarations real, dimension(10,20) :: a,b real, dimension(5) :: v the following are examples of array expressions: a/b ! Array of shape (10,20), with elements a(i,j)/b(i,j) v+1. ! Array of shape (5), with elements v(i)+1.0 5/v+a(1:5,5) ! Array of shape (5), with elements 5/v(i)+a(i,5) a == b ! Logical array of shape (10,20), with elements !.true. if a(i,j) == b(i,j), and.false. Otherwise Two arrays of the same shape are said to be conformable.

Array Expressions and Assignments (2) The correspondence is by position in the extent and not by subscript value. For example, a(2:9,5:10) + b(1:8,15:20) has element values a(i+1,j+4) + b(i,j+14), i=1,2,...,8, j=1,2,...,6 a = a Replaces a(i,j) by a(i,j)+1.0 for i = 1, 2,..., 10 and j = 1, 2,..., 20. a(1,11:15) = v ! a(1,j+10) is assigned from v(j), j=1,2,...,5 If the expression includes a reference to the array variable or to a part of it, the expression is interpreted as being fully evaluated before the assignment commences. For example, the statement v(2:5) = v(1:4) results in each element v(i) for i = 2, 3, 4, 5 having the value that v(i-1) had prior to the commencement of the assignment.

Pointers in expressions and assignments A pointer may appear as a variable in the expressions and assignments, provided it has a valid association with a target. The target is accessed without any need for an explicit dereferencing symbol. In case the left-hand pointer to point to another target, the descriptor to be altered. This is called pointer assignment: pointer => target x => null() If the target in a pointer assignment statement is a variable that is not itself a pointer or a sub- object of a pointer target, it must have the target attribute. For example, the statement real, dimension(10), target :: y declares y to have the target attribute. Any non-pointer sub-object of an object with the target attribute also has the target attribute. The target attribute is required for the purpose of code optimization by the compiler. It is very helpful to the compiler to know that a variable that is not a pointer or a target may not be accessed by a pointer target.

Examples Example3 – miscellanies Continuing lines: – f95 use ‘&’ at the end of the line – f77: put any character in column 6 on next line Formats of constants: – Use ‘.’ to distinguish real from integer (avoid 2/3=0 !) – 1.234x is written as 1.234e-13 logical variables have 2 values:.true. or.false. (T or F) Variable naming rules: – start with letter – mix numbers, letters and _ – no spaces

Example 3 Example3 – miscellanies Some intrinsic mathematical functions abs (absolute value, real or integer); sqrt (square root) sin, cos, tan, asin, acos, atan, atan2: assume radians exp and log : log is natural log, use log10 for base 10. Some conversion functions int, nint, floor, ceiling, float, real mod(a,b) is remainder of x-int(x/y)*y max(a,b,c,….), min(a,b,c,…)

I/O read(*,*) and write(*,*) read(*,*) and write(*,*) do the same thing as read* and print* but are more flexible: – The 1st * can be changed to a file number, to read or write from a file – The 2nd * can be used to specify the format (e.g., number of decimal places)

Exercises Write your programs to: Convert a real number to the nearest integer Calculate the remainder after a is divided by b