Valerie Meunier

Valerie Meunier

  • 968
  • 693
  • 73k

Why does this code not work (return into a loop)?

Aug 21 2021 7:09 PM

Hi ,

Why does this code not work? 

i starts with value 1 => 1%2 is not 0 so it should execute return "odd". In any case, a return should be executed. No?

Thanks

tatic void Main()
    {
        Console.WriteLine(test());
    }
 
    static string test()
    {
        for (int i = 1; i< 10; i++)
        {
            if (i % 2 == 0)
                return "even";
            else
                return "odd";
        }
    }

Answers (4)