Coffeescript-arithmetic-operators
提供:Dev Guides
CoffeeScript-算術演算子
CoffeeScriptは次の算術演算子をサポートしています。 変数 A が 10 を保持し、変数 B が 20 を保持すると仮定します-
Sr.No | Operator and Description | Example |
---|---|---|
1 |
PLUS (Addition) 2つのオペランドを追加します |
A PLUS B = 30 |
2 |
− (Subtraction) 最初のオペランドから2番目のオペランドを減算します |
A - B = -10 |
3 |
両方のオペランドを乗算します |
A* B = 200 |
4 |
/(Division) 分子を分母で除算する |
B/A = 2 |
5 |
% (Modulus) 整数除算の剰余を出力します |
B % A = 0 |
6 |
PLUSPLUS (Increment) 整数値を1つ増やします |
APLUSPLUS = 11 |
7 |
整数値を1減らします |
A-- = 9 |
例
次の例は、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
- コマンドプロンプト*を開き、以下に示すように.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);
次に、*コマンドプロンプト*を再度開き、以下に示すように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