+ 1

[Solved]Can you break out or exit of a "for loop"without the use of break keyword

I could exit a "while loop" by setting flag https://code.sololearn.com/cZ3TmuYb3Qgd/?ref=app But I tried with a "for loop" and it is not working https://code.sololearn.com/c7DJpfQ1Q3fh/?ref=app

27th Jun 2021, 3:34 PM
!Derrickee
!Derrickee - avatar
8 ответов
+ 2
for (;i < 10 && num_found == false;){
27th Jun 2021, 3:42 PM
visph
visph - avatar
+ 3
!Derrickee You need to change condition in for loop and also (num_found == true) is wrong as an assignment https://code.sololearn.com/cm9Q8XPyNIUp/?ref=app
27th Jun 2021, 3:45 PM
A͢J
A͢J - avatar
+ 2
Thank you visph and 🅰🅹 🅐🅝🅐🅝🅣 bro 😁
27th Jun 2021, 4:20 PM
!Derrickee
!Derrickee - avatar
+ 2
visph I think you forgot to remove Semicolon after i < 10
27th Jun 2021, 4:22 PM
A͢J
A͢J - avatar
+ 2
🅰🅹 🅐🅝🅐🅝🅣 yes, thanks: corrected ;)
27th Jun 2021, 4:29 PM
visph
visph - avatar
+ 2
!Derrickee Though there is nothing wrong in this type (num_found == false) of comparison but I would prefer !num_found like this: for (;i < 10 && !num_found;) {
27th Jun 2021, 4:33 PM
A͢J
A͢J - avatar
27th Jun 2021, 4:47 PM
!Derrickee
!Derrickee - avatar
+ 1
Yes, there is also another options. You ca use "goto". But people dont use that, because its historical. Exaple: For(int i=0;i<n;i++) { If(conditions) goto end; } Console.WriteLine("loop ends corectly"); end: ....
29th Jun 2021, 7:00 AM
Filip Dobeš