UNCA CSCI 431 - Control Flow: Expressions, Sequencing & Selection

Unformatted text preview:

Control Flow: Expressions, Sequencing & Selection (Section 6.1- 6.4)Control FlowControl Flow MechanismsExpression EvaluationOperatorsOperators Precedence and AssociativityOperator Precedence Precedence TableSlide 8Operators AssociativityAssignmentVariablesSlide 12Expressions Other IssuesSlide 14Slide 15Short-Circuited ConditionsExpressions ExamplesUnstructured Flow The GOTO StatementSlide 19Structured FlowStructured Flow Special CasesSequencingSelectionSelection Efficient Case/Switch ExampleSlide 25Reading11Control Flow: Expressions, Control Flow: Expressions, Sequencing & SelectionSequencing & Selection(Section 6.1- 6.4)(Section 6.1- 6.4)A compilation of material developed by Felix A compilation of material developed by Felix Hernandez-Campos and Michael ScottHernandez-Campos and Michael ScottCSCI 431 Programming LanguagesCSCI 431 Programming LanguagesFall 2003Fall 200322Control FlowControl Flow•Control flow refers to the Control flow refers to the order in which a program order in which a program executesexecutes•This is fundamental in the imperative programming This is fundamental in the imperative programming paradigm paradigm –E.g.E.g. Java, C++, Pascal, Fortran, Ada, etc. Java, C++, Pascal, Fortran, Ada, etc.•In other programming paradigms, the compilers or In other programming paradigms, the compilers or the interpreters take care of the orderingthe interpreters take care of the ordering–E.g.E.g. logic programming logic programming33Control Flow MechanismsControl Flow Mechanisms•SequencingSequencing–Textual order, precedence and associativity in expressionTextual order, precedence and associativity in expression•SelectionSelection•IterationIteration•Procedural abstractionProcedural abstraction•RecursionRecursion•ConcurrencyConcurrency•NondeterminacyNondeterminacy44Expression EvaluationExpression Evaluation•Expressions consist of Expressions consist of operandsoperands ( (e.g.e.g. a variable) and a variable) and operatorsoperators or functions ( or functions (e.g.e.g. +, abs()) +, abs())–By definition, operators and functions return a valueBy definition, operators and functions return a value•Operators are also functionsOperators are also functions–Infix notation is just Infix notation is just syntactic sugarsyntactic sugar–In C++, a + b means a.operator+ (b)In C++, a + b means a.operator+ (b)55OperatorsOperators•Operators are used inOperators are used in–Prefix notationPrefix notation»E.g.E.g. Expression Expression (* (+ 1 3) 2)(* (+ 1 3) 2) in Lisp in Lisp–Infix notationInfix notation»E.g.E.g. Expression Expression (1 + 3) * 2(1 + 3) * 2 in Java in Java –Postfix notationPostfix notation»E.g.E.g. Increment Increment a++a++ in C in C•Operators can have 1 or more operandsOperators can have 1 or more operands–Increment in C is a one-operand operator: Increment in C is a one-operand operator: a++a++–Subtraction in C is a two-operand operator: Subtraction in C is a two-operand operator: a-ba-b–Conditional expression in C is a three-operand operators: Conditional expression in C is a three-operand operators: (a == 3 ? 0 : 1)(a == 3 ? 0 : 1)66OperatorsOperatorsPrecedence and AssociativityPrecedence and Associativity•Precedence and associativity deal with the evaluation Precedence and associativity deal with the evaluation order within expressionsorder within expressions•PrecedencePrecedence rules specify the order in which rules specify the order in which operators of different precedence level are evaluatedoperators of different precedence level are evaluated–* usually groups * usually groups more tightlymore tightly than + than +•What is the results of What is the results of 4 * 5 ** 6 4 * 5 ** 6 ??77Operator PrecedenceOperator PrecedencePrecedence TablePrecedence Table88Operator PrecedenceOperator PrecedencePrecedence TablePrecedence Table99OperatorsOperatorsAssociativityAssociativity•AssociativityAssociativity rules specify the order in which rules specify the order in which operators of the operators of the same precedencesame precedence level are evaluated level are evaluated–+ is usually evaluated from left-to-right+ is usually evaluated from left-to-right•What is the results of What is the results of 4 ** 5 ** 6 4 ** 5 ** 6 ??•In Fortran, ** associates from right-to-left, as in In Fortran, ** associates from right-to-left, as in MathMath•In Ada, ** does not associate, so you have to write In Ada, ** does not associate, so you have to write the previous expression as the previous expression as 4 ** (5 ** 6)4 ** (5 ** 6) to obtain to obtain the expected answerthe expected answer1010AssignmentAssignment•The basic operation in imperative language is The basic operation in imperative language is assignmentassignment–The The side effectside effect of this operation is a change in memory of this operation is a change in memory–Assignments affect the whole state of the programAssignments affect the whole state of the program•Purely functional language do not have assignmentPurely functional language do not have assignment–Side effects are not possibleSide effects are not possible–Expression in purely functional languages depend only in Expression in purely functional languages depend only in their referencing environmenttheir referencing environment•ExpressionsExpressions produce values produce values•StatementsStatements do not return values, but they have side do not return values, but they have side effectseffects1111VariablesVariables•Value modelValue model of variables of variables–E.g.E.g. Pascal’s A := 3 Pascal’s A := 3–A is an A is an l-valuel-value, and it denotes a position in memory, and it denotes a position in memory–3 is a 3 is a r-valuer-value, and it denotes a value with no explicit , and it denotes a value with no explicit locationlocation•Reference modelReference model of variables of variables–E.g.E.g. Java’s A = new Integer(3) Java’s A = new Integer(3)–Variables are Variables are referencesreferences to values to values1212VariablesVariables•Value and Reference model of variablesValue and Reference model of variables•Variables in JavaVariables in Java–http://java.sun.com/docs/books/tutorial/java/nutsandbolts/varhttp://java.sun.com/docs/books/tutorial/java/nutsandbolts/variables.htmliables.html1313ExpressionsExpressionsOther IssuesOther Issues•InitializationInitialization may be implicit or explicit (at may be implicit or


View Full Document

UNCA CSCI 431 - Control Flow: Expressions, Sequencing & Selection

Download Control Flow: Expressions, Sequencing & Selection
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Control Flow: Expressions, Sequencing & Selection and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Control Flow: Expressions, Sequencing & Selection 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?