Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.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+;中的三元运算符中不允许使用continue(关键字)+;?_C++ - Fatal编程技术网

C++ 在c+;中的三元运算符中不允许使用continue(关键字)+;?

C++ 在c+;中的三元运算符中不允许使用continue(关键字)+;?,c++,C++,代码如下:- #include<iostream> using namespace std; int main(){ int n, c1=0, ans=0; cin>>n; string s; cin>>s; for(int i=0; i<n; i++){//string always start with 0 s.at(i)!='D'?++c1:--c1;//statement 1 (c1!=0

代码如下:-

 #include<iostream>
 using namespace std;
 int main(){
   int n, c1=0, ans=0;
   cin>>n;
   string s;
   cin>>s;
   for(int i=0; i<n; i++){//string always start with 0
     s.at(i)!='D'?++c1:--c1;//statement 1
     (c1!=0 && s.at(i)!='U')?continue:ans++;//statement 2

  }
  cout<<ans<<endl;
}
但当我稍微更改语句2时,它不会提示任何错误

for(int i=0; i<n; i++){//string always start with 0
    s.at(i)!='D'?++c1:--c1;
    if(c1==0 && s.at(i)=='U')//statement 2
         ans++;      

for(int i=0;iNo,它不是。
continue
是一条语句,三元运算符中只允许表达式。

No,它不是。
continue
是一条语句,三元运算符中只允许表达式

  • 三元条件表达式的形式为

    E1?E2:E3

    其中,
    E1、E2和E3必须是表达式

  • 另一方面,是一种陈述

    continue
    语句会导致跳转,就像通过
    goto
    跳转到循环体的末尾一样(它可能只出现在for、range for、while和do while循环的循环体中

  • 总之,这意味着
    continue
    不能出现在三元条件中,即使该条件是上述循环之一的一部分

  • 三元条件表达式的形式为

    E1?E2:E3

    其中,
    E1、E2和E3必须是表达式

  • 另一方面,是一种陈述

    continue
    语句会导致跳转,就像通过
    goto
    跳转到循环体的末尾一样(它可能只出现在for、range for、while和do while循环的循环体中

  • 总之,这意味着
    continue
    不能出现在三元条件中,即使该条件是上述循环之一的一部分

    for(int i=0; i<n; i++){//string always start with 0
        s.at(i)!='D'?++c1:--c1;
        if(c1==0 && s.at(i)=='U')//statement 2
             ans++;