Programming Language Concepts Control Flow Janyl Jumadinova 8 - - PowerPoint PPT Presentation

programming language concepts control flow
SMART_READER_LITE
LIVE PREVIEW

Programming Language Concepts Control Flow Janyl Jumadinova 8 - - PowerPoint PPT Presentation

Programming Language Concepts Control Flow Janyl Jumadinova 8 October, 2020 Janyl Jumadinova Programming Language Concepts Control Flow 8 October, 2020 1 / 13 Expression Evaluation Answer Poll at itempool.com/jjumadinova/live Janyl


slide-1
SLIDE 1

Programming Language Concepts Control Flow

Janyl Jumadinova 8 October, 2020

Janyl Jumadinova Programming Language Concepts Control Flow 8 October, 2020 1 / 13

slide-2
SLIDE 2

Expression Evaluation

Answer Poll at itempool.com/jjumadinova/live

Janyl Jumadinova Programming Language Concepts Control Flow 8 October, 2020 2 / 13

slide-3
SLIDE 3

Expression Evaluation

What is the value of the variable i after executing the following code (in either Java or C)? i = 10; i = i++;

Janyl Jumadinova Programming Language Concepts Control Flow 8 October, 2020 3 / 13

slide-4
SLIDE 4

Expression Evaluation

What is the value of the variable i after executing the following code (in either Java or C)? i = 10; i = i++; Same Question: i = 10; i = ++i;

Janyl Jumadinova Programming Language Concepts Control Flow 8 October, 2020 3 / 13

slide-5
SLIDE 5

Expression Evaluation

Sequential execution–expression evaluation:

Janyl Jumadinova Programming Language Concepts Control Flow 8 October, 2020 4 / 13

slide-6
SLIDE 6

Expression Evaluation

int i=6, j=3, k=2; int m = i/j*k; What is the value of m?

Janyl Jumadinova Programming Language Concepts Control Flow 8 October, 2020 5 / 13

slide-7
SLIDE 7

Expression Evaluation

int i=6, j=3, k=2; int m = i/j*k; What is the value of m? So, since 6/6 = 1, m = 1, right?

Janyl Jumadinova Programming Language Concepts Control Flow 8 October, 2020 5 / 13

slide-8
SLIDE 8

Expression Evaluation

int i=6, j=3, k=2; int m = i/j*k; What is the value of m? So, since 6/6 = 1, m = 1, right? Problem: / and * have equal precedence and they are left-associative: i/j*k = (i/j)*k = (6/3)*2 = 4.

Janyl Jumadinova Programming Language Concepts Control Flow 8 October, 2020 5 / 13

slide-9
SLIDE 9

Assignment Operators in Java and C

The assignment operator “=” produces a value, just like other

  • perators.

The value of the expression “i = 10” is 10.

Janyl Jumadinova Programming Language Concepts Control Flow 8 October, 2020 6 / 13

slide-10
SLIDE 10

Assignment Operators in Java and C

The assignment operator “=” produces a value, just like other

  • perators.

The value of the expression “i = 10” is 10. This is a right-associative operator:

“i = j = k = 10” means “i = (j = (k = 10))” and has the effect of setting all three variables to the same value, 10. In C, this can cause serious program bugs (e.g., a = b = c to be interpreted as a = (b = c))!

Janyl Jumadinova Programming Language Concepts Control Flow 8 October, 2020 6 / 13

slide-11
SLIDE 11

Assignment Operator in C

The following is legal in C: i = 0; if (i = 10) printf("i is 10"); The effect is to assign 10 to the variable i, then see if the resulting value (namely 10) is non-zero (in C, non-zero values represent “true”). This will always evaluate to true!

Janyl Jumadinova Programming Language Concepts Control Flow 8 October, 2020 7 / 13

slide-12
SLIDE 12

Assignment Operator in C

The following is legal in C: i = 0; if (i = 10) printf("i is 10"); The effect is to assign 10 to the variable i, then see if the resulting value (namely 10) is non-zero (in C, non-zero values represent “true”). This will always evaluate to true! The programmer probably meant to write: if (i == 10) printf(‘‘i is 10’’);

Janyl Jumadinova Programming Language Concepts Control Flow 8 October, 2020 7 / 13

slide-13
SLIDE 13

Assignments and Expressions

Some programming languages use the idea of l-values and r-values. In an assignment statement such as: i = i+1 the variable i plays two different roles. On the left, it stands for a memory location, or reference, called an “l-value”.

An l-value refers to an object that persists beyond a single expression.

On the right, it represents a value (“r-value”).

An r-value is a temporary value that does not persist beyond the expression that uses it.

Janyl Jumadinova Programming Language Concepts Control Flow 8 October, 2020 8 / 13

slide-14
SLIDE 14

Assignment Statements in Functional Languages

In a functional language, there are no variables, hence no assignment statements. A program in a functional language is just a collection of expressions to be evaluated. More on functional languages in a few weeks.

Janyl Jumadinova Programming Language Concepts Control Flow 8 October, 2020 9 / 13

slide-15
SLIDE 15

Initialization

Some languages allow a variable to be initialized at the same time it is declared: int i = 10 Not all languages check to see if a variable is initialized: int main() { /* C example */ int i,j,k; printf("i,j,k = %d %d %d \n",i,j,k); } Output: i,j,k = 32767 1740734558 32767

Janyl Jumadinova Programming Language Concepts Control Flow 8 October, 2020 10 / 13

slide-16
SLIDE 16

Initialization

Compilers can often determine whether a variable is initialized at the point where it is used as an r-value, e.g., Java ... int i; int j = i; ... javac Init.java Init.java:12: error: variable i might not have been initialized

Janyl Jumadinova Programming Language Concepts Control Flow 8 October, 2020 11 / 13

slide-17
SLIDE 17

Automatic Initialization

In some languages, (some) variables are automatically initialized to a default value. Example: Java initializes all instance variables (e.g., ints and doubles are zero, objects are null, etc.), but not local variables

Janyl Jumadinova Programming Language Concepts Control Flow 8 October, 2020 12 / 13

slide-18
SLIDE 18

COBOL Activity

Common Business Oriented Language. Divisions - Identification, Environment, Data, Procedure. Identification division

Entries → Clauses

Environment division

Sections → Paragraphs → Entries → Clauses, Phrases

Data division

Sections → Entries → Clauses → Phrases

Procedure division

Sections → Paragraphs → Sentences → Statements → Phrases

Janyl Jumadinova Programming Language Concepts Control Flow 8 October, 2020 13 / 13