programming language concepts control flow
play

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


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

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

  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

  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

  5. Expression Evaluation Sequential execution–expression evaluation: Janyl Jumadinova Programming Language Concepts Control Flow 8 October, 2020 4 / 13

  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

  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

  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

  9. Assignment Operators in Java and C The assignment operator “=” produces a value, just like other operators. The value of the expression “i = 10” is 10. Janyl Jumadinova Programming Language Concepts Control Flow 8 October, 2020 6 / 13

  10. Assignment Operators in Java and C The assignment operator “=” produces a value, just like other operators. 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

  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

  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

  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

  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

  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

  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

  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

  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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend