Programming Language Concepts Control Flow
Total Page:16
File Type:pdf, Size:1020Kb
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 13-15 October, 2020 2 / 25 Conditional operator \?" boolean-expression ? expression1 : expression2 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 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 For example: 5 < 10 ? 70 : −3 is 70, while 5 > 10 ? 70 : −3 is −3 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 while 5 > 10 ? 70 : −3 is −3 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 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 String operators: \Hello" + \world" Referencing/dereferencing operators (C): &; ∗; ! Many Other Operators Bitwise Operators 10j7 = 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 Referencing/dereferencing operators (C): &; ∗; ! Many Other Operators Bitwise Operators 10j7 = 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 Many Other Operators Bitwise Operators 10j7 = 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 Operators in Other Languages Janyl Jumadinova Programming Language Concepts Control Flow 13-15 October, 2020 6 / 25 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 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 COBOL Basics Every variable must be described in the DATA DIVISION. Janyl Jumadinova Programming Language Concepts Control Flow 13-15 October, 2020 8 / 25 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 COBOL Basics Data Types: Numeric Alphanumeric (text/string) Alphabetic Janyl Jumadinova Programming Language Concepts Control Flow 13-15 October, 2020 9 / 25 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. 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 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 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. 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 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 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 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 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 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 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. 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 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. 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 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 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. Short Circuit Evaluation If we switch the ordering: Janyl Jumadinova Programming Language Concepts Control Flow 13-15 October, 2020 17 / 25 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.