+ 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
8 Answers
+ 2
for (;i < 10 && num_found == false;){
+ 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
+ 2
visph
I think you forgot to remove Semicolon after i < 10
+ 2
🅰🅹 🅐🅝🅐🅝🅣 yes, thanks: corrected ;)
+ 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;) {
+ 2
🅰🅹 🅐🅝🅐🅝🅣 👍
+ 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:
....