1 / 6

C++ Expressions

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;

penha
Download Presentation

C++ Expressions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C++ Expressions Fred Kuhns fredk@cse.wustl.edu Applied Research Laboratory, Department of Computer Science and Engineering, Washington University in St. Louis

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

More Related