==

==

“==” 是計算機編程的二元運算符

當左邊的內容與右邊的內容相同時,返回"";

其餘時候返回0。

基本介紹

  • 中文名:==
  • 含義:計算機編程的二元運算符
介紹,代碼示範,

介紹

“==” 是判斷兩個值大小是否相同的運算符。

代碼示範

C++例子
第一個例子
#include <iostream>
using namespace std;
int main()
{
    int a,b;
    cin >> a >> b;
    if(a==b)
        cout << "a is equal to b" << endl;
    return 0;
}
這個程式輸入2個整數,判斷他們倆是否相等,如果相等,輸出a is equal to b。
第二個例子
#include <iostream>
#include <string>
using namespace std;
int main()
{
    string a,b;
    cin >> a >> b;
    if(a.length()==b.length())
        cout << "a is the same length as b" << endl;
    return 0;
}
這個程式輸入兩個字元串,判斷兩個字元串的長度如果相等輸出a is the same length as b。
可以將==的返回值輸出(C++):
#include <iostream>
using namespace std;
int main()
{
    cout << 1==1 << endl;//1=1,所以輸出true,也就是1
    cout << 1==2 << endl;//1不等於2,所以輸出false,也就是0
    return 0;
}
注意:在java中用==比較2個對象時是比較他們引用的地址

相關詞條

熱門詞條

聯絡我們