Docsity

Variables, Structured Flow, Sequencing and Selection - Slides | COMP 524, Study notes of Programming Languages

Partial preview of the text

Download Variables, Structured Flow, Sequencing and Selection - Slides | COMP 524 and more Study notes Programming Languages in PDF only on Docsity!

Lecture 9: Variables, Structured Flow, Sequencing

and Selection

COMP 524 Programming Language Concepts Aaron Block February 15, 2007 Based on notes by N. Fisher, F. Hernandez-Campos, and D. Stotts

Define Context Thesis Outline:P1, P

Intro

P

Intro P1 & Thesis Outline of P1 Proof (optional) Fact Analysis Repeat as necessary

P

Summarize Arguments

Conclusion

Impact of Thesis (optional) Intro P2 & Thesis

Control Flow

  • Control flow is the order in which a program executes.
  • For imperative languages (e.g., Java), this is

fundamental.

  • For other programing paradigms (e.g., functional), the

compilers/interpreters take care of ordering.

4

Expression Evaluation

  • An expressions consist of a simple object (e.g., a

variable), an operator , or a function applied to a

collection of objects and/or operators.

  • Expression evaluation is a crucial component of

functional languages.

5

Expression Evaluation

• An expressions consist of a simple object (e.g., a

variable), an operator , or a function applied to a

collection of objects and/or operators.

• Expression evaluation is a crucial component of

functional languages.

7

Functional languages are very “math-like”

and in math the most important concept is

evaluating expressions.

Operators

  • Operators are used in
    • Prefix notation: operators come first - ( (+ 1 3) 2 )*
    • Infix notation: operators in middle - (1+3)*
    • Postfix notation: operators last - a++ 8

Operators--Associativity

  • Associativity rules specify the order in which operators

of the same precedence level are evaluated.

  • Usually they are evaluated “left-to-right”
  • In Fortran, ****** associates from right-to-left
  • x ** y = x^y
  • Thus 234 is read as 2^(3^4) rather than (2^3)^4. 10

Assignment

  • The basic operation language is assignment.
  • An assignment places a value into a specific memory

location.

11 a = 2; a 2036

X

a 2036

Before After

Context

  • To see the difference between context consider the two

following statements.

13 int sum(int n){ int val=0; for(int i=0,i<=n;i++){ val+=i; } return val; } int sum(int n){ if (n<=0) then return 0 else return n+sum(n-1) }

Imperative Functional

Context

• To see the difference between context consider the two

following statements.

14 int sum(int n){ int val=0; for(int i=0,i<=n;i++){ val+=i; } return val; } int sum(int n){ if (n<=0) then return 0 else return n+sum(n-1) }

In the imperative code the

value of val changes within

the context of sum

Imperative Functional

Variables

  • Two ways to model variables:
    • Value model - Reference model 16

Value Model

  • Under the value model variables on the left-hand side

(called l-values ) of equations denote references , and

variables on the right-hand side (called r-values )

denote values.

17 b = 2; a = b; (^) a 2036

X

mem(1024)= 2; mem(2036)= 2; b 1024

a 2036

b 1024

Reference Model

  • Under the reference model variables on both the left-

and right-hand side are references.

19 b = 2; a = b; (^) a 2036

X

mem(1024)= loc(2); mem(2036)= mem(1024); b 1024

a 2036

b 1024

Reference Model

• Under the reference model variables on both the left-

and right-hand side are references.

20 b = 2; a = b; (^) a 2036

X

mem(1024)= loc(2); mem(2036)= mem(1024); b 1024

a 2036

b 1024

Chu uses this model.