C++ c+中的条件语句+;
我对在if语句块末尾执行语句感兴趣,但似乎我不能这样做。有办法吗? 例如:C++ c+中的条件语句+;,c++,C++,我对在if语句块末尾执行语句感兴趣,但似乎我不能这样做。有办法吗? 例如: int x = 0; if (x > -1) { cout << "I" << endl; else if(x > -2) cout <<"hope" <<endl; cout <<"this works" << endl; } intx=0; 如果(x>-1){ coutelse-if语句不是这样工作的。在这种情况下,您可能需要这样的
int x = 0;
if (x > -1) {
cout << "I" << endl;
else if(x > -2)
cout <<"hope" <<endl;
cout <<"this works" << endl;
}
intx=0;
如果(x>-1){
coutelse-if语句不是这样工作的。在这种情况下,您可能需要这样的语句
int x = 0;
if (x > -1) {
cout << "I" << endl;
if(x > -2){
cout <<"hope" <<endl;
}
cout <<"this works" << endl;
}
intx=0;
如果(x>-1){
coutintx=0;
如果(x>-1)
{
cout在看到您的代码后,如果您的要求符合两个条件,即打印我希望这能起作用
可以是
int x = 0;
if (x > -1) {
cout << "I" << endl;
cout <<"hope" <<endl;
cout <<"this works" << endl;
}
你的代码不应该编译:“else”没有匹配的“if”如果“if”不需要条件如果x>-2
,如果x>-1
,那么显然x
大于-2
显然在逻辑上没有意义,但是OP似乎只是在玩弄条件语句,我不认为他的程序是su他说得有道理。
int x = 0;
if (x > -1) {
cout << "I" << endl;
cout <<"hope" <<endl;
cout <<"this works" << endl;
}
int x = 0;
if (x > -2) {
cout << "I" << endl;
cout <<"hope" <<endl;
cout <<"this works" << endl;
}
int x = 0;
if (x > -1) {
cout << "I" << endl;
if(x > -2){
cout <<"hope" <<endl;
}
cout <<"this works" << endl;
}