In objective-C, in fact, like all programming languages, the plus sign (+) is used when two numbers are added, and the minus sign (-) is used when two numbers are subtracted (-), use the multiplication sign (*) When two numbers are multiplied, and use the division sign (/) When two numbers are divided (/). These operators are called Binary Arithmetic Operators because they calculate two values or items.
Operator priority
You have seen how to execute simple operations in objective-C, such as addition. The following procedures further illustrate subtraction, multiplication, and Division operations. The last two operations executed in a program introduce a concept that an operator has a higher priority than another operator. In fact, every operator in objective-C has a priority related to it.
Priority is used to determine how an expression that contains multiple operators evaluate: An operator with a higher priority is first evaluated. If the expression contains operators with the same priority, you can evaluate the value from left to right or from right to left. The specific direction of the evaluation depends on the operator. This is commonly referred to as the operator concatenation.
Code list 4-2
// Describes the usage of various Arithmetic Operators
# Import <Foundation/Foundation. h>
Int main (INT argc, char * argv [])
{
@ Autoreleasepool {
Int A = 100;
Int B = 2;
Int c = 25;
Int d = 4;
Int result;
Result = A-B; // Subtraction
Nslog (@ "a-B = % I", result );
Result = B * C; // Multiplication
Nslog (@ "B * c = % I", result );
Result = A/C; // Division
Nslog (@ "A/C = % I", result );
Result = a + B * C; // priority
Nslog (@ "A + B * c = % I", result );
Nslog (@ "a * B + C * D = % I", a * B + C * D );
}
Return 0;
}
Code list 4-2 output
A-B = 98
B * c = 50
A/C = 4
A + B * c = 150
A * B + C * D = 300
After declaring the integer variables A, B, C, D, and result, the program assigns the result of A minus B to the result, and then displays its value with an appropriate nslog call.
Next statement
Result = B * C;
Multiply the value of B and the value of C and store the result in result. Then, nslog is called to display the result of this multiplication. So far, you should be familiar with this process.
The subsequent Program Statement introduces the Division operator-slash (/). 4 is obtained by dividing 100 by 25. The available nslog statement is displayed immediately after dividing a by C.
In some computer systems, trying to divide an integer by 0 will lead to abnormal program termination or exceptions. Even if the program does not terminate abnormally, the result of executing such division is meaningless. In Chapter 2 "select structure", you will see how to check whether the divisor is 0 before performing the division operation. If the divisor is 0, appropriate operations can be used to avoid Division operations.
Expression
A + B * C
No 2550 results (102 × 25) are generated. On the contrary, the nslog statement displays 150. This is because, like most programming languages, objective-C has its own rules for the order of multiple operations or items in expressions. Generally, the expression is calculated from left to right. However, the priority specified for multiplication and division operations is higher than that of addition and subtraction. Therefore, objective-C considers the expression
A + B * C
Equivalent
A + (B * C)
(If basic algebra rules are used, the formula is the same .)
To change the order in which the items are calculated, use parentheses. In fact, the expressions listed above are quite valid objective-C expressions. In this way, the available expression
Result = a + (B * C );
Replace the expression in the code listing 4-2 to get the same result. However, if expressions are used
Result = (a + B) * C;
To replace, the value assigned to the result will be 2550, because you must first add the value of A (100) and the value of B (2, then, multiply the result with the value (25) of C. Parentheses can also be nested. In this case, the calculation of the expression should start from the inside of a pair of parentheses. Make sure that the ending and starting parentheses are equal.
From the last statement in code list 4-2, you can find that when you specify an expression as a parameter for nslog, you do not need to assign the result of this expression to a variable. This method is completely legal. Expression
A * B + C * d
According to the above rules
(A * B) + (C * D)
That is
(100*2) + (25*4)
.
Result 300 is passed to the nslog function.
Integer Operation and negative number Operator
Code list 4-3 reinforces the content discussed earlier and introduces the concept of integer calculation.
Code list 4-3
// More arithmetic expressions
# Import <Foundation/Foundation. h>
Int main (INT argc, char * argv [])
{
@ Autoreleasepool {
Int A = 25;
Int B = 2;
Float c = 25.0;
Float d = 2.0;
Nslog (@ "6 + A/5 * B = % I", 6 + A/5 * B );
Nslog (@ "A/B * B = % I", a/B * B );
Nslog (@ "C/D * D = % F", C/D * D );
Nslog (@ "-A = % I",-);
}
Return 0;
}
Code list 4-3 Output
6 + A/5 * B = 16
A/B * B = 24
C/D * D = 25.000000
-A =-25
In the first three statements, an extra space is inserted between the declaration of int, A, B, and result to align the declaration of each variable, using this method to write statements makes the program easier to read. It can also be noted that in every program that has appeared so far, there are spaces before and after each operator. This approach is also not necessary, just for aesthetic considerations. Generally, an extra space can be inserted anywhere a single space is allowed. If you can make the program easier to read, the input space key operation is worth doing.
In code listing 4-3, the expression in the first nslog call reinforces the concept of operator priority. The expression is calculated in the following order:
(1) because the Division priority is higher than the addition method, divide the value of A (25) by 5. This operation provides the intermediate result 5.
(2) because the multiplication priority is higher than the addition, the intermediate result (5) is multiplied by 2 (that is, the value of B) and a new intermediate result (10) is obtained ).
(3) Calculate 6 + 10 and obtain the final result (16 ).
The second nslog statement introduces a new misunderstanding. If you want a to be divided by B and then multiplied by B, A (set to 25) is returned ). However, this operation does not produce this result. The output display is 24. Is the computer lost somewhere? It would be unfortunate. In fact, the actual situation of this problem is that this expression uses an integer operation to evaluate the value.
If you look back at the declarations of variables A and B, you will remember that they are declared as int types. When an expression that contains two integers is evaluated, the objective-C system uses the Integer Operation to perform this operation. In this case, all decimal parts of the number are lost. Therefore, when dividing a by B, that is, 25 by 2, the intermediate result is 12, rather than the expected 12.5. Multiply the intermediate result by 2 to get the final result 24. In this way, we will explain the situation of "missing" numbers.
In the penultimate nslog Statement of code listing 4-3, we can see that if we use a floating point value instead of an integer to perform the same operation, we will get the expected result.
Decide whether to use the float variable or Int variable based on the purpose of using the variable. Integer variables can be used without any decimal places. This will make the program more efficient, in other words, it can be executed more quickly on most computers. On the other hand, if you need to be precise to the decimal places, it is clear what to choose. In this case, the only question to be answered is float or double. The answer to this question depends on the precision required for using the data and their magnitude.
In the last nslog statement, the negative number operator is used to take the negative value of variable. This unary operator is used for a single value, while the binary operator acts on two values. A negative number actually plays a dual role. As a binary operator, it performs two subtraction operations. As a one-dimensional (or single-object) operator, it takes a negative value for a value.
Compared with other Arithmetic Operators, The unary minus sign operator has a higher priority, except the unary minus sign operator (+). The unary minus sign operator has the same priority as the arithmetic operator. Therefore, the expression
C =-A * B;
Multiply execution-A by B.
This article is excerpted from objective-C Programming (version 4th).
Published by Electronic Industry Publishing House
[Us] Stephen G. kochan (Stephen G. cochang)
Translated by Lin Ji fan Jun Zhu Yixin