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

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

  • Upload
    others

  • View
    34

  • Download
    0

Embed Size (px)

Citation preview

  • Programming Language ConceptsControl 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

  • A Very Unusual Operator: ?

    Most operators are either binary (+,−, ∗,

  • A Very Unusual Operator: ?

    Most operators are either binary (+,−, ∗,

  • 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

  • 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

  • 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

  • Many Other Operators

    Bitwise Operators

    10|7 = 15 (bitwise “or”)10&7 = 2 (bitwise “and”)

    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

  • Many Other Operators

    Bitwise Operators

    10|7 = 15 (bitwise “or”)10&7 = 2 (bitwise “and”)

    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

  • Many Other Operators

    Bitwise Operators

    10|7 = 15 (bitwise “or”)10&7 = 2 (bitwise “and”)

    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

  • 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

    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

  • Declaring Data-Items in COBOL

    A variable (elementary item) declaration consists of a line in the DATADIVISION 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

  • Declaring Data-Items in COBOL

    A variable (elementary item) declaration consists of a line in the DATADIVISION 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

  • Declaring Data-Items in COBOL

    It maybe convenient to treat a collection of elementary items as a singlegroup– (e.g., group YearofBirth, MonthofBirth, DayOfBirth under thegroup name - DateOfBirth).

    Group items are declared using a level number and a data name only.

    Hierarchical relationship between the various subordinate items of thegroup 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

  • Declaring Data-Items in COBOL

    It maybe convenient to treat a collection of elementary items as a singlegroup– (e.g., group YearofBirth, MonthofBirth, DayOfBirth under thegroup name - DateOfBirth).

    Group items are declared using a level number and a data name only.

    Hierarchical relationship between the various subordinate items of thegroup 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,

  • Conditional branches–switch statements

    Without break statements?

    i=0;

    switch(i) {

    case 0:

    case 2:

    case 4: System.out.println(i+": even, 4");

    }

    Janyl Jumadinova Programming Language Concepts Control Flow 13-15 October, 2020 15 / 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–wealready know that the “&&” will be false.

    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–wealready know that the “&&” will be false.

    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–wealready know that the “&&” will be false.

    Janyl Jumadinova Programming Language Concepts Control Flow 13-15 October, 2020 16 / 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

  • 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

  • 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 squareroot of a negative value.

    More generally,

    if (valid(data) && meets criteria(data)) ... It is more efficientthan 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

  • 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 squareroot of a negative value.

    More generally,

    if (valid(data) && meets criteria(data)) ... It is more efficientthan 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

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

  • 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 operandsand 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 FORTRAN 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. “Consideredharmful” 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

  • The “go to” Statement

    But why?

    We can “break out of scope” with a goto (the for-loop block mighthave 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