Multiple conditions in for loop c++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Multiple conditions in for loop c++

Can I put conditions which contain (&& , || ) in a for loop? i.e : for(i=1 ; x>3 && !test ; i++ )

6th Jun 2018, 6:53 PM
Eslam Khaled
Eslam Khaled - avatar
2 Answers
+ 4
In the beginning of your loop, do an IF condition for !test. If it's test, then 'break;' the loop to end it. Get what I mean? That's best way to go about it. However, to properly answer you, yes you can do that. I'd organize things with the expectations that others will use it also and so things should be readable to the best of your ability. https://code.sololearn.com/c7KokF2VMwaf/#cpp #include <iostream> using namespace std; int main() { bool test = false; for(int i = 0; i < 3 && !test; ++i) { cout << "Test!"; test = true; } return 0; }
6th Jun 2018, 6:55 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 2
thank you guys
6th Jun 2018, 7:02 PM
Eslam Khaled
Eslam Khaled - avatar