1 / 26

What is the Result and Type of the Following Expressions?

What is the Result and Type of the Following Expressions?. int x=2, y=15; double u=2.0, v=15.0; -x x+y x-y x*v y / x x/y y%x x%y u*v u/v v/u u%v x * u (x+y)*u u /(x-x) x++ u++ u = --x u = x-- u *= ++x; v /= x; . Types. are these constants legal? .5 5.E 0.5e3 0.5E-3 0.5E-3.5

danielsd
Télécharger la présentation

What is the Result and Type of the Following 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. What is the Result and Type of the Following Expressions? int x=2, y=15; double u=2.0, v=15.0; -x x+y x-y x*v y / x x/y y%x x%y u*v u/v v/u u%v x * u (x+y)*u u /(x-x) x++ u++ u = --x u = x-- u *= ++x; v /= x;

  2. Types • are these constants legal? • .5 5.E0.5e30.5E-30.5E-3.5 • ’a’’%’’Ab’”Ab” • are these variable names legal? • MyTotalMy_Total__MyTotalMy Total • what does this mean? int mytotal=0; int yourtotal(1); • what would be stored in myvar? int myvar; myvar=2.56; • what would be stored in yourvar? double yourvar; yourvar=5/2;

  3. Logical Expressions, IF

  4. Boolean Algebra • logical expressions have one of two values - true or false • a rectangle has three sides. • the instructor has a pleasant smile • the branch of mathematics that deals with this type of logic is called Boolean algebra • developed by the British mathematician George Boole in the 19th century • C++ makes extensive use of Boolean algebra in the form of logical expressions; what is an expression again? • three key logical operators in C++: • && - logical “and” • ||- logical “or” • ! - logical “not”

  5. P Q P && Q False False False False True False True False False True True True Boolean Algebra • truth tables • Lists all combinations of operand values and the result of the operation for each combination • truth table for && (logical “and’)

  6. P Q P || Q False False False False True True True False True True True True Boolean Algebra • truth table for || (logical “or”)

  7. Boolean Algebra • truth table for ! (logical “not”) P !P False True True False

  8. Boolean Algebra • can create complex logical expressions by combining simple logical expressions • example • ! (P && Q) • a truth table can be used to determine when a logical expression is true • note that & and | are also legal operators, make sure to use the correct ones P Q P && Q ! (P && Q) False False False True False True False True True False False True True True True False

  9. Examples of Logical Expressions bool P = true;bool Q = false;bool R = true;bool S = P && Q; bool T = P && !Q bool U = !Q || R; bool V = P || !Q || !R; bool W = P && Q && !R; bool X = Q || (P && R); bool Y = !(R && !Q);bool Z = !(P && Q && R);

  10. Relational Operators • equality operators == note the two equal signs != • examples int i = 32; int k = 45; bool q = i == k; bool r = i != k;

  11. Relational Operators • ordering operators < > >= () <= () • examples int i = 5; int k = 12; bool p = i < 10; bool q = k > i; bool r = i >= k; bool s = k <= 12;

  12. Operator Precedence Expanded • precedence of operators (from highest to lowest) () Unary + - ! * / % + - > < >= >= != == && || =

  13. Examples of Logical Expressions • int a = 5; int b = 10 int c = 20; • bool d = a < b; bool e = a > b; • bool f = (a > b) || (b < c ); • bool g = (a > b) && (b < c ); • bool h = !(a < b); bool i = !(a==b); • bool j = 2*a == b; bool k = (a+b) >= c; • bool l = !((a+b) != c); • bool m = (a+b) == (c-a); • bool n = (a+b) >= (c-a); • int o=a; • int p=o=b;what is the outcome of this operation? • bool q=true; q = d = false;

  14. Operator Precedence Revisited • same or different? (a*b)+c a*b + c a*(b+c) a*b + c (a+b) > c a + b > c a+(b>c) a + b > c (a > b) == (b > c) a > b == b > c (a == b) > (b == c) a == b > b == c (a != b) && (c <= d) a != b && c <= d (a > b) && (c || d) a > b && c || d (a = b) && c a = b && c

  15. Conditional Constructs • provide ability to control whether a statement is executed • two constructs • if-statement • if • if-else • if-else-if • switch-statement

  16. Blocks and Local Variables • a list of statements enclosed in curly brackets is called a block • a block may be placed anywhere a statement can be placed (note the placement of brackets: if ((saleType == ’W’) || (saleType == ’w’)) { total = price * number; } • a variable can be declared and used within block, such avariable is local to the block and does not exist outside of it else if ((saleType == ’R’) || (saleType == ’R’)){ double subtotal; subtotal = price * number; total = subtotal + subtotal * TAX_RATE; } scope of a variable – area in the program where a variable can be used • what is the scope of a variable local to a block? • pitfall: a local variable is accessed outside of the block

  17. expression true false action The Basic If-Statement • syntax if(expression) action • if the expression is true then execute action • action is either a single statement or a block • example 1:if (value > 0) value =0; • example 2: if (value < 0) { value = -value; ++i; }

  18. Sorting Two Numbers cout << "Enter two integers: "; int value1; int value2; cin >> value1 >> value2; if (value1 > value2) { int tmp = value1; value1 = value2; value2 = tmp; } cout << "The input in sorted order: " << value1 << " " << value2 << endl; programming idiom– a common way of accomplishing a simple task: swapping values of two variables using a third is an idiom

  19. The If-Else Statement • syntax if (expression) action1else action2 • if expression is true thenexecute action1otherwiseexecute action2 if (v == 0) cout << "v is 0"; else cout << "v is not 0"; expression false true action1 action2

  20. Selection • it is often the case that depending upon the value of an expression we want to perform a particular action • two major ways of accomplishing this • multiway if-statement • if-else statements “glued” together • switch statement

  21. Switch Statement syntax switch (expression) { case constant: statements break; case constant: statements default:statements }

  22. Switch Example 1 int vclass; cout << "Enter the vehicle class: "; cin >> vclass; switch (vclass){ case 1: cout << "Passenger car"; break; case 2: cout << "Bus"; break; default: cout << "Unknown vehicle class! "; break; // unnecessary but used for consistency }

  23. Switch Example 2 cout << "Enter simple expression: "; int Left; int Right; char Operator; cin >> Left >> Operator >> Right; cout << Left << " " << Operator << " " << Right << " = "; switch (Operator) { case '+' : cout << Left + Right << endl; break; case '-' : cout << Left - Right << endl; break; case '*' : cout << Left * Right << endl; break; case '/' : cout << Left / Right << endl; break; case '%' : cout << Left % Right << endl; break; default: cout << "Illegal operation" << endl; }

  24. Conditional Assignment • an abbreviated form of branching construct (or expanded assignment?) - conditional assignment variable = expression ? true-expression : false-expression; • variable is assigned the value of true-expression if expression evaluates to true and the value of false-expression otherwise • what branching construct is this assignment equivalent to? • example: int i = j>0 ? j : -j; • program that calculates the largest number ( of two) int main() { int n1, n2; cin >> n1 >> n2; int max = n1 > n2 ? n1 : n2; cout << ”maximum is ” << max << endl; } • conditional assignment is a tertiary operator

  25. Named Constants • using number constants can be troublesome: 9.8 does not give a hint as whether it is a tax rate, acceleration of gravity, or what. • number constants are hard to modify if spread all over the program • named constant provides a name to a constant: const int WINDOW_COUNT = 5; const double TAX_RATE = 9.8; • constants are usually declared at the beginning of the program. capital letters are usually used to give the programmer a hint that this is a constant when he encounters it in the program note to C programmers: #define for constants is completely replaced by constin C++; in general, #define should not be used in C++; there are better versions for each of its uses in C++.

  26. Debugging and Tracing Programs • specially compiled executables leave information from the original source file: names of variables and source lines • this allows • program tracing – suspending program execution at specific source lines and executing the program one source line at a time • variable watching – observing values stored in source program variables • Setting breakpoints i.e. lines in the source program where execution is to be suspended.

More Related