4
Answers

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

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)