Many good reasons about how the code looks like. But what about results?
Let's take a look to some C# code and its IL compiled form:
using System;
public class Test {
public static void Main(string[] args) {
if (args.Length == 0) return;
if ((args.Length+2)/3 == 5) return;
Console.WriteLine("hey!!!");
}
}
This simple snippet can be compiled. You can open the generated .exe
file with ildasm
and check what is the result. I won't post all the assembler thing but I'll describe the results.
The generated IL code does the following:
false
, jumps to the code where the second is.true
jumps to the last instruction. (Note: the last instruction is a return).Console.WriteLine
if false
or to the end if this is true
.So it seems that the code will jump to the end. What if we do a normal if with nested code?
using System;
public class Test {
public static void Main(string[] args) {
if (args.Length != 0 && (args.Length+2)/3 != 5)
{
Console.WriteLine("hey!!!");
}
}
}
The results are quite similar in IL instructions. The difference is that before there were two jumps per condition: if false
go to next piece of code, if true
go to the end. And now the IL code flows better and has 3 jumps (the compiler optimized this a bit):
false
, jump to the end.Anyway, the program counter will always jump.