介紹
“==” 是判斷兩個值大小是否相同的運算符。
代碼示範
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。
#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個對象時是比較他們引用的
地址。