計算機程式設計 - 運算子



程式語言中的運算子是一個符號,它告訴編譯器或直譯器執行特定的數學、關係或邏輯運算併產生最終結果。本章將解釋運算子的概念,並帶您瞭解 C、Java 和 Python 中可用的重要算術和關係運算符。

算術運算子

計算機程式被廣泛用於數學計算。我們可以編寫一個計算機程式來執行簡單的計算,例如將兩個數字相加 (2 + 3),我們也可以編寫一個程式來解決複雜的方程式,例如 P(x) = x4 + 7x3 - 5x + 9。即使你是一個差生,你也必須知道在第一個表示式中 2 和 3 是運算元,而 + 是運算子。類似的概念存在於計算機程式設計中。

請看以下兩個示例:

2 + 3

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

這兩個語句在程式語言中稱為算術表示式,而這些表示式中使用的加號減號稱為算術運算子,這些表示式中使用的值,如 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)

這裡,我們使用了符號 >,它稱為關係運算符,在最簡單的形式中,它們會產生布爾結果,這意味著結果要麼為真,要麼為假。類似地,程式語言提供了各種關係運算符。下表列出了 C 程式語言中一些重要的關係運算符。假設變數A為 10,變數B為 20,則:

運算子 描述 示例
== 檢查兩個運算元的值是否相等,如果相等則條件為真。 (A == B) 為假。
!= 檢查兩個運算元的值是否相等,如果不相等則條件為真。 (A != B) 為真。
> 檢查左運算元的值是否大於右運算元的值,如果大於則條件為真。 (A > B) 為假。
< 檢查左運算元的值是否小於右運算元的值,如果小於則條件為真。 (A < B) 為真。
>= 檢查左運算元的值是否大於或等於右運算元的值,如果大於或等於則條件為真。 (A >= B) 為假。
<= 檢查左運算元的值是否小於或等於右運算元的值,如果小於或等於則條件為真。 (A <= B) 為真。

在這裡,我們將向您展示一個使用if 條件語句的 C 程式設計示例。雖然此語句將在後面的單獨章節中討論,但簡而言之,我們使用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) 為真。
! 稱為邏輯 NOT 運算子。用於反轉其運算元的邏輯狀態。如果條件為真,則邏輯 NOT 運算子將使其變為假。 !(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
廣告

© . All rights reserved.