+ 1

instead of break

for a for loop in java, what can be used as an alternative to break?

6th Oct 2021, 5:46 AM
snaj
8 odpowiedzi
+ 2
You could change the loop statement variable, to make it stop in next iteration, but other than that, no... Java removed "goto", because they though of it as "useless". How sad. 😔
6th Oct 2021, 6:34 AM
Aleksei Radchenkov
Aleksei Radchenkov - avatar
+ 1
There is no other equivalent keyword of break. What are you trying to achieve that break cannot be used?
6th Oct 2021, 6:09 AM
你知道規則,我也是
你知道規則,我也是 - avatar
+ 1
If your for loop is in a function you could use "return" but that exits the for loop AND the function.
6th Oct 2021, 6:30 AM
᠌᠌Code X
᠌᠌Code X - avatar
+ 1
Aleksei Radchenkov oh okay, thank you for the help!
6th Oct 2021, 3:19 PM
snaj
+ 1
snaj, No problem 👍 And happy learning!
7th Oct 2021, 2:23 AM
Aleksei Radchenkov
Aleksei Radchenkov - avatar
0
CarrieForle I am trying to find an object in a partially filled array by using a for loop. Once found, I can exit the loop. We’ve been asked not to use break.
6th Oct 2021, 2:14 PM
snaj
0
Aleksei Radchenkov what is a loop statement variable?
6th Oct 2021, 2:17 PM
snaj
0
snaj , by loop variable I mean the variable which you use for comparison for your loop.
6th Oct 2021, 2:49 PM
Aleksei Radchenkov
Aleksei Radchenkov - avatar