programming language concepts control flow
play

Programming Language Concepts Control Flow Janyl Jumadinova 13-15 - PowerPoint PPT Presentation

Programming Language Concepts Control Flow Janyl Jumadinova 13-15 October, 2020 Janyl Jumadinova Programming Language Concepts Control Flow 13-15 October, 2020 1 / 25 Operators Janyl Jumadinova Programming Language Concepts Control Flow


  1. Programming Language Concepts Control Flow Janyl Jumadinova 13-15 October, 2020 Janyl Jumadinova Programming Language Concepts Control Flow 13-15 October, 2020 1 / 25

  2. Operators Janyl Jumadinova Programming Language Concepts Control Flow 13-15 October, 2020 2 / 25

  3. A Very Unusual Operator: ? Most operators are either binary (+ , − , ∗ , <, == , && , etc . ) or unary (“plus sign” +, “minus sign” -, ++, !, etc.). However, C and Java also have a ternary operator (takes 3 arguments). Janyl Jumadinova Programming Language Concepts Control Flow 13-15 October, 2020 3 / 25

  4. A Very Unusual Operator: ? Most operators are either binary (+ , − , ∗ , <, == , && , etc . ) or unary (“plus sign” +, “minus sign” -, ++, !, etc.). However, C and Java also have a ternary operator (takes 3 arguments). Conditional operator “?” boolean-expression ? expression1 : expression2 Janyl Jumadinova Programming Language Concepts Control Flow 13-15 October, 2020 3 / 25

  5. A Very Unusual Operator: ? Conditional operator “?” boolean-expression ? expression1 : expression2 The boolean-expression is evaluated. If it is true, the value is expression1 , otherwise it is expression2 . Janyl Jumadinova Programming Language Concepts Control Flow 13-15 October, 2020 4 / 25

  6. A Very Unusual Operator: ? Conditional operator “?” boolean-expression ? expression1 : expression2 The boolean-expression is evaluated. If it is true, the value is expression1 , otherwise it is expression2 . For example: 5 < 10 ? 70 : − 3 is 70, Janyl Jumadinova Programming Language Concepts Control Flow 13-15 October, 2020 4 / 25

  7. A Very Unusual Operator: ? Conditional operator “?” boolean-expression ? expression1 : expression2 The boolean-expression is evaluated. If it is true, the value is expression1 , otherwise it is expression2 . For example: 5 < 10 ? 70 : − 3 is 70, while 5 > 10 ? 70 : − 3 is − 3 Janyl Jumadinova Programming Language Concepts Control Flow 13-15 October, 2020 4 / 25

  8. Many Other Operators Bitwise Operators 10 | 7 = 15 (bitwise “or”) 10&7 = 2 (bitwise “and”) 10 << 3 = 80 (left shift) 10 >> 1 = 5 (right shift) Janyl Jumadinova Programming Language Concepts Control Flow 13-15 October, 2020 5 / 25

  9. Many Other Operators Bitwise Operators 10 | 7 = 15 (bitwise “or”) 10&7 = 2 (bitwise “and”) 10 << 3 = 80 (left shift) 10 >> 1 = 5 (right shift) String operators: “Hello” + “world” Janyl Jumadinova Programming Language Concepts Control Flow 13-15 October, 2020 5 / 25

  10. Many Other Operators Bitwise Operators 10 | 7 = 15 (bitwise “or”) 10&7 = 2 (bitwise “and”) 10 << 3 = 80 (left shift) 10 >> 1 = 5 (right shift) String operators: “Hello” + “world” Referencing/dereferencing operators (C): & , ∗ , → Janyl Jumadinova Programming Language Concepts Control Flow 13-15 October, 2020 5 / 25

  11. Operators in Other Languages Janyl Jumadinova Programming Language Concepts Control Flow 13-15 October, 2020 6 / 25

  12. Operators in Other Languages COBOL Operators: Arithmetic Logical (AND, OR, NOT) Relational (IS [NOT] LIKE, IS LESS THAN ..) Janyl Jumadinova Programming Language Concepts Control Flow 13-15 October, 2020 7 / 25

  13. COBOL Basics Every variable must be described in the DATA DIVISION. Janyl Jumadinova Programming Language Concepts Control Flow 13-15 October, 2020 8 / 25

  14. COBOL Basics Every variable must be described in the DATA DIVISION. Data Categories: Variables Literals: String/Alphanumeric Literals and Numeric Literals Figurative Constants SPACE or SPACES - Acts like one or more spaces ZERO or ZEROS or ZEROES - Acts like one or more zeros QUOTE or QUOTES - Used instead of a quotation mark HIGH-VALUE or HIGH-VALUES - Uses the maximum value possible LOW-VALUE or LOW-VALUES - Uses the minimum value possible ALL literal - Allows a ordinary literal to act as Figurative Constant Janyl Jumadinova Programming Language Concepts Control Flow 13-15 October, 2020 8 / 25

  15. COBOL Basics Data Types: Numeric Alphanumeric (text/string) Alphabetic Janyl Jumadinova Programming Language Concepts Control Flow 13-15 October, 2020 9 / 25

  16. Declaring Data-Items in COBOL A variable (elementary item) declaration consists of a line in the DATA DIVISION that contains the following: A level number A data-name or identifier. A Picture clause. Janyl Jumadinova Programming Language Concepts Control Flow 13-15 October, 2020 10 / 25

  17. Declaring Data-Items in COBOL A variable (elementary item) declaration consists of a line in the DATA DIVISION that contains the following: A level number A data-name or identifier. A Picture clause. 9 Indicates the occurrence of a digit. X Indicates the occurrence of any character from the character set. A Indicates the occurrence of any alphabetic character (A to Z plus blank). V Indicates the position of the decimal point in a numeric value. S Indicates the presence of a sign and can only appear at the beginning of PIC. Janyl Jumadinova Programming Language Concepts Control Flow 13-15 October, 2020 10 / 25

  18. Declaring Data-Items in COBOL It maybe convenient to treat a collection of elementary items as a single group – (e.g., group YearofBirth , MonthofBirth , DayOfBirth under the group name - DateOfBirth ). Janyl Jumadinova Programming Language Concepts Control Flow 13-15 October, 2020 11 / 25

  19. Declaring Data-Items in COBOL It maybe convenient to treat a collection of elementary items as a single group – (e.g., group YearofBirth , MonthofBirth , DayOfBirth under the group name - DateOfBirth ). Group items are declared using a level number and a data name only. Hierarchical relationship between the various subordinate items of the group is expressed using level numbers. The higher the level number, the lower the item is in the hierarchy. Janyl Jumadinova Programming Language Concepts Control Flow 13-15 October, 2020 11 / 25

  20. Assignment Statements in COBOL 1 MOVE statement ( MOVE 25 TO NUM1 NUM3. ) 2 Computation written out as COMPUTE var = var operator var. or by using ADD, DIVIDE, MULTIPLY, SUBTRACT ... GIVING . Janyl Jumadinova Programming Language Concepts Control Flow 13-15 October, 2020 12 / 25

  21. Conditional Branches Familiar to most novice programmers: “if” and “if-else” statements “switch” statements Basic idea : if (condition) then ... else ... It wasn’t always quite this easy, though Janyl Jumadinova Programming Language Concepts Control Flow 13-15 October, 2020 13 / 25

  22. Conditional branches–switch statements In C and Java: switch(i) { case 0: case 2: case 4: System.out.println(i+": even, <= 4"); break; case 1: System.out.println(i+" is one"); break; default: } Janyl Jumadinova Programming Language Concepts Control Flow 13-15 October, 2020 14 / 25

  23. Conditional branches–switch statements Without break statements? i=0; switch(i) { case 0: case 2: case 4: System.out.println(i+": even, <= 4"); case 1: System.out.println(i+" is one"); default: System.out.println(i+": odd or > 4"); } Janyl Jumadinova Programming Language Concepts Control Flow 13-15 October, 2020 15 / 25

  24. Short Circuit Evaluation According to the laws of logic, order doesn’t matter in “and”: “p AND q” is the same as “q AND p”. Similarly, for OR. Janyl Jumadinova Programming Language Concepts Control Flow 13-15 October, 2020 16 / 25

  25. Short Circuit Evaluation According to the laws of logic, order doesn’t matter in “and”: “p AND q” is the same as “q AND p”. Similarly, for OR. But in Java and C, order of evaluation is important: Janyl Jumadinova Programming Language Concepts Control Flow 13-15 October, 2020 16 / 25

  26. Short Circuit Evaluation According to the laws of logic, order doesn’t matter in “and”: “p AND q” is the same as “q AND p”. Similarly, for OR. But in Java and C, order of evaluation is important: int i = 10, j = 0, k = 0; if (i > 10 && 5/j < 3) { k = 5; } Since i > 10 is false, there is no need to look at the second condition–we already know that the “&&” will be false. Janyl Jumadinova Programming Language Concepts Control Flow 13-15 October, 2020 16 / 25

  27. Short Circuit Evaluation If we switch the ordering: Janyl Jumadinova Programming Language Concepts Control Flow 13-15 October, 2020 17 / 25

  28. Short Circuit Evaluation If we switch the ordering: int i = 10, j = 0, k = 0; if ( 5/j < 3 && i > 10 ) { k = 5; } If we start with 5 / j < 3, we”ll get a “division by zero” error. Janyl Jumadinova Programming Language Concepts Control Flow 13-15 October, 2020 17 / 25

  29. Short Circuit Evaluation Short circuit evaluation is used often in situations like this: if (i >= 0 && sqrt(i) > 5.0) ... By checking i > = 0 first, we guarantee that we won’t try taking square root of a negative value. Janyl Jumadinova Programming Language Concepts Control Flow 13-15 October, 2020 18 / 25

Recommend


More recommend