C
C
CSharpMutant2021-08-09 16:53:26
C++ / C#
CSharpMutant, 2021-08-09 16:53:26

Why return does not return 5, but restarts the loop?

I don't understand why in this code

static int SearchInsert(int[] nums, int target)
        {
            for (int i = 0; i < nums.Length; i++)
            {
                if (nums[i] >= target)
                {
                    return i;
                }
            }
            Console.WriteLine(nums.Length);
            return nums.Length;
        }

, return nums.Length starts the loop again, instead of exiting the method as I thought, can you chew me please?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question