Show 例子 1

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

CoffeeScript支持以下算术运算符。 假设变量A保持10 ,变量B保持20 ,则 -

Sr.No运算符和描述
1

+ (Addition)

添加两个操作数

A + B = 30
2

− (Subtraction)

从第一个中减去第二个操作数

A - B = -10
3

* (Multiplication)

将两个操作数相乘

A * B = 200
4

/ (Division)

用分母除以分子

B/A = 2
5

% (Modulus)

输出整数除法的余数

B%A = 0
6

++ (Increment)

将整数值增加1

A ++ = 11
7

-- (Decrement)

将整数值减1

A-- = 9

例子 (Example)

以下示例显示如何在CoffeeScript中使用算术运算符。 将此代码保存在名为arithmetic_example.coffee的文件中

a = 33
b = 10
c = "test"
console.log "The value of a + b = is"
result = a + b
console.log result
result = a - b
console.log "The value of a - b = is "
console.log result
console.log "The value of a/b = is"
result = a/b
console.log result
console.log "The value of a % b = is"
result = a % b
console.log result
console.log "The value of a + b + c = is"
result = a + b + c
console.log result
a = ++a
console.log "The value of ++a = is"
result = ++a
console.log result
b = --b
console.log "The value of --b = is"
result = --b
console.log result

打开command prompt并编译.coffee文件,如下所示。

c:\> coffee -c arithmetic_example.coffee

在编译时,它为您提供以下JavaScript。

// Generated by CoffeeScript 1.10.0
(function() {
  var a, b, c, result;
  a = 33;
  b = 10;
  c = "test";
  console.log("The value of a + b = is");
  result = a + b;
  console.log(result);
  result = a - b;
  console.log("The value of a - b = is ");
  console.log(result);
  console.log("The value of a/b = is");
  result = a/b;
  console.log(result);
  console.log("The value of a % b = is");
  result = a % b;
  console.log(result);
  console.log("The value of a + b + c = is");
  result = a + b + c;
  console.log(result);
  a = ++a;
  console.log("The value of ++a = is");
  result = ++a;
  console.log(result);
  b = --b;
  console.log("The value of --b = is");
  result = --b;
  console.log(result);
}).call(this);

现在,再次打开command prompt并运行CoffeeScript文件,如下所示。

c:\> coffee arithmetic_example.coffee

执行时,CoffeeScript文件生成以下输出。

The value of a + b = is
43
The value of a - b = is
23
The value of a/b = is
3.3
The value of a % b = is
3
The value of a + b + c = is
43test
The value of ++a = is
35
The value of --b = is
8