W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
C++提供了六個(gè)關(guān)系運(yùn)算符來(lái)比較數(shù)字。
運(yùn)算符 | 含義 |
---|---|
< | 小于 |
<= | 小于或等于 |
== | 等于 |
> | 大于 |
>= | 大于或等于 |
!= | 不等于 |
#include <iostream>
#include <string> // string class
int main()
{
using namespace std;
string word = "?ate";
for (char ch = "a"; word != "mate"; ch++)
{
cout << word << endl;
word[0] = ch;
}
cout << "After loop ends, word is " << word << endl;
// cin.get();
return 0;
}
上面的代碼生成以下結(jié)果。
使用if語(yǔ)句,關(guān)系運(yùn)算符和等式運(yùn)算符比較整數(shù)。
#include <iostream> // allows program to perform input and output
using std::cout; // program uses cout
using std::cin; // program uses cin
using std::endl; // program uses endl
int main() {
int number1; // first integer to compare
int number2; // second integer to compare
cout << "Enter two integers to compare: "; // prompt user for data
cin >> number1 >> number2; // read two integers from user
if ( number1 == number2 )
cout << number1 << " == " << number2 << endl;
if ( number1 != number2 )
cout << number1 << " != " << number2 << endl;
if ( number1 < number2 )
cout << number1 << " < " << number2 << endl;
if ( number1 > number2 )
cout << number1 << " > " << number2 << endl;
if ( number1 <= number2 )
cout << number1 << " <= " << number2 << endl;
if ( number1 >= number2 )
cout << number1 << " >= " << number2 << endl;
}
上面的代碼生成以下結(jié)果。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: