目录

Computer Programming - 运算符

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

算术运算符 (Arithmetic Operators)

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

看看以下两个例子 -

2 + 3
P(x) = x<sup>4</sup> + 7x<sup>3</sup> - 5x + 9. 

这两个语句在编程语言中称为算术表达式,而在这些表达式中使用的minus称为算术运算符,这些表达式中使用的值如2,3和x等称为操作数。 在它们最简单的形式中,这样的表达式产生数值结果。

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

操作者 描述
+ 添加两个操作数 A + B将给出30
- 从第一个减去第二个操作数 A - B将给-10
* 将两个操作数相乘 A * B将给出200
/Divides numerator by de-numerator 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

关系运算符 (Relational Operators)

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

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)是真的。

在这里,我们将向您展示一个使用if conditional statement的C编程示例。 虽然稍后将在单独的章节中讨论此语句,但简而言之,我们使用if statement来检查条件,如果条件为真,则执行if statement的主体,否则跳过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

逻辑运算符 (Logical Operators)

逻辑运算符在任何编程语言中都非常重要,它们可以帮助我们根据特定条件做出决策。 假设我们想要结合两个条件的结果,那么逻辑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提供了几乎相同的运算符和条件语句集。 这个程序将创建两个变量ab ,非常类似于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编写的等效程序。 该程序将创建两个变量ab ,同时在这些变量中分配10和20。 幸运的是,C编程和Python编程语言提供了几乎相同的运算符集。 这个程序将创建两个变量ab ,非常类似于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"

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

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
↑回到顶部↑
WIKI教程 @2018