instead of break | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
0

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 Respostas
+ 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
į Œį ŒBrains[Abidemi]
į Œį ŒBrains[Abidemi] - avatar
+ 1
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
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