R
R
ReD2016-05-09 15:03:22
Programming
ReD, 2016-05-09 15:03:22

How to correctly write condition fulfillment at certain iterations of a loop in C++?

Hello!
Please tell me how to write the following condition:

int main(){
bool D=false;
for (int i=1; i<10; i++)
{
  for (int j=1; j<10; j++)
  {
    for (int k=1; k<10; k++)
    {
      if ( i==3 && j==2 && j==3 && j==4 && k==4 ){
         D=true;
         cout << D << endl;
      }
    }
  }
}
return 0;
}

When executed, nothing is output, that is, the condition is not met.
But when I write like this:
int main(){
bool D=false;
for (int i=1; i<10; i++)
{
  for (int j=1; j<10; j++)
  {
    for (int k=1; k<10; k++)
    {
      if ( i==3 && j==2 && j==3 && j==4 && k==4 ){
         D=true;
         cout << D << endl;
      }
    }
  }
}
 return 0;
}

In this case, everything is correct, "1" is displayed.
How to correctly write a condition in a nested loop for a match with several values ​​of one of the nested loop counters at once?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Bogachev, 2016-05-09
@trinitr0

Perhaps you meant

if ( i==3 && (j==2 || j==3 || j==4) && k==4 ) {
    // ...
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question