Lecture Ppt 4

download Lecture Ppt 4

of 42

Transcript of Lecture Ppt 4

  • 8/2/2019 Lecture Ppt 4

    1/42

  • 8/2/2019 Lecture Ppt 4

    2/42

    Operators and Operands

    Unary Operators

    The Positive Operator + The Negative Operator -

    The Address Of Operator &

    C++ Operators Parentheses

    Square Brackets

    Curly Brackets

  • 8/2/2019 Lecture Ppt 4

    3/42

    An operation is an action performed on one ormore values either to modify the value held byone or both of the variables or to produce anew value by combining variables. Therefore,an operation is performed using at least onesymbol and one value. The symbol used in anoperation is called an operator. A variable or avalue involved in an operation is called anoperand.

    A unary operator is an operator that performsits operation on only one operand.

    An operator is referred to as binary if itoperates on two operands.

  • 8/2/2019 Lecture Ppt 4

    4/42

    A unary operator is an operator thatperforms its operation on only one

    operand.

  • 8/2/2019 Lecture Ppt 4

    5/42

  • 8/2/2019 Lecture Ppt 4

    6/42

    A value on the right side of 0 is consideredpositive. To express that a number ispositive, you can write a + sign on its left.

    Examples are +4, +228, +90335. In this casethe + symbol is called a unary operatorbecause it acts on only one operand.

    The positive unary operator, when used,must be positioned on the left side of itsoperand, never on the right side.

    As a mathematical convention, when avalue is positive, you do not need to expressit with the + operator.

  • 8/2/2019 Lecture Ppt 4

    7/42

    #include using namespace std; int main()

    { int a; a = +4; cout

  • 8/2/2019 Lecture Ppt 4

    8/42

    in order to express any number on the left side of 0, itmust be appended with a sign, namely the - symbol.Examples are -12, -448, -32706. A valueaccompanied by - is referred to as negative.

    The - sign must be typed on the left side of thenumber it is used to negate.

    Remember that if a number does not have a sign, it isconsidered positive. Therefore, whenever a number isnegative, it MUST have a - sign. In the same way, if

    you want to change a value from positive tonegative, you can just add a - sign to its left. Here is an example that uses two variables. One has

    a positive value while the other has a negative value:

  • 8/2/2019 Lecture Ppt 4

    9/42

    #include using namespace std; int main() { int a , b; a = -4; b = 3;

    cout

  • 8/2/2019 Lecture Ppt 4

    10/42

    In the previous lesson, we learned that,when declaring a variable, the compilerreserves an amount of space in memory for

    that variable. C++ provides an operatorthat can tell you where (the space for) avariable is located. This is done using the"address of" operator represented by theampersand &.

    To get the address of a variable, use theampersand operator on its left. The addressis a hexadecimal number. Here is anexample:

  • 8/2/2019 Lecture Ppt 4

    11/42

    #include using namespace std; int main() { int number = 46;

    cout

  • 8/2/2019 Lecture Ppt 4

    12/42

    As a language, C++ is equipped withnon-algebraic operators intended to

    perform specific operations. We willreview some of these operators now;some others need intermediaryconcepts that we haven't studied and

    cannot study at this time.

  • 8/2/2019 Lecture Ppt 4

    13/42

    Like most computer languages, C++ usesparentheses to isolate a group of items

    that must be considered as belonging toone entity. For example, as we will learnsoon, parentheses allow a function todelimit the list of its arguments.

  • 8/2/2019 Lecture Ppt 4

    14/42

    In the same way, if your operation involvesvarious operators such as addition(s) andsubtraction(s), you can use parentheses to tellthe compiler how to proceed with theoperations, that is, what operation should(must) be performed first. Consider thefollowing algebraic operation: 154 - 12 + 8

    The question here is to know whether you want

    to subtract the addition of 12 and 8 from 154 oryou want to add the difference between 154and 12 to 8. Using parentheses, you cancommuniqu your intentions to the compiler.This is illustrated in the following program:

  • 8/2/2019 Lecture Ppt 4

    15/42

    #include using namespace std; int main() { cout

  • 8/2/2019 Lecture Ppt 4

    16/42

    Square brackets are mostly used tocontrol the dimension or index of an

    array. We will learn how to use themwhen we study arrays.

  • 8/2/2019 Lecture Ppt 4

    17/42

    Curly brackets are probably the mostused and the most tolerant operators of

    C++. Fundamentally, curly brackets areused to create or isolate sections ofcode. As such they are required todelimit the bodies of functions, classes,

    structures, exceptions, and namespaces.They are also optionally used inconditional statements.

  • 8/2/2019 Lecture Ppt 4

    18/42

    Algebraic Operators The Addition The Multiplication

    The Subtraction The Division The Remainder Increment and Decrement ++ and -- Incrementing a Number

    Pre and Post-Increment Decrementing -Pre and Post- Decrementing Techniques of Incrementing and Decrementing a

    Variable Operator Precedence and Direction

  • 8/2/2019 Lecture Ppt 4

    19/42

    In algebra, operations are performed onnumeric values. Algebraic operators are

    represented with the following symbols:

  • 8/2/2019 Lecture Ppt 4

    20/42

    The addition is an operation used to add one numberto another. For example, if you have one pen andsomebody gives you another pen, then you havetwo pens. If you keep receiving pens, you will soon

    have more than you had originally. The addition is performed in mathematics using the +

    sign. The same sign is used in C++. The + sign islocated on the left side of the Backspace key. Youget it by pressing Shift and =.

    The order you use to add two or more values doesn'tmatter. This means Value1 + Value2 is the same asValue2 + Value1. In the same way a + b + c is thesame as a + c + b the same as b + a + c and thesame as c + b + a.

  • 8/2/2019 Lecture Ppt 4

    21/42

    #include using namespace std; int main() { // Get the sum of two values

    cout

  • 8/2/2019 Lecture Ppt 4

    22/42

    The multiplication allows adding one value to itself acertain number of times, set by a second value. As anexample, instead of adding a value to itself in thismanner: a + a + a + a, since the variable a is repeated

    over and over again, you could simply find out howmany times a is added to itself, then multiply a by thatnumber which, is this case, is 4. This would mean addinga to itself 4 times, and you would get the same result.

    The multiplication is performed with the * sign which istyped with Shift + 8.

    Just like the addition, the multiplication is associative: a * b * c = c * b * a. When it comes to programming syntax, the rules we

    learned with the addition operation also apply to themultiplication.

  • 8/2/2019 Lecture Ppt 4

    23/42

    // Multiplication of multiple values #include

    using namespace std;

    int main() { float a, b; // Multiple of a and b. cout

  • 8/2/2019 Lecture Ppt 4

    24/42

    The subtraction operation is used to takeout or subtract a value from another value.It is essentially the opposite of the addition.

    The subtraction in C++ is performed withthe - sign, which is between the 0 and the =keys.

  • 8/2/2019 Lecture Ppt 4

    25/42

    #include using namespace std; int main() { // Values used in this program

    int value1, value2, value3; // Get the values from the user cout > value1; cout > value2;

    // Subtract the first value from the second value3 = value1 - value2; cout

  • 8/2/2019 Lecture Ppt 4

    26/42

    cut an apple in the middle, you are dividing it in 2pieces. If you cut each one of the resulting pieces, youwill get 4 pieces or fractions. This is considered that youhave divided the apple in 4 parts.

    Therefore, the division is used to get the fraction of onenumber in terms of another.

    The division is performed with the forward slash /.

    When performing the division, be aware of its many

    rules. Never divide by zero (0). Make sure that you knowthe relationship(s) between the numbers involved in theoperation.

  • 8/2/2019 Lecture Ppt 4

    27/42

    #include using namespace std; int main() { float a; // Get a number from the user. cout > a; cout

  • 8/2/2019 Lecture Ppt 4

    28/42

    The division program above will give you aresult of a number with decimal values if youtype an odd number (like 147), which is fine insome circumstances. Sometimes you will want

    to get the value remaining after a divisionrenders a natural result.

    The remainder operation is performed with thepercent sign (%) which is gotten from pressingShift + 5.

  • 8/2/2019 Lecture Ppt 4

    29/42

    #include using namespace std;

    int main() { players = 26;

    int yourPlayers; // When the game starts, how many players will wait?. cout

  • 8/2/2019 Lecture Ppt 4

    30/42

    We are used to counting numbers such as 1, 2, 3, 4, etc. In reality,when counting such numbers, we are simply adding 1 to anumber in order to get the next number in the range. C++provides a technique of transparently counting such numbers.

    The simplest technique of incrementing a value consists ofadding 1 to it. After adding 1, the value or the variable is(permanently) modified and the variable would hold the newvalue.

  • 8/2/2019 Lecture Ppt 4

    31/42

    #include

    using namespace std; int main() { int value = 12; cout

  • 8/2/2019 Lecture Ppt 4

    32/42

    #include

    using namespace std;

    int main()

    {

    int value = 12;

    cout

  • 8/2/2019 Lecture Ppt 4

    33/42

    When using the ++ operator, the position of the operator with regard to thevariable it is modifying can be significant. To increment the value of the

    variable before re-using it, you should position the operator on the left of thevariable:

    #include

    using namespace std;

    int main() { int value = 12;

    cout

  • 8/2/2019 Lecture Ppt 4

    34/42

    When writing ++Value, the value of the variable is incremented beforebeing called. On the other hand, if you want to first use a variable, then

    increment it, in other words, if you want to increment the variable aftercalling it, position the ++ operator on the right side of the variable:

    #include using namespace std; int main() { int value = 12; cout

  • 8/2/2019 Lecture Ppt 4

    35/42

    When counting numbers backward, such as 8, 7, 6, 5, etc, we are infact subtracting 1 from a value in order to get the lesser value. Thisoperation is referred to as decrementing a variable. Thisoperation works as if a variable called Value has its valuediminished by 1, as in Value = Value1:

    #include using namespace std; int main() { int value = 8; cout

  • 8/2/2019 Lecture Ppt 4

    36/42

    As done to increment, C++ provides a quicker way of subtracting 1 from avariable. This is done using the decrement operation, that is --. To use thedecrement operator, typeon the left or the right side of the variable whenthis operation is desired. Using the decrement operator.

    #include

    using namespace std; int main() {

    int value = 8;

    cout

  • 8/2/2019 Lecture Ppt 4

    37/42

    #include

    using namespace std;

    int main() {

    int value = 8;

    cout

  • 8/2/2019 Lecture Ppt 4

    38/42

    It is not unusual to add or subtract a constant value to or from a variable. All youhave to do is to declare another variable that would hold the new value. Here isan example:

    #include

    using namespace std;

    int main() { double value = 12.75;

    double newValue;

    cout

  • 8/2/2019 Lecture Ppt 4

    39/42

    The previous technique requires that you use an extra variable in yourapplication. The advantage is that each value can hold its own value althoughthe value of the second variable depends on whatever would happen to theoriginal or source variable.

    Sometimes in your program you will not need to keep the original value of thesource variable. You might want to simply permanently modify the value that a

    variable is holding. In this case you can perform the addition operation directlyon the variable by adding the desired value to the variable. This operationmodifies whatever value a variable is holding and does not need an additionalvariable. To add a value to a variable and change the value that the variable isholding, use the assignment = and the addition + operators to produce a

    new operator as +=

  • 8/2/2019 Lecture Ppt 4

    40/42

    #include

    using namespace std;

    int main() {

    double value = 12.75;

    cout

  • 8/2/2019 Lecture Ppt 4

    41/42

    To diminish the value of a variable, instead of the addition operation, use thesubtraction and apply the same technique. In the above program, the variablecan have its value decremented by applying the assignment and thesubtraction operations on the variable. This is done with the -= operator. Here isan example:

    #include

    using namespace std;

    int main() {

    double value = 12.75;

    cout

  • 8/2/2019 Lecture Ppt 4

    42/42

    When combining operations in C++, there are two aspects involved: anoperator's precedence and its direction.

    If you ask a program to add two numbers, for example 240 + 65, theprogram will execute the operation by adding 240 to 65; in other words, itwould read 240, then +, then 65, and evaluate the result. This is consideredas operating from left to right: 240 -> + -> 65.

    On the other hand, if you ask the program to find the size of a variable, you

    would write sizeof(FirstName). In this case, the program would first considerthe FirstName variable, then it would evaluate the size of that variable; inother words, it first finds out what the variable it is operating on, in this caseFirstName. Once it knows the variable that is involved, it would execute theoperation. This is considered that the operation is executed from right to left.

    This process is referred to as the direction of the operation.

    As you will regularly combine operators on your various calculations, eachoperation is known for how much it "weighs" as compared to otheroperators. This is known as its precedence. This means that when a certainoperator is combined with another, such as a + b * c, or x / y - z, someoperators would execute before others, almost regardless of how you writethe operation. That's why an operator could be categorized by its level ofprecedence.