60 likes | 182 Views
C++ Expressions. Fred Kuhns fredk@cse.wustl.edu Applied Research Laboratory, Department of Computer Science and Engineering, Washington University in St. Louis. Expressions. composed of one or more operands combined with operators. x = w + 5; x; 23;
E N D
C++ Expressions Fred Kuhns fredk@cse.wustl.edu Applied Research Laboratory, Department of Computer Science and Engineering, Washington University in St. Louis
Expressions • composed of one or more operands combined with operators. x = w + 5; x; 23; • Simplest expression contains no operators: the corresponding objects value is used as the expressions result • literal or constant • variable • Expressions with operators, the result is obtained by evaluating each operator. • Expression result is by default an rValue. CSE332– Object Oriented Programming Lab
Operators • An operator can not be applied (selected) until the types of its operands are known. y = a + b; // which ‘+’ operator is applied • The language defines the behavior of operators applied to built-in types. int i, *ptr = i; *ptr = 5; // OK *i = 7; // Error • Operator precedence, associatively and evaluation order control expression evaluation and thus the result. • We have already talked about this in terms of the C programming language. • Likewise we have discussed of implicit type converion and promotions are used to ensure an operators operands conform to its signature. CSE332– Object Oriented Programming Lab
Some interesting properties • Evaluation order defined for: • Logical && and || operators have an interesting property: they always evaluate their operands left to right. This alows us to use the following simple idiom: if (ptr && (*ptr > 4)) { … } • Comma expression is evaluated left-to-right and the value is the last term evaluated. for (int x = 0; …; x += 1, y = x - 4) {} • ternary operator: condition then either 2nd or 3rd operand x < y ? x : -1; • Otherwise the operand evaluation order is not defined. x = Array[i++] + Array[i]; // Error • Relational operators do not chain if (i < j < k){} versus if (i < j && j < k) {} • Overloaded operators have the same precedence and associativity as the corresponding built-in operator. x = y << 2; // x and y unsigned ints cout << “Some text\n”; CSE332– Object Oriented Programming Lab
More on operators • prefix increment and decrement return the object itself while the postfix operators return an rvalue (i.e. non-modifiable temporary object) int Array[cnt], *ptr = Array; do {*ptr++ = 0;} while (ptr != Array + cnt); *ptr++ = 0; // OK int x = 0; ++x = 0; // OK x++ = 0; // Error • Assignment operator: result is the left hand operand, and is an lValue. • It is right associative. • Has low precedence so you may have to use parens. i = j = k = 4; if ((fd = open(…)) < 0) { … } CSE332– Object Oriented Programming Lab
Statements • Simple statement c = a + b; • Compound statement are defined using curly braces, this define a new scope. {expr; … expr;} • if define variable in a control statement then it is visible only for the that statement for (int x = 0; x < Max; ++x) y = 2 * x; // OK z = x; // Error CSE332– Object Oriented Programming Lab