<<

Concepts

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, 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 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 Referencing/dereferencing operators (C): &, ∗, →

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 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 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

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’ 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.

Janyl Jumadinova Programming Language Concepts Control Flow 13-15 October, 2020 17 / 25 More generally, if (valid(data) && meets criteria(data)) ... It is more efficient than evaluating both operands and then performing an “and” or an “or” on them.

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 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. More generally, if (valid(data) && meets criteria(data)) ... It is more efficient than evaluating both operands and then performing an “and” or an “or” on them.

Janyl Jumadinova Programming Language Concepts Control Flow 13-15 October, 2020 18 / 25 Languages like Ada provide for both full evaluation of all operands and also short-circuit operations:

if (a and b) : full evaluation of both a and b if (a and then b) : short-circuit--quit if a is false

Short Circuit Evaluation

What if, for some reason, we WANT both operands to be evaluated?

Janyl Jumadinova Programming Language Concepts Control Flow 13-15 October, 2020 19 / 25 Short Circuit Evaluation

What if, for some reason, we WANT both operands to be evaluated? Languages like Ada provide for both full evaluation of all operands and also short-circuit operations:

if (a and b) : full evaluation of both a and b if (a and then b) : short-circuit--quit if a is false

Janyl Jumadinova Programming Language Concepts Control Flow 13-15 October, 2020 19 / 25 Old Days

Janyl Jumadinova Programming Language Concepts Control Flow 13-15 October, 2020 20 / 25 The “go to” Statement

“go to” is an UNCONDITIONAL branch. Most early programming languages had “go to” statements. Later languages like C also adopted them. But, they were easy to misuse.

Janyl Jumadinova Programming Language Concepts Control Flow 13-15 October, 2020 21 / 25 The “go to” Statement

Janyl Jumadinova Programming Language Concepts Control Flow 13-15 October, 2020 22 / 25 The “go to” Statement

Janyl Jumadinova Programming Language Concepts Control Flow 13-15 October, 2020 23 / 25 The “go to” Statement

HUGE response. Letter is now famous; many imitations. “” essays appear about almost every topic in science: XMLHttpRequest Considered Harmful Csh Programming Considered Harmful Turing Test Considered Harmful Considered Harmful Essays Considered Harmful ... etc ...

Janyl Jumadinova Programming Language Concepts Control Flow 13-15 October, 2020 24 / 25 The “go to” Statement

But why? We can “break out of ” with a (the for-loop might have its own local variables) We can write incomprehensible code (“”)

IN-CLASS EXERCISE (Oct. 15th): C and goto

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