There are some Do's and Don'ts while writing a code in programming language to maintain the time complexity and space complexity in your code.
While using the If and If...else conditions you can minimize the number of returns in the loop by following some protocol.
//You should Avoid the use of if condition in this manner
//But Instead of above method you can do as shown in the below image :
Thank You!