Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Hans-Peter Plag November 6, 2014 Session 4 (Programming Languages) (Data Types and Variables) Expressions and Operators Flow Control."— Presentation transcript:

1

2 Hans-Peter Plag November 6, 2014 Session 4 (Programming Languages) (Data Types and Variables) http://www.mari.odu.edu Expressions and Operators Flow Control Statements Programs, Subroutines, Functions,... Assignment

3 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

4 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 if end

5 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 while end 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

6 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_languages See also: http://en.wikibooks.org/wiki/Computer_Programming/Hello_world

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

8 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

9 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

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

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

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

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

14 Variables: depends on programming language Fortran: Variables need to be declared before being used; some implicit declarations integer, integer*2/*4/*8; same for complex real, real*4/*8/*16 logical, logical*1/*2/*4 character*n arrays: type name(n1:m1,..., nN,mN) Examples: integer nmax,nadd,nlines parameter (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)

15 Variables: depends on programming language PHP: 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”;

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

17 Scope of Variables: depends on programming language Many languages have local and global variables, depending on the declaration Fortran has local variables and common blocks

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

19

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

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

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

23 Concept of main program in almost all languages With 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

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

25 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 = 1 else f = k* factorial(k-1) end if end

26


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

Similar presentations


Ads by Google