Show 例子 1

优质
小牛编辑
139浏览
2023-12-01

下表显示了Objective-C语言支持的所有算术运算符。 假设变量A保持10,变量B保持20,则 -

操作者描述
+添加两个操作数A + B将给出30
-从第一个减去第二个操作数A - B将给-10
*将两个操作数相乘A * B将给出200
/Divides numerator by denominatorB/A会给2
%模数运算符和整数除法后的余数B%A将给出0
++递增运算符将整数值增加1A ++将给出11
--递减运算符将整数值减1A-- will give 9

例子 (Example)

尝试以下示例来了解Objective-C编程语言中可用的所有算术运算符 -

#import <Foundation/Foundation.h>
int main() {
   int a = 21;
   int b = 10;
   int c ;
   c = a + b;
   NSLog(@"Line 1 - Value of c is %d\n", c );
   c = a - b;
   NSLog(@"Line 2 - Value of c is %d\n", c );
   c = a * b;
   NSLog(@"Line 3 - Value of c is %d\n", c );
   c = a/b;
   NSLog(@"Line 4 - Value of c is %d\n", c );
   c = a % b;
   NSLog(@"Line 5 - Value of c is %d\n", c );
   c = a++; 
   NSLog(@"Line 6 - Value of c is %d\n", c );
   c = a--; 
   NSLog(@"Line 7 - Value of c is %d\n", c );
}

编译并执行上述程序时,会产生以下结果 -

2013-09-07 22:10:27.005 demo[25774] Line 1 - Value of c is 31
2013-09-07 22:10:27.005 demo[25774] Line 2 - Value of c is 11
2013-09-07 22:10:27.005 demo[25774] Line 3 - Value of c is 210
2013-09-07 22:10:27.005 demo[25774] Line 4 - Value of c is 2
2013-09-07 22:10:27.005 demo[25774] Line 5 - Value of c is 1
2013-09-07 22:10:27.005 demo[25774] Line 6 - Value of c is 21
2013-09-07 22:10:27.005 demo[25774] Line 7 - Value of c is 22