Adr*_*scu 3 c# loops goto do-while
以下2个C#片段之间的执行存在差异吗?
do
{
Console.WriteLine(x.ToString());
++x;
}
while (x < 7);
Run Code Online (Sandbox Code Playgroud)
和
label:
{
Console.WriteLine(x.ToString());
++x;
}
if (x < 7) goto label;
Run Code Online (Sandbox Code Playgroud)
我想弄清楚为什么这么糟糕.谢谢.
编辑:如果我添加括号,片段非常相似.
EDIT2:在Visual Studio中,我点击了Go to Disassembly,我得到以下代码:
do
{
00000037 nop
Console.WriteLine(x.ToString());
00000038 lea ecx,[ebp-40h]
0000003b call 63129C98
00000040 mov dword ptr [ebp-48h],eax
00000043 mov ecx,dword ptr [ebp-48h]
00000046 call 63148168
0000004b nop
++x;
0000004c inc dword ptr [ebp-40h]
}
0000004f nop
while (x < 7);
00000050 cmp dword ptr [ebp-40h],7
00000054 setl al
00000057 movzx eax,al
0000005a mov dword ptr [ebp-44h],eax
0000005d cmp dword ptr [ebp-44h],0
00000061 jne 00000037
Run Code Online (Sandbox Code Playgroud)
和
label:
{
Console.WriteLine(x.ToString());
00000069 lea ecx,[ebp-40h]
0000006c call 63129C98
00000071 mov dword ptr [ebp-4Ch],eax
00000074 mov ecx,dword ptr [ebp-4Ch]
00000077 call 63148168
0000007c nop
++x;
0000007d inc dword ptr [ebp-40h]
}
00000080 nop
if (x < 7) goto label;
00000081 cmp dword ptr [ebp-40h],7
00000085 setge al
00000088 movzx eax,al
0000008b mov dword ptr [ebp-44h],eax
0000008e cmp dword ptr [ebp-44h],0
00000092 jne 00000097
00000094 nop
00000095 jmp 00000068
Run Code Online (Sandbox Code Playgroud)
区别在于无条件跳跃.
不,我甚至认为a while在后面实现了.
使用不好的goto是它鼓励你的代码中来回传递(也称为"意大利面条代码":它是一团糟).它使您的代码极难阅读,调试和分析,并且它引入了错误,因为您无法真正理解正在发生的事情.
好处while是,你可以理解它,编译器可以理解它,所以它可以给你很好的警告.
| 归档时间: |
|
| 查看次数: |
515 次 |
| 最近记录: |