+ All Categories
Home > Documents > Hans-Peter Plag November 6, 2014 Session 4 (Programming Languages) (Data Types and Variables) ...

Hans-Peter Plag November 6, 2014 Session 4 (Programming Languages) (Data Types and Variables) ...

Date post: 02-Jan-2016
Category:
Upload: ellen-moody
View: 213 times
Download: 0 times
Share this document with a friend
26
Transcript
Page 1: Hans-Peter Plag November 6, 2014 Session 4 (Programming Languages) (Data Types and Variables)  Expressions and Operators Flow Control.
Page 2: Hans-Peter Plag November 6, 2014 Session 4 (Programming Languages) (Data Types and Variables)  Expressions and Operators Flow Control.

Hans-Peter Plag November 6, 2014

Session 4

(Programming Languages)

(Data Types and Variables)

http://www.mari.odu.edu

Expressions and OperatorsFlow Control StatementsPrograms, Subroutines, Functions, ...

Assignment

Page 3: Hans-Peter Plag November 6, 2014 Session 4 (Programming Languages) (Data Types and Variables)  Expressions and Operators Flow Control.

Use a simple syntax to outline a procedure:- read an ASCII file (generated in an editor)- Separate the contents into logical record indicated by the string ‘\n’ - Write a file with records separated by ‘|’

Use a simple syntax to outline a procedure that can decode a command line consisting of:command-name p1=value p2=value ... pn=value

Page 4: Hans-Peter Plag November 6, 2014 Session 4 (Programming Languages) (Data Types and Variables)  Expressions and Operators Flow Control.

Use a simple syntax to outline a procedure:- read an ASCII file (generated in an editor)- Separate the contents into logical record indicated by the string ‘\n’ - Write a file with records separated by ‘|’

main program ... get file_name_in,file_name_out error = copy_file (file_name_in,file_name_out) if (not error) then message that file has been copied else message that file has not been copied end if ...end

integer procedure copy_file (file_in,file_out) declare file_in,file_out

declare string/character line declare file_access variable

if (not file_can_be_opened(file_out)) then error message copy_file = -1 else if (file_can_be_opened(file_in)) then while (get_next_line(line)) while (there_is_in_string(line,‘\n’)) replace ‘\n’ by ‘|’ end while write (line,file_out) end while copy_file = 0 else error message copy_file = -2 end ifend

Page 5: Hans-Peter Plag November 6, 2014 Session 4 (Programming Languages) (Data Types and Variables)  Expressions and Operators Flow Control.

main program declare string command, parameters(2,nmax) finished = false while (not finished) n_pars = nmax get_next_command (command,parameters,n_pars) switch (command) case (‘FILES’) call a procedure to open/close files case (‘DATE’) set date to parameters(1) case (‘DEFINE’) call a procedure to define run-time parameters case (‘SOLVE’) call a procedure to do a main part of the work case (‘END’) do some final step to finish the program finished = true case (‘DEFAULT’) message about this case end switch end whileend

procedure get_next_command (command,parameters,n_pars) declare command, parameters(2,n_pars)

declare string/character line

line = BLANK while (line equal blank) read next command line end while command = next_word (line,’ ‘) np = 0 while (line not equal blank) np = np + 1 parameter_string = next_word(line,‘ ‘) parameters(1,np) = next_word(parameter_string,’=’) parameters(2,np) = parameter_string) end while end

Use a simple syntax to outline a procedure that can decode a command line consisting of:command-name p1=value p2=value ... pn=value

Page 6: Hans-Peter Plag November 6, 2014 Session 4 (Programming Languages) (Data Types and Variables)  Expressions and Operators Flow Control.

Interpreter-based languages: are translated into machine code every time a script is executed (at run time).Compiler-based languages: are translated into machine code, then linked into an executable file. Can be executed afterwards (many times).

Interpreter-based languages: - syntax check, check of logic at run time- translation into machine code at run time reduces speed

Examples: - Linux/OS9 script (shell scripts) - PHP - Pearl - Javascript - Python

Compiler-based languages: - syntax check by compiler; check of some logic by compiler- compilation only once- linking checks libraries and compatibility with operating system- executable much faster than scripting language

Examples: - Fortran - C - C++ - Java

Steps:- write code in an editor- compile -> object code (binary)- link -> executable (binary)- test/validate

Steps:- write code in a shell/editor- execute (test/validate)

See more examples at: http://en.wikipedia.org/wiki/Comparison_of_programming_languagesSee also: http://en.wikibooks.org/wiki/Computer_Programming/Hello_world

Page 7: Hans-Peter Plag November 6, 2014 Session 4 (Programming Languages) (Data Types and Variables)  Expressions and Operators Flow Control.

See more examples at: http://en.wikipedia.org/wiki/Comparison_of_programming_languages

Page 8: Hans-Peter Plag November 6, 2014 Session 4 (Programming Languages) (Data Types and Variables)  Expressions and Operators Flow Control.

Fortran (2000): • originally based on ANSI Standards, now ISO standards: compilers are reliable• very good for numerical processing• also good for handling of text variables/strings• clear program control structures• subroutines and functions• very good tools for testing, validation, error detection • not good for interactions with the operating system or a GUI

There is a lot of code available in Fortran; mainly Fortran77 but also increasingly Fortran90/95/2000

Page 9: Hans-Peter Plag November 6, 2014 Session 4 (Programming Languages) (Data Types and Variables)  Expressions and Operators Flow Control.

C/C++: • based on ISO standards, latest is C++2014• very good for numerical processing• not that good for handling of text variables/strings• clear program control structures• procedures• reasonable tools for testing, validation, error detection • good for interactions with the operating system, less for a GUI• C++ very good for object-based programming

Page 10: Hans-Peter Plag November 6, 2014 Session 4 (Programming Languages) (Data Types and Variables)  Expressions and Operators Flow Control.

PHP: • no standard, de fact standard set by the interpreter• “hypertext preprocessor” • very good for web applications, generation of html code• can be used for stand-alone graphical applications using a command-line interpreter (CLI)

Page 11: Hans-Peter Plag November 6, 2014 Session 4 (Programming Languages) (Data Types and Variables)  Expressions and Operators Flow Control.

Python: • no standard• handles variable types dynamically• no limit to range of numbers• excellent file handling

Page 12: Hans-Peter Plag November 6, 2014 Session 4 (Programming Languages) (Data Types and Variables)  Expressions and Operators Flow Control.

How different is the syntax of languages?See: http://en.wikibooks.org/wiki/Computer_Programming/Hello_world

Page 13: Hans-Peter Plag November 6, 2014 Session 4 (Programming Languages) (Data Types and Variables)  Expressions and Operators Flow Control.

Data Types:Single-value:- Integer Numbers- Floating-point numbers- Logical/Booleans- Text/StringsCompound/collections:- Arrays- ObjectsResources/Handles- links to external resources (e.g. files)NULL data type- has no value

Page 14: Hans-Peter Plag November 6, 2014 Session 4 (Programming Languages) (Data Types and Variables)  Expressions and Operators Flow Control.

Variables: depends on programming languageFortran:Variables need to be declared before being used; some implicit declarationsinteger, integer*2/*4/*8; same for complexreal, real*4/*8/*16 logical, logical*1/*2/*4 character*narrays: type name(n1:m1, ..., nN,mN)Examples:integer nmax,nadd,nlinesparameter (nmax=1000,nadd=100,nlines=10)real*8 y(0:nmax),x(0:nmax,5)complex*8 z(nmax)character addresses*80(0:nadd,nlines)

Page 15: Hans-Peter Plag November 6, 2014 Session 4 (Programming Languages) (Data Types and Variables)  Expressions and Operators Flow Control.

Variables: depends on programming languagePHP:Variables are declared by usage (except for static variables)$a = 1;$b = “MARI”;$person[0] = “Jim”;$person[1] = “Smith”;$person[3] = “Norfolk”;$address[‘Name’] = “Jim Smith”;$address[‘City’] =”Norfolk”;

Page 16: Hans-Peter Plag November 6, 2014 Session 4 (Programming Languages) (Data Types and Variables)  Expressions and Operators Flow Control.

See more examples at: http://en.wikipedia.org/wiki/Comparison_of_programming_languages

Page 17: Hans-Peter Plag November 6, 2014 Session 4 (Programming Languages) (Data Types and Variables)  Expressions and Operators Flow Control.

Scope of Variables: depends on programming languageMany languages have local and global variables, depending on the declarationFortran has local variables and common blocks

Page 18: Hans-Peter Plag November 6, 2014 Session 4 (Programming Languages) (Data Types and Variables)  Expressions and Operators Flow Control.

x = y*z/3.y = sin(omega)*sqrt(a**2+b**2)

switch = a.gt.b.or.b.gt.c

Important:- precedence: for example, x/ are higher than +-: 2+2/2 -> 3; (2+2)/2 -> 2- associativity: 4/2*2 -> 4; (4/2)*2 -> 4; 4/(2*2) -> 1-

string = first_name//’ Plag’$string = $first_name.’ Plag’

Page 19: Hans-Peter Plag November 6, 2014 Session 4 (Programming Languages) (Data Types and Variables)  Expressions and Operators Flow Control.
Page 20: Hans-Peter Plag November 6, 2014 Session 4 (Programming Languages) (Data Types and Variables)  Expressions and Operators Flow Control.

If (expression) then statement

If (expression1) then statements 1elseif (expression2) then statements 2...elseif (expression n) then statements n else statements

Page 21: Hans-Peter Plag November 6, 2014 Session 4 (Programming Languages) (Data Types and Variables)  Expressions and Operators Flow Control.

switch (variable) case 1 do something ... case n do something

Page 22: Hans-Peter Plag November 6, 2014 Session 4 (Programming Languages) (Data Types and Variables)  Expressions and Operators Flow Control.

Loops:While (expression) do somethingFor start,condition,increment do something

Page 23: Hans-Peter Plag November 6, 2014 Session 4 (Programming Languages) (Data Types and Variables)  Expressions and Operators Flow Control.

Concept of main program in almost all languagesWith no structure, programs can be very long (“spaghetti code”)

Recurrent parts of code can be put into “procedures”Using “procedures” as much as possible makes programs short, clean, readable ...

Implementation depends on language

Page 24: Hans-Peter Plag November 6, 2014 Session 4 (Programming Languages) (Data Types and Variables)  Expressions and Operators Flow Control.

Implementation depends on language

Fortran:- Program: main program; cannot be called from another program- Subroutine: call subroutine_name(parameter,parameter,...)- Function: variable = function_name(parameter,parameter,...) functions can have all data types (integer, real, complex, character, ...)

C:- main: main program- Function: variable = function_name(parameter,parameter,...) functions can have all data types (integer, real, complex, character, VOID, ...)

Page 25: Hans-Peter Plag November 6, 2014 Session 4 (Programming Languages) (Data Types and Variables)  Expressions and Operators Flow Control.

Static versus dynamic:- static: on reentry of a procedure, all variables have the value they had at the end of the last call; - dynamic: all variables are undefined.

Recursive:- a function can call itself

Pros and cons:- static: program occupies a constant space in memory; - dynamic: size of memory occupied changes as needed.

Example:recursive function factorial (k) result (f)integer k,f

if (k.le.1) then f = 1else f = k* factorial(k-1)end ifend

Page 26: Hans-Peter Plag November 6, 2014 Session 4 (Programming Languages) (Data Types and Variables)  Expressions and Operators Flow Control.

Recommended