









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An overview of various control structures in programming, including expressions and statements, relational and boolean expressions, assignments, compound statements, conditional execution, iteration, and routines. It covers operator precedence, associativity, and evaluation rules for expressions, as well as the use of infix, prefix, and postfix notations. The document also discusses the differences between assignment operators, compound assignment operators, and unary assignment operators. Additionally, it covers the use of cloning in python for list initialization and the concept of compound statements and conditional execution.
Typology: Study notes
1 / 16
This page cannot be seen from the preview
Don't miss anything!










To control flow among the different constructs in a program. There are two levels of control: Statement-level control: Organizing statements into patterns of control: o Statements and expressions o Iterations o Conditional statements Unit-level control: Organizing the flow of control between program units: o Routine call and return o Exception handling o Event-driven control
Motivation of the first programming languages: Evaluate expressions. Arithmetic expressions consist of operators, operands, parentheses, and function calls: x = y f(x,y,z)+zt; Design issues for arithmetic expressions: What are the operator precedence rules?
Expression Representations: Expressions can be expressed as trees a+b(c-d)+z/t* The tree is evaluated bottom up Different notations: o Prefix notation o Postfix notation o Infix notation Evaluation of expressions Semantics Prefix notation: o Based on functional notation: f(x, y) o The operator precedes the operands o Unambiguous o Hard to read o Example: +a+b-cd/zt* Postfix Notation: o Same advantages and disadvantages as prefix notation. o Example: abcd-zt/++*
o In C, ++ can be written in either pre- or postfix, and the semantics are different k++ is not the same as ++k. o What are the values of x1 and x2? x1 = k++; x2 = ++k; Infix Notation : o Although infix is used by most programming languages, it gives rise to ambiguities that need to be resolved usually via parenthesis, precedence rules, and associativity. o Example: a+b(c-d)+z/t*
Relational Expressions: Use relational operators and operands of various Types. Evaluate to some Boolean representation Operator symbols used vary somewhat among languages (!=, /=, .NE., <>, #) Boolean Expressions
C, C++, and Java treat = as an arithmetic binary operator, e.g. a = b * (c = d * 2 + 1) + 1 Assignment Using Cloning o Python uses cloning to initialize a list from an existing list o It uses the slice operator o What is slicing? o S licing a list v[i:j] returns a list containing elements from position i to position j-1. o It can also be applied to strings o Note that indices, i and j can be negative, in which case they count from the right instead of the left. o Example:
mylist = ['a', 'b', 'c', 'd', 'e'] mylist0 = mylist[:] #initialize mylist mylist ['a', 'b', 'c', 'd', 'e'] mylist1 = mylist[2:4] mylist ['c', 'd'] mylist2= mylist[0:4] mylist ['a', 'b', 'c', 'd'] id(mylist) #note the address of mylist 13188632 id(mylist1) #note the address of mylist 13226352 id(mylist2) #note the address of mylist 13226832
Most languages use ALGOL-style conditionals Dangling else if x > 0 then if x < 10 then x := 0 else x := 1000; Which if gets the else? Example: Java if ... if ... ... else ... Java's static semantics rule: else goes with the nearest if. Example: Ada if ... then if ... then if ... then if ... then ... ... else end if ... else end if ... end if end if Example Python:
num = 11 if 1<= num <=8: print "in" else: print "out" Advantage: flexibility and readability. Short-circuit Boolean expressions: Java and Ada case statement switch( operator ) { case ‘+’: result = operand1 + operand2; break; case ‘*’: result = operand1 * operand2; break; case ‘-’: result = operand1 - operand2; break; case ‘/’: result = operand1 / operand2; break; default: break; // Do nothing. }; Note that Python does not have a switch statement. Users can use dictionaries and such
What are the type and scope of the loop var? What is the value of the loop var at loop termination? Should it be legal for the loop var or loop parameters to be changed in the loop body, and if so, does the change affect loop control? Should the loop parameters be evaluated only once, or once for every iteration?
BEFORE the loop is executed (while)
AFTER the loop is executed (do...while)
Number of iterations is not known while Condition do Statement Test is at the top of the loop repeat Statement until Condition Test is at the bottom of the loop
C and C++ also have both, but the control expression for the posttest version is treated just like in the pretest case (while - do and do - while) Java is like C, except the control expression must be Boolean (and the body can only be entered at the beginning--Java has no goto) Ada has a pretest version, but no posttest