Computer-programming-operators
コンピュータープログラミング-オペレーター
プログラミング言語の演算子は、特定の数学、関係、または論理演算を実行して最終結果を生成するようにコンパイラーまたはインタープリターに指示する記号です。 この章では、*演算子*の概念について説明し、C、Java、Pythonで使用可能な重要な算術演算子と関係演算子について説明します。
算術演算子
コンピュータプログラムは、数学計算に広く使用されています。 2つの数値(2 + 3)を加算するなどの簡単な計算ができるコンピュータープログラムを作成できます。また、P(x)= x ^ 4 ^ + 7x ^ 3 ^などの複雑な方程式を解くプログラムを作成することもできます。 5x + 9。 あなたが貧しい学生であっても、最初の式2と3はオペランドであり、+は演算子であることに注意する必要があります。 同様の概念がコンピュータープログラミングにも存在します。
次の2つの例を見てみましょう-
2 + 3
P(x) = x4 + 7x3 - 5x + 9.
これら2つのステートメントはプログラミング言語では算術式と呼ばれ、これらの式で使用される plus 、 minus は算術演算子と呼ばれ、2、3、xなどのこれらの式で使用される値はオペランドと呼ばれます。 最も単純な形式では、そのような式は数値結果を生成します。
同様に、プログラミング言語はさまざまな算術演算子を提供します。 次の表に、Cプログラミング言語で使用できる重要な算術演算子の一部を示します。 変数Aが10を保持し、変数Bが20を保持すると仮定します-
Operator | Description | Example |
---|---|---|
+ | Adds two operands | A + B will give 30 |
- | Subtracts second operand from the first | A - B will give -10 |
* | Multiplies both operands | A* B will give 200 |
/ | Divides numerator by de-numerator | B/A will give 2 |
% | This gives remainder of an integer division | B % A will give 0 |
以下は、上記の数学演算子を理解するためのCプログラミングの簡単な例です-
#include <stdio.h>
int main() {
int a, b, c;
a = 10;
b = 20;
c = a + b;
printf( "Value of c = %d\n", c);
c = a - b;
printf( "Value of c = %d\n", c);
c = a * b;
printf( "Value of c = %d\n", c);
c = b/a;
printf( "Value of c = %d\n", c);
c = b % a;
printf( "Value of c = %d\n", c);
}
上記のプログラムが実行されると、次の結果が生成されます-
Value of c = 30
Value of c = -10
Value of c = 200
Value of c = 2
Value of c = 0
関係演算子
次のように2つの変数を作成し、それらにいくつかの値を割り当てる状況を考えてください-
A = 20
B = 10
ここで、変数Aの値がBより大きいことは明らかです。 そのため、リレーショナル式と呼ばれるこのような式を作成するには、いくつかのシンボルの助けが必要です。 私たちはCプログラミング言語を使用する場合、それは次のように書かれます-
(A > B)
ここでは、シンボル>を使用しました。これは関係演算子と呼ばれ、最も単純な形式ではブール結果を生成します。つまり、結果はtrueまたはfalseになります。 同様に、プログラミング言語はさまざまな関係演算子を提供します。 次の表に、Cプログラミング言語で使用できる重要な関係演算子のいくつかを示します。 変数 A が10を保持し、変数 B が20を保持すると仮定します-
Operator | Description | Example |
---|---|---|
== | Checks if the values of two operands are equal or not, if yes then condition becomes true. | (A == B) is not true. |
!= | Checks if the values of two operands are equal or not, if values are not equal then condition becomes true. | (A != B) is true. |
> | Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. | (A > B) is not true. |
< | Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. | (A < B) is true. |
>= | Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. | (A >= B) is not true. |
⇐ | Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. | (A ⇐ B) is true. |
ここでは、* if条件ステートメント*を使用するCプログラミングの例を示します。 このステートメントについては別の章で説明しますが、要するに、* ifステートメント*を使用して条件をチェックし、条件がtrueの場合は* ifステートメント*の本体が実行され、そうでない場合は ifの本体が実行されますstatement はスキップされます。
#include <stdio.h>
int main() {
int a, b;
a = 10;
b = 20;
/*Here we check whether a is equal to 10 or not*/
if( a == 10 ) {
/*if a is equal to 10 then this body will be executed*/
printf( "a is equal to 10\n");
}
/*Here we check whether b is equal to 10 or not*/
if( b == 10 ) {
/*if b is equal to 10 then this body will be executed*/
printf( "b is equal to 10\n");
}
/*Here we check if a is less b than or not*/
if( a < b ) {
/*if a is less than b then this body will be executed*/
printf( "a is less than b\n");
}
/*Here we check whether a and b are not equal*/
if( a != b ) {
/*if a is not equal to b then this body will be executed*/
printf( "a is not equal to b\n");
}
}
上記のプログラムが実行されると、次の結果が生成されます-
a is equal to 10
a is less than b
a is not equal to b
論理演算子
論理演算子はどのプログラミング言語でも非常に重要であり、特定の条件に基づいて決定を下すのに役立ちます。 2つの条件の結果を結合したい場合、論理ANDおよびOR論理演算子が最終結果の生成に役立ちます。
次の表は、C言語でサポートされているすべての論理演算子を示しています。 変数 A が1を保持し、変数 B が0を保持すると仮定します-
Operator | Description | Example |
---|---|---|
&& | Called Logical AND operator. If both the operands are non-zero, then condition becomes true. | (A && B) is false. |
Called Logical OR Operator. If any of the two operands is non-zero, then condition becomes true. | (A | |
B) is true. | ! | Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. |
Cプログラミング言語で利用可能なすべての論理演算子を理解するために、次の例を試してください-
#include <stdio.h>
int main() {
int a = 1;
int b = 0;
if ( a && b ) {
printf("This will never print because condition is false\n" );
}
if ( a || b ) {
printf("This will be printed print because condition is true\n" );
}
if ( !(a && b) ) {
printf("This will be printed print because condition is true\n" );
}
}
上記のプログラムをコンパイルして実行すると、次の結果が生成されます-
This will be printed print because condition is true
This will be printed print because condition is true
Javaの演算子
以下は、Javaで作成された同等のプログラムです。 CプログラミングとJavaは、ほぼ同一の演算子と条件ステートメントのセットを提供します。 このプログラムは、Cプログラミングに非常によく似た2つの変数 a および b を作成し、これらの変数に10と20を割り当て、最後に異なる算術演算子と関係演算子を使用します-
次のプログラムを実行して出力を確認できます。出力は、上記の例で生成された結果と同一である必要があります。
public class DemoJava {
public static void main(String []args) {
int a, b, c;
a = 10;
b = 20;
c = a + b;
System.out.println("Value of c = " + c );
c = a - b;
System.out.println("Value of c = " + c );
c = a * b;
System.out.println("Value of c = " + c );
c = b/a;
System.out.println("Value of c = " + c );
c = b % a;
System.out.println("Value of c = " + c );
if( a == 10 ) {
System.out.println("a is equal to 10" );
}
}
}
上記のプログラムが実行されると、次の結果が生成されます-
Value of c = 30
Value of c = -10
Value of c = 200
Value of c = 2
Value of c = 0
a is equal to 10
Pythonの演算子
以下は、Pythonで作成された同等のプログラムです。 このプログラムは、2つの変数 a と b を作成し、同時にそれらの変数に10と20を割り当てます。 幸いなことに、Cプログラミング言語とPythonプログラミング言語は、ほぼ同一の演算子セットを提供します。 このプログラムは、Cプログラミングに非常によく似た2つの変数 a と b を作成し、これらの変数に10と20を割り当て、最後に異なる算術演算子と関係演算子を使用します。
次のプログラムを実行して出力を確認できます。出力は、上記の例で生成された結果と同一である必要があります。
a = 10
b = 20
c = a + b
print "Value of c = ", c
c = a - b
print "Value of c = ", c
c = a * b
print "Value of c = ", c
c = a/b
print "Value of c = ", c
c = a % b
print "Value of c = ", c
if( a == 10 ):
print "a is equal to 10"
上記のプログラムが実行されると、次の結果が生成されます-
Value of c = 30
Value of c = -10
Value of c = 200
Value of c = 0
Value of c = 10
a is equal to 10