1/19/2020

Programs are:

COMPSCI 105/119/120: • Source code written in a text editor, Programming, Flowcharting, and • Following the syntax of a language, Running Program Flowcharts • Specifying both memory locations (variables) and instructions (statements), ©2014‐2019 Dr. William T. Verts • That must be translated into a form the computer can actually use (often binary instructions directly executable by the CPU).

()2016 Dr. William T. Verts

Translators Errors

• Assemblers • Syntax Errors – Translate very low‐level statements into binary – Violations of the rules of the language instructions (11), creating stand‐alone .EXE files. • Compilers • Run‐Time Errors (Bugs) – Translate high‐level statements into many binary – Computations giving the wrong results instructions (1many), creating stand‐alone .EXE – Computations halting the program (unchecked files. divide‐by‐zero, for example) • Interpreters • Both require editing the source text of the – Translates and executes each statement as it is encountered, requiring translator to run programs. program, retranslating it, and trying again.

(C)2016 Dr. William T. Verts (C)2016 Dr. William T. Verts

Languages Flowcharts

• Early compiled languages (FORTRAN, COBOL, • Provide a visual, non‐language‐specific way of ALGOL, PL/I) from the 1950s and 1960s. describing a program, • Later compiled languages (Pascal, C, C++, Ada) from the 1970s and 1980s. • Used to be how programmers designed • Early interpreted languages (BASIC, LISP, APL) programs in the first place, from the 1960s. • Are a good teaching tool to illustrate how • Later interpreted languages (Python, JavaScript, Perl, many scripting languages for Web servers) programs work. • Modern languages compiled to a “generic” computer model, then interpreted by a virtual machine (Java)

(C)2016 Dr. William T. Verts (C)2014 Dr. William T. Verts

1 1/19/2020

Statements (Flowchart) Example: Factorial

• The factorial of an integer N is the product of all integers from 1 up through N. • N factorial is written as N! • N! = 1 ×2 ×3 ×… ×N (iterative definition) • N! = N ×(N‐1)! (recursive definition) • 0! = 1 (makes recursion work) • 5! = 1 ×2 ×3 ×4 ×5 = 120

(C)2017 Dr. William T. Verts (C)2016 Dr. William T. Verts

Flowcharts

• Here's the flowchart version of the factorial program:

(C)2017 Dr. William T. Verts ©2014 Dr. William T. Verts

Tracing Flowcharts

• Put your finger on the START box, • Follow the flow‐arrows, • When you enter a box do what it says, • Update the variables appropriately, • Don't take your finger off until you hit STOP.

(C)2014 Dr. William T. Verts ©2014 Dr. William T. Verts

2 1/19/2020

(C)2014 Dr. William T. Verts (C)2014 Dr. William T. Verts

(C)2014 Dr. William T. Verts (C)2014 Dr. William T. Verts

(C)2014 Dr. William T. Verts (C)2014 Dr. William T. Verts

3 1/19/2020

(C)2014 Dr. William T. Verts (C)2014 Dr. William T. Verts

(C)2014 Dr. William T. Verts (C)2014 Dr. William T. Verts

(C)2014 Dr. William T. Verts (C)2014 Dr. William T. Verts

4 1/19/2020

(C)2014 Dr. William T. Verts (C)2014 Dr. William T. Verts

(C)2014 Dr. William T. Verts (C)2014 Dr. William T. Verts

(C)2014 Dr. William T. Verts (C)2014 Dr. William T. Verts

5 1/19/2020

(C)2014 Dr. William T. Verts (C)2014 Dr. William T. Verts

(C)2014 Dr. William T. Verts (C)2014 Dr. William T. Verts

Programs may be written in Different What Does This Give Us? Ways: • By following a flowchart, we see how • Some are shorter computers execute their programs, • Some are faster • We also see how detailed programs must be • Some use less memory to accomplish any task, • Some use bizarre techniques • But computers do each step extremely fast (on • Some are easier to teach the order of a few nanoseconds). • Some are easier to debug • Some languages are easier than others

(C)2014 Dr. William T. Verts (C)2014 Dr. William T. Verts

6 1/19/2020

Here's the same program in JavaScript The Factorial Program in Python 3 (embedded in HTML Web Page)

(C)2014 Dr. William T. Verts (C)2017 Dr. William T. Verts

Here's the same program in Pascal Here's the same program in BASIC

Program Factorial ; 10 INPUT N Var N,F,I : Integer ; Begin 20 LET F = 1 Write ('Enter Number: ') ; 30 LET I = 1 Readln(N) ; F := 1 ; 40 IF I > N THEN 80 I := 1 ; 50 LET F = F * I While (I <= N) Do Begin 60 LET I = I + 1 F := F * I ; 70 GOTO 40 I := I + 1 ; End ; 80 PRINT F Writeln (F) ; 90 END End.

(C)2014 Dr. William T. Verts (C)2014 Dr. William T. Verts

Here's the same program in 8088 Languages (1/3) Assembly Language MOV AX,1 ; F=1 • Python: MOV BX,5 ; N=5 – Interpreted, MOV CX,1 ; I=1 – Dynamically Typed, TopLoop: CMP CX,BX ; Test I:N – One statement per line, JG EndLoop ; Jump if > – Whitespace (indentation) determines lexical scope. MUL CX ; F=F*I • JavaScript (not Java): ADD CX,1 ; I=I+1 – Interpreted (typically by Web browser), JMP TopLoop ; Jump back – Dynamically Typed, EndLoop: CALL PRINT ; – Statements terminated by semicolons (;), – Curly braces ({}) determine lexical scope.

(C)2014 Dr. William T. Verts (C)2017 Dr. William T. Verts

7 1/19/2020

Languages (2/3) Languages (3/3)

• Pascal: • Java (not JavaScript): – Compiled, – Compiled to intermediate form interpreted by JVM, – Statically Typed, – Statically Typed, – Statements separated by semicolons (;) – Statements terminated by semicolons (;) – Keywords (Begin-End) determine lexical scope. – Curly braces ({}) determine lexical scope. • BASIC (as originally implemented): • Assembly Language: – Interpreted, – Assembled (for particular machine architecture), – Statically Typed (suffixes carry type: A, A$, A%), – Instructions carry type (ADD vs. FADD), – One statement per line, – One statement per line, – What lexical scope? – What lexical scope?

(C)2017 Dr. William T. Verts (C)2017 Dr. William T. Verts

The Factorial Program in Python 3 Here's a more efficient way Again N = int(input("Enter Number: ")) N = int(input("Enter Number: ")) F = 1 F = 1 I = 1 for I in range(1,N+1): F = F * I while (I <= N): print (F) F = F * I I = I + 1 print (F)

(C)2014 Dr. William T. Verts (C)2014 Dr. William T. Verts

Here's a radically different way: On the 105 Final Exam… def Factorial(N): • I will provide a flowchart of roughly this if (N <= 1): return 1 complexity (but not the same program), else return N*Factorial(N-1) • I will provide boxes for each of the variables, • You will trace the flowchart and determine the N = int(input("Enter Number: ")) final results. • print (Factorial(N)) I will NOT ask you to draw a flowchart.

(C)2014 Dr. William T. Verts (C)2017 Dr. William T. Verts

8