网站首页 > 基础教程 正文
c#中跳转语句\循环控制语句主要用于无条件的转移控制,跳转语句会将控制转到某个位置,这个位置就成为跳转语句的目标。如果跳转语句出现在一个语句块内,而跳转语句的目标却在该语句块之外,则称为跳转语句退出该语句块。跳转语句主要包括break语句、continue语句、goto语句、return语句。
跳转语句更改执行的正常序列。当执行离开一个范围时,所有在该范围中创建的自动对象都会被销毁。
C# 提供了下列的控制语句:
- break语句。终止 loop 或 switch 语句,程序流将继续执行紧接着 loop 或 switch 的下一条语句。
- continue语句。引起循环跳过主体的剩余部分,立即重新开始测试条件。
- goto语句。无条件跳转语句。
- return语句。用于退出类的方法。
break 语句
break 语句有以下两种用法:
- 当 break 语句出现在一个循环内时,循环会立即终止,且程序流将继续执行紧接着循环的下一条语句。
- 它可用于终止 switch 语句中的一个 case。
如果使用的是嵌套循环(即一个循环内嵌套另一个循环),break 语句会停止执行最内层的循环,然后开始执行该块之后的下一行代码。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ForLoop4
{
internal class Program
{
static void Main(string[] args)
{
/* 局部变量定义 */
int a = 10;
/* while 循环执行 */
while (a < 20)
{
Console.WriteLine("a 的值: {0}", a);
a++;
if (a > 15)
{
/* 使用 break 语句终止 loop */
break;
}
}
}
}
}
模拟场景:
计算1到100的累加运算,在值为50的时候使用break退出循环。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Breakloop
{
internal class Program
{
static void Main(string[] args)
{
int iNum = 1; //iNum从1到100递增
int iSum = 0; //记录每次累加后的结果
while (iNum <= 100) //iNum <= 100 是循环条件
{
iSum += iNum; //把每次的iNum的值累加到上次累加的结果中
iNum++; //每次循环iNum的值加1
if (iNum == 50) //判断iNum的值是否为50
break; //退出循环
}
Console.WriteLine("1到49的累加结果是:" + iSum); //输出结果
}
}
}
continue 语句
C# 中的 continue 语句有点像 break 语句。但它不是强迫终止,continue 会跳过当前循环中的代码,强迫开始下一次循环。对于 for 循环,continue 语句会导致执行条件测试和循环增量部分。
对于 while 和 do…while 循环,continue 语句会导致程序控制回到条件测试上。
完整示例代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ContinueLoop
{
internal class Program
{
static void Main(string[] args)
{
/* 局部变量定义 */
int a = 10;
/* do 循环执行 */
do
{
if (a == 15)
{
/* 跳过迭代 */
a = a + 1;
continue;
}
Console.WriteLine("a 的值: {0}", a);
a++;
} while (a < 20);
}
}
}
模拟场景:
在for循环中使用continue语句计算1到100的偶数累加运算。
完整示例代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace continue2
{
internal class Program
{
static void Main(string[] args)
{
int iSum = 0; //定义变量,用来存储偶数和
int iNum = 1; //定义变量,用来作为循环变量
for (; iNum <= 100; iNum++) //执行for循环
{
if (iNum % 2 == 1) //判断是否为偶数
continue; //继续下一次循环
iSum += iNum; //记录偶数的和
}
Console.WriteLine("1到100之间的偶数的和:" + iSum); //输出偶数和
}
}
}
goto语句
在 C# 中,goto 语句用于将程序的执行转移到代码中的标签。这种语句通常用于跳出嵌套循环、跳出多层嵌套的代码块或者在代码中创建无限循环。
语法格式:
goto label;
//label 是一个标签,可以是任何合法的 C# 标识符,后跟一个冒号(:)。
//标签必须位于同一个方法或访问块中,并且不能位于 goto 语句之前。
完整示例代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
namespace GotoLoop
{
internal class Program
{
static void Main(string[] args)
{
int i = 0;
while (i < 10)
{
i++;
if (i == 5)
{
goto Label; // 跳转到标签位置
}
Label:
Console.WriteLine(i);
}
}
}
}
//goto 语句在 i 等于 5 时跳转到标签 end 的位置,从而跳出循环。
尽管 goto 语句在某些情况下可能很有用,但它通常被认为是不良的编程实践,因为它可能导致代码难以理解和维护。在大多数情况下,您可以使用其他结构(如 break、continue、return 等)来代替 goto 语句。
return语句
在C#中,return语句用于从函数或方法中返回一个值。这个值可以是任何类型,例如整数,字符串,对象等。
return语法:
return num1 + num2;
完整示例代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace returnLoop
{
internal class Program
{
static void Main(string[] args)
{
int result = AddNumbers(4, 5);
Console.WriteLine(result); // 输出9
}
public static int AddNumbers(int num1, int num2)
{
return num1 + num2;
}
}
}
//AddNumbers的函数,接收两个整数参数并返回它们的和。
//使用return语句返回这个和。
return语句返回对象.
完整示例代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace returnLoop2
{
internal class Program
{
static void Main(string[] args)
{
Person person = GetPerson();
Console.WriteLine(#34;Name: {person.Name}, Age: {person.Age}"); // 输出"Name: John, Age: 30"
}
public static Person GetPerson()
{
return new Person { Name = "John", Age = 30 };
}
}
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
}
//返回一个新的Person对象
- 上一篇: C#_图片拖拽 c#拖动
- 下一篇: 如何在 C# 程序中注入恶意 DLL? dll注入进程
猜你喜欢
- 2024-10-12 340.C# 中最有价值的语法糖及其应用场景
- 2024-10-12 Dev——手把手教你学会CheckedListBox(C#)
- 2024-10-12 流程控制-循环语句(5-5)-C#编程零基础到入门学习
- 2024-10-12 C#12那些有意思的特性 c# 特性的实现原理
- 2024-10-12 C#得到网关和DNS地址 .netcore 网关
- 2024-10-12 C#设计模式之4-原型模式 c++原型模式
- 2024-10-12 C#05(判断、循环语句) c#循环次数由什么决定
- 2024-10-12 C#:编程界的全能王,其他语言只能望尘莫及?
- 2024-10-12 C# 中的模式匹配与安全的类型转换:is 和 as 运算符的深入解析
- 2024-10-12 C# 循环语句介绍 c#循环输出
- 最近发表
- 标签列表
-
- gitpush (61)
- pythonif (68)
- location.href (57)
- tail-f (57)
- pythonifelse (59)
- deletesql (62)
- c++模板 (62)
- css3动画 (57)
- c#event (59)
- linuxgzip (68)
- 字符串连接 (73)
- nginx配置文件详解 (61)
- html标签 (69)
- c++初始化列表 (64)
- exec命令 (59)
- canvasfilltext (58)
- mysqlinnodbmyisam区别 (63)
- arraylistadd (66)
- node教程 (59)
- console.table (62)
- c++time_t (58)
- phpcookie (58)
- mysqldatesub函数 (63)
- window10java环境变量设置 (66)
- c++虚函数和纯虚函数的区别 (66)