Lecture 4 Expressions & Statements
Lecture 4 Expressions & Statements
ExpressionsAn expression is composed of one or more operands and yields a result when itis evaluated.The simplestform ofan expression is a singleliteral orvariable..More complicated expressions areformed from an operatorand one or moreoperands.a*bcout<<2"Hello"+"World
Expressions cout << 2 An expression is composed of one or more operands and yields a result when it is evaluated. • The simplest form of an expression is a single literal or variable. • More complicated expressions are formed from an operator and one or more operands. a * b “Hello” + “World
OperatorsBinary operatorsUnary operatorsinta = 3;double x =2.3 *5.0;// multiplicationint*p = &a;//insertion//address-ofcout <<x;int b=*p;lldereference
Operators int a = 3; int *p = &a; int b = *p; Unary operators double x = 2.3 * 5.0; // address-of cout << x; // dereference // multiplication // insertion Binary operators
Operatorsvector<int> v;/ / do something to vTheconditional operator (ternary)string s;vector<int>v;if (v.size() > 10) (strings=(v.size()>10)??"long":"short"s ="long";1else (Syntaxs="short";(condition) ? (if_true) : (if false);1
Operators vector<int> v; string s = (v.size( ) > 10) ? “long”: “short”; The conditional operator (ternary) (condition) ? (if_true) : (if_false); Syntax vector<int> v; // do something to v string s; if (v.size( ) > 10) { s = “long”; } else { s = “short”; }
Precedence & associativity优先级结合性Table 4.4.Operator PrecedenceSweAssociativityTagEFunctionUseand Operator2globalscupetiname3+4*5拉1Class soopeclass:inamenamespuce scopenamespacetiname2memberselectorsobject.member1101.member.selectorspointer->memberLsubscriptexpr(expr]20-15-3Vfunction calloname(exprJist)1164()type (expr.list)type cometructionHRposthix incrementIvalue+*++147R-postfix decrementIvalue-.R826typeldtypelDtypeid(type)06Rrun-time typeIDtypeidtypeid(expr)8162explicitcastast_ramrctypes(expr)type-conversionNTinta[5] =[1,2, 4, 8, 16];Rprefix increment++lvalue++147Rprefix decrement--lvalue--R152bitwise Nor"exprR1411logical Norlexprint b = *(a + 3);R1401/b=8unary minus-OAPER140+unaryplusexprR.dereferenceCeNpER-slvalueaddress-ofint c=*a + 3;1/c=4R0161type conversion(type)exprR15aizeofsizeofobjectsizeofexpr156Rsineotsizeoftypeadseot(type)R700stee of parameter paickstseot...(rume)siseot...R458newallocate objecttew.typeRnewtlnev typelsizel458allocatearrav
Precedence & associativity 3 + 4 * 5 优先级 结合性 20 – 15 – 3 int a[5] = {1, 2, 4, 8, 16}; int b = *(a + 3); int c = *a + 3; // b = 8 // c = 4