Lecture 3

568 views

Published on

Operators and Expressions in C++

Published in: Education, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
568
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
29
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • Lecture 3

    1. 1. <ul><li>AU/MITM/1.6 </li></ul><ul><li>By Mohammed A. Saleh </li></ul>
    2. 2. <ul><li>➩ Module 2 </li></ul><ul><li>Fundamentals of C++ - Operators & Expressions – Data I/O – Control Structures – Storage Classes – Arrays and Strings. </li></ul>
    3. 3. <ul><li>Arithmetic Operators </li></ul><ul><li>C++ uses operators to do arithmetic </li></ul><ul><li>It provides operators for five basic arithmetic calculations: addition, subtraction, multiplication, division, and taking the modulus </li></ul><ul><li>Each of these operators uses two values (called operands) to calculate a final answer. </li></ul><ul><li>Together, the operator and its operands constitute an expression. </li></ul>
    4. 4. <ul><li>Example </li></ul><ul><li>int wheels = 4 + 2; </li></ul><ul><li>The values 4 and 2 are operands, the + symbol is the addition operator, and 4 + 2 is an expression whose value is 6. </li></ul><ul><li>The % operator finds the modulus of its first operand with respect to the second. That is, it produces the remainder of dividing the first by the second. For example, 19 % 6 is 1 . </li></ul>
    5. 5. <ul><li>// arith.cpp -- some C++ arithmetic </li></ul><ul><li>#include <iostream> </li></ul><ul><li>int main() </li></ul><ul><li>{ </li></ul><ul><li>float hats, heads; </li></ul><ul><li>cout << “Enter a number: “; </li></ul><ul><li>cin >> hats; </li></ul><ul><li>cout << “Enter another number: “; </li></ul><ul><li>cin >> heads; </li></ul><ul><li>cout << “hats = “ << hats << “; heads = “ << heads << endl; </li></ul><ul><li>cout << “hats + heads = “ << hats + heads << endl; </li></ul><ul><li>cout << “hats - heads = “ << hats - heads << endl; </li></ul><ul><li>cout << “hats * heads = “ << hats * heads << endl; </li></ul><ul><li>cout << “hats / heads = “ << hats / heads << endl; </li></ul><ul><li>return 0; </li></ul><ul><li>} </li></ul>
    6. 6. <ul><li>Output for the program: </li></ul><ul><li>Enter a number: 50.25 </li></ul><ul><li>Enter another number: 11.17 </li></ul><ul><li>hats = 50.250000; heads = 11.170000 </li></ul><ul><li>hats + heads = 61.419998 </li></ul><ul><li>hats - heads = 39.080002 </li></ul><ul><li>hats * heads = 561.292480 </li></ul><ul><li>hats / heads = 4.498657 </li></ul><ul><li>If you need greater accuracy then use double or long double. </li></ul>
    7. 7. <ul><li>Order of Operation – Operator Precedence and </li></ul><ul><li>Associativity </li></ul><ul><li>Consider this statement: </li></ul><ul><li>int flyingpigs = 3 + 4 * 5; // 35 or 23? </li></ul><ul><li>The 4 appears to be an operand for both the + and * operators. </li></ul><ul><li>When this happens, C++ uses precedence rules to decide which operator is used first. </li></ul>
    8. 8. <ul><li>The arithmetic operators follow the usual algebraic precedence, with multiplication, division, and the taking of the modulus done before addition and subtraction. Thus, 3 + 4 * 5 means 3 + (4*5). So the answer is 23, not 35. </li></ul><ul><li>Note that *, /, and % all have equal precedence. Similarly, addition and subtraction share a lower precedence. </li></ul>
    9. 9. <ul><li>Consider the following: </li></ul><ul><li>float logs = 120 / 4 * 5; // 150 or 6? </li></ul><ul><li>When two operators have the same precedence, C++ looks at whether the operators have a left-to-right associativity a right-to-left associativity. </li></ul><ul><li>L-R associativity means that if two operators acting on the same operand have the same precedence, you apply the left-hand operator first. The reverse is true for R-L. </li></ul>
    10. 10. <ul><li>Division Diversion </li></ul><ul><li>The behavior of this operator depends on the type of the operands. </li></ul><ul><li>If both operands are integers, C++ performs integer division. That means any fractional part of the answer is discarded, making the result an integer. </li></ul><ul><li>If one or both operands are floating-point values, the fractional part is kept, making the result floating-point. </li></ul>
    11. 11. <ul><li>// divide.cpp -- integer and floating-point division </li></ul><ul><li>#include <iostream> </li></ul><ul><li>int main() </li></ul><ul><li>{ </li></ul><ul><li>cout << “Integer division: 9/5 = “ << 9 / 5 << endl; </li></ul><ul><li>cout << “Floating-point division: 9.0/5.0 = “; </li></ul><ul><li>cout << 9.0 / 5.0 << endl; </li></ul><ul><li>cout << “Mixed division: 9.0/5 = “ << 9.0 / 5 << endl; </li></ul><ul><li>cout << “double constants: 1e7/9.0 = “; </li></ul><ul><li>cout << 1.e7 / 9.0 << endl; </li></ul><ul><li>cout << “float constants: 1e7f/9.0f = “; </li></ul><ul><li>cout << 1.e7f / 9.0f << endl; </li></ul><ul><li>return 0; </li></ul><ul><li>} </li></ul>
    12. 12. <ul><li>Program Output: </li></ul><ul><li>Integer division: 9/5 = 1 </li></ul><ul><li>Floating-point division: 9.0/5.0 = 1.800000 </li></ul><ul><li>Mixed division: 9.0/5 = 1.800000 </li></ul><ul><li>double constants: 1e7/9.0 = 1111111.111111 </li></ul><ul><li>float constants: 1e7f/9.0f = 1111111.125000 </li></ul><ul><li>The division operator represents three distinct operations: int division, float division, and double division. C++ uses the context—in this case the type of operands—to determine which operator is meant. </li></ul>
    13. 13. <ul><li>The process of using the same symbol for more than one operation is called operator overloading </li></ul><ul><li>Figure 1.0: Division Diversion </li></ul>
    14. 14. <ul><li>The Modulus Operator </li></ul><ul><li>The modulus operator returns the remainder of an integer division. </li></ul><ul><li>Useful in problems that require dividing a quantity into different integral units, such as converting inches to feet and inches or converting dollars to quarters, dimes, nickels, and pennies. </li></ul><ul><li>Example program: </li></ul>
    15. 15. <ul><li>// modulus.cpp -- uses % operator to convert lbs to stone </li></ul><ul><li>#include <iostream> </li></ul><ul><li>int main() </li></ul><ul><li>{ </li></ul><ul><li>const int Lbs_per_stn = 14; </li></ul><ul><li>int lbs; </li></ul><ul><li>cout << “Enter your weight in pounds: “; </li></ul><ul><li>cin >> lbs; </li></ul><ul><li>int stone = lbs / Lbs_per_stn; // whole stone </li></ul><ul><li>int pounds = lbs % Lbs_per_stn; // remainder in pounds cout << lbs << “ pounds are “ << stone </li></ul><ul><li><< “ stone, “ << pounds << “ pound(s). ”; </li></ul><ul><li>return 0; </li></ul><ul><li>} </li></ul>
    16. 16. <ul><li>Sample run of the program: </li></ul><ul><li>Enter your weight in pounds: 177 </li></ul><ul><li>184 pounds are 12 stone, 9 pound(s ). </li></ul>
    17. 17. <ul><li>Relational Expressions </li></ul><ul><li>C++ provides six relational operators to compare numbers. </li></ul><ul><li>Each relational expression reduces to the bool value true if the comparison is true and to the bool value false if the comparison is false, so these operators are well suited for use in a loop test expression </li></ul>
    18. 18. <ul><li>Table 1.0: Relational operators </li></ul>
    19. 19. <ul><li>Example tests: </li></ul><ul><li>for (x = 20; x > 5; x--) // continue while x is greater than 5 </li></ul><ul><li>for (x = 1; y != x; ++x) // continue while y is not equal to x </li></ul><ul><li>The relational operators have a lower precedence than the arithmetic operators. That means this expression : </li></ul><ul><li>x + 3 > y - 2 // Expression 1 </li></ul><ul><li>corresponds to this: </li></ul><ul><li>(x + 3) > (y - 2) // Expression 2 </li></ul><ul><li>and not to the following: </li></ul><ul><li>x + (3 > y) - 2 // Expression 3 </li></ul>
    20. 20. 1. Load <iostream> 2. main 2.1 Initialize num1 and num2 2.1.1 Input data 2.2 if statements Enter two integers, and I will tell you the relationships they satisfy: 3 7 3 is not equal to 7 3 is less than 7 3 is less than or equal to 7 1 // relational.cpp 2 // Using if statements, relational 3 // operators, and equality operators 4 #include <iostream> 5 6 7 8 9 10 int main() 11 { 12 int num1, num2; 13 14 cout << &quot;Enter two integers, and I will tell you &quot; 15 << &quot;the relationships they satisfy: &quot;; 16 cin >> num1 >> num2; // read two integers 17 18 if ( num1 == num2 ) 19 cout << num1 << &quot; is equal to &quot; << num2 << endl; 20 21 if ( num1 != num2 ) 22 cout << num1 << &quot; is not equal to &quot; << num2 << endl; 23 24 if ( num1 < num2 ) 25 cout << num1 << &quot; is less than &quot; << num2 << endl; 26 27 if ( num1 > num2 ) 28 cout << num1 << &quot; is greater than &quot; << num2 << endl; 29 30 if ( num1 <= num2 ) 31 cout << num1 << &quot; is less than or equal to &quot; 32 << num2 << endl; 33 The if statements test the truth of the condition. If it is true , body of if statement is executed. If not, body is skipped. To include multiple statements in a body, delineate them with braces {} .
    21. 21. 2.3 exit ( return 0 ) Program Output Enter two integers, and I will tell you the relationships they satisfy: 3 7 3 is not equal to 7 3 is less than 7 3 is less than or equal to 7 Enter two integers, and I will tell you the relationships they satisfy: 22 12 22 is not equal to 12 22 is greater than 12 22 is greater than or equal to 12 Enter two integers, and I will tell you the relationships they satisfy: 7 7 7 is equal to 7 7 is less than or equal to 7 7 is greater than or equal to 7 34 if ( num1 >= num2 ) 35 cout << num1 << &quot; is greater than or equal to &quot; 36 << num2 << endl; 37 38 return 0; // indicate that program ended successfully 39 }
    22. 22. <ul><li>Logical Expressions </li></ul><ul><li>Often you must test for more than one condition. For example, for a character to be a lower- case letter, its value must be greater than or equal to ‘a’ and less than or equal to ‘z’ </li></ul><ul><li>Or, if you ask a user to respond with a y or an n, you want to accept uppercase (Y and N) as well as lowercase. </li></ul>
    23. 23. <ul><li>To meet this kind of need, C++ provides three logical operators to combine or modify existing expressions. </li></ul><ul><li>The operators are logical OR, written ||; logical AND, written &&; and logical NOT, written ! </li></ul><ul><li>The Logical OR Operator: || </li></ul><ul><li>This operator combines two expressions into one. </li></ul>
    24. 24. <ul><li>If either or both of the original expressions is true, or nonzero, the resulting expression has the value true. Otherwise, the expression has the value false. </li></ul><ul><li>Example: </li></ul><ul><li>5 ==5 || 5 == 9 </li></ul><ul><li>5 > 3 || 5 > 10 </li></ul><ul><li>5 > 8 || 5 < 10 </li></ul><ul><li>5 < 8 || 5 > 2 </li></ul><ul><li>5 > 8 || 5 < 2 </li></ul>
    25. 25. <ul><li>Table 2.0: The || Operator </li></ul><ul><li>C++ provides that the ||operator is a sequence point. That is, any value changes indicated on the left side take place before the right side is evaluated. </li></ul><ul><li>Example: </li></ul><ul><li>i++ < 6 || i == j // take i to be 10 </li></ul>
    26. 26. <ul><li>// or.cpp -- using the logical OR operator </li></ul><ul><li>#include <iostream> </li></ul><ul><li>int main() </li></ul><ul><li>{ </li></ul><ul><li>cout << “This program may reformat your hard disk ” </li></ul><ul><li>“ and destroy all your data. ” </li></ul><ul><li>“ Do you wish to continue? <y/n> “; </li></ul><ul><li>char ch; </li></ul><ul><li>cin >> ch; </li></ul><ul><li>if (ch == ‘y’ || ch == ‘Y’) // y or Y </li></ul><ul><li>cout << “You were warned!aa ”; </li></ul><ul><li>else if (ch == ‘n’ || ch == ‘N’) // n or N </li></ul><ul><li>cout << “A wise choice ... bye ”; </li></ul><ul><li>Else </li></ul><ul><li>cout << “That wasn’t a y or an n, so I guess I’ll “ “trash your disk anyway.aaa ”; </li></ul><ul><li>return 0; </li></ul><ul><li>} </li></ul>
    27. 27. <ul><li>Sample run of the program: </li></ul><ul><li>This program may reformat your hard disk </li></ul><ul><li>and destroy all your data. </li></ul><ul><li>Do you wish to continue? <y/n> N </li></ul><ul><li>A wise choice ... Bye </li></ul><ul><li>The Logical AND Operator: && </li></ul><ul><li>Like the || operator, the && operator also combines two expressions into one. </li></ul>
    28. 28. <ul><li>The resulting expression has the value true only if both of the original expressions are true. </li></ul><ul><li>Examples: </li></ul><ul><li>5 == 5 && 4 == 4 </li></ul><ul><li>5 == 3 && 4 == 4 </li></ul><ul><li>5 > 3 && 5 > 10 </li></ul><ul><li>5 > 8 && 5 < 10 </li></ul><ul><li>5 < 8 && 5 > 2 </li></ul><ul><li>5 > 8 && 5 < 2 </li></ul>
    29. 29. <ul><li>Like the ||operator, the &&operator acts as a sequence point, so the left side is evaluated. </li></ul><ul><li>Table 3.0: The && Operator </li></ul>
    30. 30. <ul><li>The Logical NOT Operator : ! </li></ul><ul><li>The ! operator negates, or reverses the truth value of, the expression that follows it. </li></ul><ul><li>That is, if expression is true, then !expression is false—and vice versa </li></ul><ul><li>Example: </li></ul><ul><li>while (n!=7) </li></ul><ul><li>body </li></ul>
    31. 31. <ul><li>Logical Operator Facts </li></ul><ul><li>The C++ logical OR and logical AND operators have a lower precedence than relational operators. </li></ul><ul><li>This means that an expression such as this: </li></ul><ul><li>x > 5 && x < 10 </li></ul><ul><li>is read this way: </li></ul><ul><li>(x > 5) && (x < 10) </li></ul>
    32. 32. <ul><li>The ! operator, on the other hand, has a higher precedence than any of the relational or arithmetic operators. </li></ul><ul><li>Therefore, to negate an expression, you should enclose the expression in parentheses, like this: </li></ul><ul><li>!(x > 5) // is it false that x is greater than 5 </li></ul><ul><li>The logical AND operator has a higher precedence than the logical OR operator. Thus this expression: </li></ul>
    33. 33. <ul><li>age > 30 && age < 45 || weight > 300 </li></ul><ul><li>means the following: </li></ul><ul><li>(age > 30 && age < 45) || weight > 300 </li></ul><ul><li>That is, one condition is that age be in the range 31–44, and the second condition is that weight be greater than 300. The entire expression is true if one or the other or both of these conditions are true. </li></ul>
    34. 34. <ul><li>// LAB </li></ul><ul><li># include <iostream> </li></ul><ul><li># include <conio> </li></ul><ul><li>int main () </li></ul><ul><li>{ </li></ul><ul><li>cout << “ LET’S CODE ”; </li></ul><ul><li>getch (); </li></ul><ul><li>return 0; } </li></ul>
    35. 35. <ul><li>// Weekend </li></ul><ul><li># include <iostream> </li></ul><ul><li># include <conio> </li></ul><ul><li>int main () </li></ul><ul><li>{ </li></ul><ul><li>cout << “ Have A C++ Weekend ”; </li></ul><ul><li>getch (); </li></ul><ul><li>return 0; } </li></ul>

    ×