Is
while (k >= 0 && arr[k] > 0)
safe?
It loop when k in range and arr[k] > 0. But I don’t know if this is a good practice of coding. I know that if we do this
while (arr[k] > 0 && k >= 0)
it would be a disaster.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It is safe in the fact that
&&short circuits. Ifkis less than zero, it won’t index intoarrwith it.It might not be safe in that the body of your while loop might not decrement
kcorrectly, leading to an infinite loop. Or it might not be safe ifkis beyondarr‘s bounds.