Resolved "Unreachable code detected"

IAmNew

New member
Joined
Jan 28, 2025
Messages
1
Programming Experience
Beginner
Hello I am new to coding in C# and i keep geting the warning "Unreachable code detected" at line 19. This is my code:
C#:
using System;

public class Laguna
{

public static void Main(string[] args)
{
    //zmienne

    int Racje = 10;
    int Ludzie = 2;
    int Budynki = 1;



    //żeczy
    while (true); 
    {
        Console.WriteLine("Witamy w Laugunie");   //can't read this
        Console.WriteLine($"Racje:{Racje}");
        Console.WriteLine($"Populacja:{Ludzie}");
        Console.WriteLine($"Budynki:{Budynki}");
        Console.WriteLine( );
        Console.WriteLine("1:Eksploruj");
        Console.WriteLine("2:Zbuduj...");
        Console.WriteLine("3:Zobacz Statystyki");

        // odczytuje
        string OdczytanoWybor = Console.ReadLine();




        switch (OdczytanoWybor)
        {
            case "":
                Console.WriteLine("ERROR 404: not found");
                break;
                

            case "1":
                Console.WriteLine("Ile osób wysłać na eksploracje?");
                string OdczytanoPopulacje = Console.ReadLine();
                int test = int.Parse(OdczytanoPopulacje);
                if (test > Ludzie)
                {
                    Console.WriteLine("Za mało ludzi");
                    break;
                }
                else
                {
                    Random rnd = new Random();
                    int ran = rnd.Next(1, 4);


                    if (ran == 1)
                    {
                        Racje ++;
                        Console.WriteLine($"Znalazłeś 1 Racji. Aktualne Racje:{Racje}");
                        break;
                    }
                    if (ran == 2)
                    {
                        Racje ++;
                        Racje ++;
                        Racje ++;
                        Console.WriteLine($"Znalazłeś 3 Racji. Aktualne Racje:{Racje}");
                        break;
                    }
                    if (ran == 3)
                    {
                        Racje ++;
                        Racje ++;
                        Racje ++;
                        Racje ++;
                        Racje ++;
                        Console.WriteLine($"Znalazłeś 5 Racji. Aktualne Racje:{Racje}");
                        break;
                    }
                    if (ran == 4)
                    {
                        Racje ++;
                        Racje ++;
                        Racje ++;
                        Racje ++;
                        Racje ++;
                        Racje ++;
                        Racje ++;
                        Racje ++;
                        Racje ++;
                        Racje ++;
                        Console.WriteLine($"Znalazłeś 10 Racji. Aktualne Racje:{Racje}");
                        break;
                    }

                }
                
            
        }    
            

            Console.ReadKey();
    }
}

}
 
Last edited by a moderator:
Notice the semicolon at the end of the while statement on line 17. That would cause an infinite loop because the condition is always true. The compiler is letting you know that as best as it can tell, you won't get past line 17, and so everything past it is unreachable.
 
Back
Top Bottom