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

programming language concepts control flow
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 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

slide-2
SLIDE 2

Operators

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

slide-3
SLIDE 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

slide-4
SLIDE 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

slide-5
SLIDE 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

slide-6
SLIDE 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

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

slide-8
SLIDE 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

slide-9
SLIDE 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

slide-10
SLIDE 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

slide-11
SLIDE 11

Operators in Other Languages

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

slide-12
SLIDE 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

slide-13
SLIDE 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

slide-14
SLIDE 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

slide-15
SLIDE 15

COBOL Basics

Data Types: Numeric Alphanumeric (text/string) Alphabetic

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

slide-16
SLIDE 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

slide-17
SLIDE 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

slide-18
SLIDE 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

slide-19
SLIDE 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

slide-20
SLIDE 20

Assignment Statements in COBOL

1 MOVE statement (MOVE 25 TO NUM1 NUM3.) 2 Computation written out as COMPUTE var = var operator var.

  • r by using ADD, DIVIDE, MULTIPLY, SUBTRACT ...

GIVING.

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

slide-21
SLIDE 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

slide-22
SLIDE 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

slide-23
SLIDE 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

slide-24
SLIDE 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

slide-25
SLIDE 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

slide-26
SLIDE 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

slide-27
SLIDE 27

Short Circuit Evaluation

If we switch the ordering:

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

slide-28
SLIDE 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

slide-29
SLIDE 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

slide-30
SLIDE 30

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”

  • n them.

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

slide-31
SLIDE 31

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

slide-32
SLIDE 32

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

slide-33
SLIDE 33

Old FORTRAN Days

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

slide-34
SLIDE 34

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

slide-35
SLIDE 35

The “go to” Statement

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

slide-36
SLIDE 36

The “go to” Statement

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

slide-37
SLIDE 37

The “go to” Statement

HUGE response. Letter is now famous; many imitations. “Considered harmful” essays appear about almost every topic in computer 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

slide-38
SLIDE 38

The “go to” Statement

But why? We can “break out of scope” with a goto (the for-loop block might have its own local variables) We can write incomprehensible code (“spaghetti code”) IN-CLASS EXERCISE (Oct. 15th): C and goto

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