C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
And: The compiler can figure this out before the program ever executes. It will generate an unreachable code detected warning.
C# program that shows unreachable code
using System;
class Program
{
static void Main()
{
while (false)
{
int value = 1;
if (value == 2)
{
throw new Exception();
}
}
}
}
Warning:
Warning CS0162: Unreachable code detected
Tip: With pragma warning disable, you can keep the code compiling but eliminate the error as well.
Pragma DirectiveC# program that uses pragma to disable warnings
using System;
class Program
{
static void Main()
{
#pragma warning disable
while (false)
{
int value = 1;
if (value == 2)
{
throw new Exception();
}
}
#pragma warning restore
}
}
And: If the end point of a statement is reachable, the statement itself is reachable.