📜  计算机编程-操作员

📅  最后修改于: 2021-01-18 06:22:05             🧑  作者: Mango


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

算术运算符

计算机程序被广泛用于数学计算。我们可以写一个计算机程序,其可以做简单的计算,如添加两个数字(2 + 3),我们也可以编写一个程序,它可以解决像P(x)的一个复杂的方程式= X 4 + 7×3 – 5×+ 9。如果您甚至是一个贫穷的学生,您都必须意识到在第一个表达式中2和3是操作数,而+是运算符。计算机编程中也存在类似的概念。

看一下以下两个示例-

2 + 3

P(x) = x4 + 7x3 - 5x + 9. 

这两个语句在编程语言中称为算术表达式,而在这些表达式中使用的plus减号称为算术运算运算符,在这些表达式中使用的值(如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 

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,则-

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语句的主体。

#include 

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,则-

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. !(A && B) is true.

尝试以下示例以了解C编程语言中可用的所有逻辑运算符-

#include 

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