Post on 02-Jan-2016
transcript
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
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
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
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
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
See more examples at: http://en.wikipedia.org/wiki/Comparison_of_programming_languages
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
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
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)
Python: • no standard• handles variable types dynamically• no limit to range of numbers• excellent file handling
How different is the syntax of languages?See: http://en.wikibooks.org/wiki/Computer_Programming/Hello_world
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
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)
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”;
See more examples at: http://en.wikipedia.org/wiki/Comparison_of_programming_languages
Scope of Variables: depends on programming languageMany languages have local and global variables, depending on the declarationFortran has local variables and common blocks
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’
If (expression) then statement
If (expression1) then statements 1elseif (expression2) then statements 2...elseif (expression n) then statements n else statements
switch (variable) case 1 do something ... case n do something
Loops:While (expression) do somethingFor start,condition,increment do something
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
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, ...)
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