Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ c+中的条件语句+;_C++ - Fatal编程技术网

C++ c+中的条件语句+;

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语句不是这样工作的。在这种情况下,您可能需要这样的

我对在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){
cout
intx=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;
}