I am still a beginner and I’m just wondering if my loop has any other ways I can write it for learning sake. Here is my loop:
int count = 0;
int index = 0;
while(index >= 0 && index < arr.length && count < 100){
index = arr[index];
count ;
}
CodePudding user response:
you can use for loop
for that mission, so you can write:
for (int count = 0, index = 0; index >= 0 && index < arr.length && count < 100 ;count )
{
index = arr[index];
}
but notice that in your for loop, you are not updating the value of index
, by which I mean that index
is always 0 even in your original code
CodePudding user response:
A great alternative in this case would be a for loop. As you'll notice while coding when using multiple conditions to a loop it is best to use a for loop because it will look much neater and for loops already require three conditions perfect for your case.
while ( condition && condition && condition)
vs
for ( condition ; condition ; condition)
Upvote if this helped thanks.