Is there a way to define variables of two different types in a for loop initializer?

后端 未结 13 654
余生分开走
余生分开走 2020-12-19 06:12

You can define 2 variables of the same type in a for loop:

int main() {
  for (int i = 0, j = 0; i < 10; i += 1, j = 2*i) {
    cout << j << e         


        
13条回答
  •  隐瞒了意图╮
    2020-12-19 06:41

    This will make the iterator (or in this case, float) disappear when it's no more needed:

    int main() {
      // some code...
    
      {
        float j = 0.0;
        for (int i = 0; i < 10; i += 1, j = 2*i) {
          cout << j << endl;
        }
      }
    
      // more code...
    }
    

提交回复
热议问题