本帖最後由 tonyh 於 2016-5-21 11:33 編輯
條件運算子通常運用在需要二選一的情況下,提供了一個更精簡快速的方法來撰寫條件式的陳述,可用來取代if-else陳述。
它的語法是:
條件判斷式 ? true陳述 : false陳述;- #include<iostream>
- #include<cstdlib>
- using namespace std;
- int main()
- {
- float age;
- string str="";
- re:
- cout<<"請輸入年齡: ";
- cin>>age;
- str=age>=18?"可以考駕照了":"未滿18";
- cout<<str<<endl<<endl;
- goto re;
- system("pause");
- return 0;
- }
複製代碼 |