1 / 36

Chapter 3: Expressions and Interactivity

Chapter 3: Expressions and Interactivity. Outline. cin object Mathematical expressions Type Conversion and Some coding styles. The cin Object. Standard input object Like cout , requires iostream file Used to read input from keyboard. How to use cin ?. SYNTAX

Télécharger la présentation

Chapter 3: Expressions and Interactivity

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. Chapter 3: Expressions and Interactivity

  2. Outline • cin object • Mathematical expressions • Type Conversion and Some coding styles

  3. The cin Object • Standard input object • Like cout, requires iostream file • Used to read input from keyboard

  4. How to use cin? SYNTAX These examples yield the same result. cin >> length ; cin >> width ; or cin >> length >> width ; cin >> Variable >> Variable .. . ;

  5. The cin Object • Information retrieved from cin with >> (the extraction operator) • operator >> attempts to extractthe next item from the input stream and store its value in the right operand variable • Input is stored in one or more variables

  6. Displaying a Prompt • A prompt is a message that instructs the user to enter data. • You should alwaysusecoutto display a prompt before each cin statement.cout << "How tall is the room? ";cin >> height;

  7. The cin Object • Can be used to input more than one value: cin >> height >> width; • Multiple values from keyboard must be separated by spaces • Order is important: first value entered goes to first variable, etc.

  8. Outline • cin object • Mathematical expressions • Type Conversion and Some coding styles

  9. Mathematical Expressions • Can create complex expressions using multiple mathematical operators • An expression can be a literal, a variable, or a mathematical combination of constants and variables

  10. Mathematical Expressions • Can be used in assignment, cout, other statements: area = 2 * PI * radius; cout << "border is: " << 2*(l+w);

  11. Some C++ operators PrecedenceOperator Description Higher + Positive - Negative ----------------------------------------------------------- * Multiplication / Division % Modulus ------------------------------------------------------------ + Addition - Subtraction ------------------------------------------------------------- Lower = Assignment

  12. Example 7 * 10 - 5 % 3 * 4 + 9 means (7 * 10) - 5 % 3 * 4 + 9 70 - 5 % 3 * 4 + 9 70 - (5 % 3) * 4 + 9 70 - 2 * 4 + 9 70 - ( 2 * 4 ) + 9 70 - 8 + 9 ( 70 - 8 ) + 9 62 + 9 71

  13. Parentheses • Parentheses can be used to change the usual order; expressions inside ( ) are evaluated first • evaluate (7 * (10 - 5) % 3) * 4 + 9 ( 7 * 5 % 3 ) * 4 + 9 ( 35 % 3 ) * 4 + 9 2 * 4 + 9 8 + 9 17

  14. Algebraic Expressions • Multiplication requires an operator: Area=lw is written as Area = l * w; • Parentheses may be needed to maintain order of operations: is written as m = (y2-y1) /(x2-x1);

  15. When calculation average • Average = a + b + c / 3.0; (Wrong!!!) • Average = (a + b + c) / 3.0; (Correct!!!)

  16. Algebraic Expressions

  17. Algebraic Expressions

  18. y = 3 * x /2; A = (3*x+2)/(4*a-1); d = b*b -4*a*c; Algebra and C++ expressions

  19. Outline • cin object • Mathematical expressions • Type Conversion and Some coding styles

  20. When You Mix Apples and Oranges: Type Conversion • Operations are performed between operands of the same type. • If not of the same type, C++ will convert one to be the type of the other • This can impact the results of calculations.

  21. Example • double parts; • parts = 15/6; //parts=2 • double parts; • parts = 15.0/6; //parts=2.5

  22. Assignment operator = Variable = Expression; The Expression on right is evaluated first. Then the resulting value is stored in the memory location of Variable on left. NOTE: An automatic type coercion occurs after evaluation but before the value is stored if the types differ for Expression and Variable

  23. Named (Symbolic) Constants • Named constant (constant variable): variable whose content cannot be changed during program execution • Often named in uppercase letters • Used for representing constant values with descriptive names: const double TAX_RATE = 0.0675; constint NUM_STATES = 50;

  24. value is 5 value is 5 value is 5 Multiple Assignment and Combined Assignment • The = can be used to assign a value to multiple variables: x = y = z = 5; • Value of = is the value that is assigned • Associates right to left: x = (y = (z = 5));

  25. Combined Assignment • Look at the following statement: sum = sum + 1; This adds 1 to the variable sum.

  26. Other Similar Statements

  27. Other Similar Statements Slide 3- 31

  28. Combined Assignment • The combined assignment operators provide a shorthand for these types of statements. • The statement sum = sum + 1; is equivalent to sum += 1;

  29. Combined Assignment Operators

  30. Combined Assignment Operators Slide 3- 34

  31. Mathematical Library Functions • #include <cmath> • Commonly used functions: • where n can be an integer, double, float. Slide 3- 35

  32. Mathematical Library Functions • In order to user the functions you need to know:  • name of the function you need • what the function does (input/output) • input parameter and the data type of parameter • data type of returned value • Syntax: value = function_name (argument); Slide 3- 36

  33. Mathematical Library Functions • Examples: sqrt_value = sqrt(49); //Result is 7 abs_value = abs(-34.5); //Result is 34.5 power_value = pow(2.1,3); //Result is 9.261 cosine_value = cos(3.1415196/6); //Result is 0.866 (cos 30) Slide 3- 37

More Related