Explore Courses Blog Tutorials Interview Questions
0 votes
in Java by (13.1k points)
I was trying to iterate over an array, whenever it get the element it will return the index i and otherwise it will return -1. Can anyone help me how I can able to do that?

1 Answer

0 votes
by (26.7k points)

Basically, if you want to stop a for loop, you can use the break statement:

var remSize = [], 





/* ...I assume there's code here putting entries in `remSize` and assigning something to `remData`... */

remIndex = -1; // Set a default if we don't find it

for (i = 0; i < remSize.length; i++) {      

     // I'm looking for the index i, when the condition is true

     if (remSize[i].size === remData.size) {

          remIndex = i;

          break;       // <=== breaks out of the loop early



I hope this will help.

Want to become a Java Expert? Join Java Course now!!

Want to know more about Java? Watch this video on Java Course | Java Tutorial for Beginners:

Related questions

0 votes
1 answer
asked Feb 19, 2021 in Python by laddulakshana (16.4k points)
0 votes
1 answer
asked Jan 8, 2021 in SQL by Appu (6.1k points)
0 votes
1 answer

Browse Categories