Upload
others
View
18
Download
0
Embed Size (px)
Citation preview
Programming Language ConceptsControl Flow
Janyl Jumadinova
8 October, 2020
Janyl Jumadinova Programming Language Concepts Control Flow 8 October, 2020 1 / 13
Expression Evaluation
Answer Poll atitempool.com/jjumadinova/live
Janyl Jumadinova Programming Language Concepts Control Flow 8 October, 2020 2 / 13
itempool.com/jjumadinova/live
Expression Evaluation
What is the value of the variable i after executing the following code (ineither 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
Expression Evaluation
What is the value of the variable i after executing the following code (ineither 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
Expression Evaluation
Sequential execution–expression evaluation:
Janyl Jumadinova Programming Language Concepts Control Flow 8 October, 2020 4 / 13
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
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
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
Assignment Operators in Java and C
The assignment operator “=” produces a value, just like otheroperators.
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 ofsetting all three variables to the same value, 10.In C, this can cause serious program bugs (e.g., a = b = c to beinterpreted as a = (b = c))!
Janyl Jumadinova Programming Language Concepts Control Flow 8 October, 2020 6 / 13
Assignment Operators in Java and C
The assignment operator “=” produces a value, just like otheroperators.
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 ofsetting all three variables to the same value, 10.In C, this can cause serious program bugs (e.g., a = b = c to beinterpreted as a = (b = c))!
Janyl Jumadinova Programming Language Concepts Control Flow 8 October, 2020 6 / 13
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 willalways evaluate to true!
The programmer probably meant to write: if (i == 10) printf(‘‘iis 10’’);
Janyl Jumadinova Programming Language Concepts Control Flow 8 October, 2020 7 / 13
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 willalways evaluate to true!
The programmer probably meant to write: if (i == 10) printf(‘‘iis 10’’);
Janyl Jumadinova Programming Language Concepts Control Flow 8 October, 2020 7 / 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 theexpression that uses it.
Janyl Jumadinova Programming Language Concepts Control Flow 8 October, 2020 8 / 13
Assignment Statements in Functional Languages
In a functional language, there are no variables, hence no assignmentstatements.
A program in a functional language is just a collection of expressionsto be evaluated.
More on functional languages in a few weeks.
Janyl Jumadinova Programming Language Concepts Control Flow 8 October, 2020 9 / 13
Initialization
Some languages allow a variable to be initialized at the same time itis 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
Initialization
Compilers can often determine whether a variable is initialized at thepoint 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
Automatic Initialization
In some languages, (some) variables are automatically initialized to adefault value.
Example: Java initializes all instance variables (e.g., ints and doublesare zero, objects are null, etc.), but not local variables
Janyl Jumadinova Programming Language Concepts Control Flow 8 October, 2020 12 / 13
COBOL Activity
Common Business Oriented Language.Divisions - Identification, Environment, Data, Procedure.
Identification division
Entries → ClausesEnvironment division
Sections → Paragraphs → Entries → Clauses, PhrasesData division
Sections → Entries → Clauses → PhrasesProcedure division
Sections → Paragraphs → Sentences → Statements → Phrases
Janyl Jumadinova Programming Language Concepts Control Flow 8 October, 2020 13 / 13