Computer Programming 简明教程
Computer Programming - Operators
编程语言中的运算符是告诉编译器或解释器执行特定的数学、关系或逻辑运算并生成最终结果的符号。本章将解释 operators 的概念,并将带您了解 C、Java 和 Python 中可用的重要算术和关系运算符。
An operator in a programming language is a symbol that tells the compiler or interpreter to perform specific mathematical, relational or logical operation and produce final result. This chapter will explain the concept of operators and it will take you through the important arithmetic and relational operators available in C, Java, and Python.
Arithmetic Operators
计算机程序被广泛用于数学计算。我们可以编写一个计算机程序,它可以进行简单的计算,例如对两个数字求和 (2 + 3),我们还可以编写一个程序,它可以解复杂的方程,如 P(x) = x^4 + 7x^3 - 5x + 9。如果您曾经是一个成绩差的学生,那么您一定知道在第一个表达式中 2 和 3 是操作数,而 + 是操作符。类似的概念存在于计算机编程中。
Computer programs are widely used for mathematical calculations. We can write a computer program which can do simple calculation like adding two numbers (2 + 3) and we can also write a program, which can solve a complex equation like P(x) = x4 + 7x3 - 5x + 9. If you have been even a poor student, you must be aware that in first expression 2 and 3 are operands and + is an operator. Similar concepts exist in Computer Programming.
观察以下两个示例 −
Take a look at the following two examples −
2 + 3
P(x) = x4 + 7x3 - 5x + 9.
这两个语句在编程语言中称为算术表达式,并且在这些表达式中使用的 plus 、 minus 称为算术运算符,而这些表达式中使用的值(如 2、3 和 x 等)称为操作数。在其最简单的形式中,此类表达式会产生数值结果。
These two statements are called arithmetic expressions in a programming language and plus, minus used in these expressions are called arithmetic operators and the values used in these expressions like 2, 3 and x, etc., are called operands. In their simplest form, such expressions produce numerical results.
类似地,编程语言提供了各种算术运算符。下表列出了 C 编程语言中可用的一些重要算术运算符。假设变量 A 持有 10,变量 B 持有 20,那么 −
Similarly, a programming language provides various arithmetic operators. The following table lists down a few of the important arithmetic operators available in C programming language. Assume variable A holds 10 and variable B holds 20, then −
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 编程示例,用于了解上述数学运算符 −
Following is a simple example of C Programming to understand the above mathematical operators −
#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);
}
当执行上述程序时,它将生成以下结果 −
When the above program is executed, it produces the following result −
Value of c = 30
Value of c = -10
Value of c = 200
Value of c = 2
Value of c = 0
Relational Operators
考虑一种情况,我们在其中创建两个变量并将一些值分配给它们,如下所示 −
Consider a situation where we create two variables and assign them some values as follows −
A = 20
B = 10
在此,很明显,变量 A 的值大于 B。因此,我们需要一些符号来编写此类表达式,这些符号称为关系表达式。如果我们使用 C 编程语言,那么将按如下方式编写 −
Here, it is obvious that variable A is greater than B in values. So, we need the help of some symbols to write such expressions which are called relational expressions. If we use C programming language, then it will be written as follows −
(A > B)
在此,我们使用了符号 >,它称为关系运算符,并且在其最简单的形式中,它们产生布尔结果,这意味着结果将是 true 或 false。类似地,编程语言提供了各种关系运算符。下表列出了 C 编程语言中可用的一些重要关系运算符。假设变量 A 持有 10,变量 B 持有 20,那么 −
Here, we used a symbol > and it is called a relational operator and in their simplest form, they produce Boolean results which means the result will be either true or false. Similarly, a programming language provides various relational operators. The following table lists down a few of the important relational operators available in C programming language. Assume variable A holds 10 and variable B holds 20, then −
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 conditional statement 的 C 编程示例。尽管将在单独的章节中讨论此语句,但简而言之,我们使用 if statement 来检查条件,如果条件为 true,那么将执行 if statement 的主体,否则将跳过 if statement 的主体。
Here, we will show you one example of C Programming which makes use of if conditional statement. Though this statement will be discussed later in a separate chapter, but in short, we use if statement to check a condition and if the condition is true, then the body of if statement is executed, otherwise the body of if statement is skipped.
#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");
}
}
当执行上述程序时,它将生成以下结果 −
When the above program is executed, it produces the following result −
a is equal to 10
a is less than b
a is not equal to b
Logical Operators
逻辑运算符在任何编程语言中都非常重要,它们帮助我们根据某些条件做出决策。假设我们要合并两个条件的结果,那么逻辑 AND 和 OR 逻辑运算符有助于我们产生最终结果。
Logical operators are very important in any programming language and they help us take decisions based on certain conditions. Suppose we want to combine the result of two conditions, then logical AND and OR logical operators help us in producing the final result.
下表显示了 C 语言支持的所有逻辑运算符。假设变量 A 持有 1,变量 B 持有 0,那么 −
The following table shows all the logical operators supported by the C language. Assume variable A holds 1 and variable B holds 0, then −
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 编程语言中可用的所有逻辑运算符 −
Try the following example to understand all the logical operators available in C programming language −
#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" );
}
}
当您编译和执行上述程序时,它将生成以下结果 −
When you compile and execute the above program, it produces the following result −
This will be printed print because condition is true
This will be printed print because condition is true
Operators in Java
以下是用 Java 编写的等效程序。C 编程和 Java 提供了几乎相同的运算符和条件语句组。此程序将创建两个变量 a 和 b ,非常类似于 C 编程,然后我们在这些变量中分配 10 和 20,最后,我们将使用不同的算术和关系运算符 −
Following is the equivalent program written in Java. C programming and Java provide almost identical set of operators and conditional statements. This program will create two variables a and b, very similar to C programming, then we assign 10 and 20 in these variables and finally, we will use different arithmetic and relational operators −
您可以尝试执行以下程序以查看输出,其必须与上述示例生成的结果相同。
You can try to execute the following program to see the output, which must be identical to the result generated by the above example.
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" );
}
}
}
当执行上述程序时,它将生成以下结果 −
When the above program is executed, it produces the following result −
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
Operators in Python
以下是用 Python 编写的等效程序。此程序将创建两个变量 a 和 b ,同时在这些变量中分配 10 和 20。幸运的是,C 编程和 Python 编程语言提供了几乎相同的运算符组。此程序将创建两个变量 a 和 b ,非常类似于 C 编程,然后我们在这些变量中分配 10 和 20,最后,我们将使用不同的算术和关系运算符。
Following is the equivalent program written in Python. This program will create two variables a and b and at the same time, assign 10 and 20 in those variables. Fortunately, C programming and Python programming languages provide almost identical set of operators. This program will create two variables a and b, very similar to C programming, then we assign 10 and 20 in these variables and finally, we will use different arithmetic and relational operators.
您可以尝试执行以下程序以查看输出,其必须与上述示例生成的结果相同。
You can try to execute the following program to see the output, which must be identical to the result generated by the above example.
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"
当执行上述程序时,它将生成以下结果 −
When the above program is executed, it produces the following result −
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