计算机编程 - 运算符

编程语言中的运算符是一个符号,它告诉编译器或解释器执行特定的数学,关系或逻辑运算并产生最终结果.本章将解释运算符的概念,它将指导您完成C,Java和Python中可用的重要算术和关系运算符.

算术运算符

计算机程序广泛用于数学计算.我们可以编写一个计算机程序,可以进行简单的计算,如添加两个数字(2 + 3),我们也可以编写一个程序,它可以解决复杂的方程,如P(x)= x 4 + 7x 3  -  5x + 9.如果你甚至是一个贫穷的学生,你必须要知道,在第一个表达式2和3中是操作数,+是操作符.计算机编程中也存在类似的概念.

看看以下两个例子 :

 
 2 + 3 
 P(x)= x  + 7x   -  5x + 9.

这两个语句在编程语言中称为算术表达式,在这些表达式中使用的 plus minus 称为算术运算符,这些中使用的值像2,3和x等表达式被称为操作数.这些表达式以其最简单的形式产生数值结果.

类似地,编程语言提供各种算术运算符.下表列出了C编程语言中可用的一些重要算术运算符.假设变量A保持10,变量B保持20,然后 : 去;

运算符描述示例
+添加两个操作数A + B将给出30
-从第一个减去第二个操作数A  -  B将给-10
*将两个操作数相乘A * B将给出200
/将分子除以除分子B/A将给出2
这给出了整数除法的余数B%A将给出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

关系运算符

考虑一种情况,我们创建两个变量并为它们分配一些值如下 :

 
 A = 20 
 B = 10

在这里,它很明显,变量A的值大于B值.因此,我们需要一些符号的帮助来编写这样的表达式,这些表达式称为关系表达式.如果我们使用C编程语言,那么它将写成如下 :

 
(A> B)

在这里,我们使用了一个符号>它被称为关系运算符,它们以最简单的形式产生布尔结果,这意味着结果将为true或false.类似地,编程语言提供各种关系运算符.下表列出了C编程语言中可用的一些重要关系运算符.假设变量 A 持有10,变量 B 持有20,然后是 :

运算符描述示例
==检查两个操作数的值是否相等,如果是,则条件变为真.(A == B)不是真的.
!=检查两个值是否为操作数相等或不相等,如果值不相等则条件变为真.(A!= B)为真.
>检查左操作数的值是否大于右操作数的值,如果是,则条件成立.(A> B)不成立.
<检查左操作数的值是否小于右操作数的值,如果是,则条件变为真.(A< B)为真.
> =检查左操作数的值是否大于或等于右操作数的值,如果是,则条件变为真.(A> = B)不成立.
< =检查值是否为左操作数的值小于或等于右操作数的值,如果是,则条件变为真.(A< = B)为真.

在这里,我们将向您展示一个C编程示例,它使用 if条件语句 .虽然这个陈述将在后面的单独章节中讨论,但简而言之,我们使用 if语句来检查条件,如果条件为真,那么 if语句的主体被执行,否则将跳过 if语句的正文.

#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

逻辑运算符

逻辑运算符在任何编程语言中都非常重要,它们可以帮助我们根据特定条件做出决策.假设我们想要结合两个条件的结果,那么逻辑AND和OR逻辑运算符可以帮助我们生成最终结果.

下表显示了C语言支持的所有逻辑运算符.假设变量 A 保持1而变量 B 保持0,然后是 :

运算符说明示例
&&被称为逻辑AND运算符.如果两个操作数都不为零,则条件变为真.(A&& B)为假.
||被称为逻辑OR运算符.如果两个操作数中的任何一个非零,则条件变为真.(A || B)为真.
被称为逻辑非运算符.用于反转其操作数的逻辑状态.如果条件为真,那么Logical NOT运算符将为false.!(A&& B)为真.

尝试以下示例来理解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提供了几乎相同的运算符和条件语句集.这个程序将创建两个变量 a b ,非常类似于C编程,然后我们在这些变量中分配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编写的等效程序.该程序将创建两个变量 a b ,同时在这些变量中分配10和20.幸运的是,C编程和Python编程语言提供了几乎相同的运算符集.这个程序将创建两个变量 a b ,非常类似于C编程,然后我们在这些变量中分配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"

当上述情况程序执行后,产生以下r esult :

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