break/continue;goto(避免使用);作用域;异常与保护性检查
正确使用 break 和 continue,理解不建议使用 goto 的原因,安全地应用作用域规则,并在异常与保护性检查之间做出选择。
break/continue;goto(避免使用);作用域;异常与保护性检查 是 CoddyKit 上的免费 C# Academy 课时。 这是第 2 节课,共 3 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 C# Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 C# Academy 课程共包含 3 节课。
课程概览
目标:使用 break/continue 控制循环流程,避免使用 goto,遵守变量的作用域,并在异常与保护性检查之间做出选择。
break 的实际使用
break 会立即退出循环。
using System;
public class Program
{
public static void Main(string[] args)
{
for (int i = 1; i <= 10; i = i + 1)
{
if (i == 4)
{
Console.WriteLine("Stop at 4");
break; // exit loop completely
}
Console.WriteLine("i = " + i);
}
}
}
continue 的实际使用
continue 会跳过当前迭代并进入下一次迭代。
using System;
public class Program
{
public static void Main(string[] args)
{
for (int i = 1; i <= 5; i = i + 1)
{
if (i % 2 == 0)
{
continue; // skip even numbers
}
Console.WriteLine("Odd: " + i);
}
}
}
避免使用 goto
goto 会造成混乱的流程;请改用结构化循环(while/for/foreach)。
using System;
public class Program
{
public static void Main(string[] args)
{
// Goto works but harms readability; prefer structured loops.
int i = 0;
start:
i = i + 1;
Console.WriteLine("i = " + i);
if (i < 3)
{
goto start; // avoid using goto in real code
}
// Prefer: while loop (clear intent)
int j = 0;
while (j < 3)
{
j = j + 1;
Console.WriteLine("j = " + j);
}
}
}
作用域基础
作用域由大括号 { } 定义。请在接近使用位置的地方声明变量,并避免在嵌套代码块中重复使用名称。
using System;
public class Program
{
public static void Main(string[] args)
{
int x = 10; // outer scope
if (x > 0)
{
int y = 5; // inner scope
Console.WriteLine("x + y = " + (x + y));
}
// y is not visible here; this would not compile:
// Console.WriteLine(y);
// Shadowing (avoid reusing names in nested scopes)
int value = 1;
{
int valueInner = 2; // use a new name instead of shadowing
Console.WriteLine("value=" + value + ", inner=" + valueInner);
}
}
}
诊断基础
对于预期的无效输入,请使用保护性检查;对于异常状态,请使用异常。
using System;
public class Program
{
// Guard check: return a bool and an out parameter (no exception on normal invalid input).
public static bool TryDivide(int x, int y, out int result)
{
if (y == 0)
{
result = 0;
return false; // guard fail
}
result = x / y;
return true;
}
// Exceptional state: throw when something truly exceptional happens.
public static int PriceWithTax(int price, int ratePercent)
{
if (price < 0)
{
throw new ArgumentOutOfRangeException("price");
}
if (ratePercent < 0 || ratePercent > 100)
{
throw new ArgumentOutOfRangeException("ratePercent");
}
return price + (price * ratePercent / 100);
}
public static void Main(string[] args)
{
int r;
bool ok = TryDivide(10, 0, out r);
Console.WriteLine("TryDivide ok=" + ok + " result=" + r);
try
{
int total = PriceWithTax(100, 18);
Console.WriteLine("Total=" + total);
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
}
}
break 与 continue
回顾
回顾:使用 break 退出循环,使用 continue 跳过一次迭代;避免使用 goto;让变量处于正确的作用域中;对于预期的无效输入,优先使用保护性检查,对于异常状态则使用异常。
常见问题解答
「break/continue;goto(避免使用);作用域;异常与保护性检查」课时是免费的吗?
是的 — 「break/continue;goto(避免使用);作用域;异常与保护性检查」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 C# Academy 课程的其余内容,请升级到 CoddyKit PRO。 C# Academy 课程共包含 3 节课。
「break/continue;goto(避免使用);作用域;异常与保护性检查」这节课中我会学到什么?
正确使用 break 和 continue,理解不建议使用 goto 的原因,安全地应用作用域规则,并在异常与保护性检查之间做出选择。 你通过在浏览器中直接运行的动手代码来练习 C# Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 C# Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 C# Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 3 节。
「break/continue;goto(避免使用);作用域;异常与保护性检查」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 C# Academy 课中编写并运行代码吗?
能。每节 C# Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- if/else、switch(经典形式)与循环:for、while、do、foreach
- break/continue;goto(避免使用);作用域;异常与保护性检查
- 简单诊断:异常与保护性检查