c# 中的 goto 语句
本教程将演示如何在 c# 中使用 goto
语法,并提供一些代码中的实际使用示例。
goto
是一个无条件跳转语句,一旦遇到程序将自动转到代码的新部分。要使用 goto
,你必须同时拥有一个由标签标记的语句和一个调用该标签的实例。
要创建标签,请在调用标签时要运行的语句之前添加其名称和冒号。
例子:
using system;
namespace goto_example1 {
class program {
static void main(string[] args) {
int a = 1;
goto print_a;
a = 2;
print_a:
console.writeline(a.tostring());
console.readline();
}
}
}
在本例中,我们将整数变量 a
初始化为等于 1。由于我们立即调用 goto
跳转到语句 print_a
,所以 a
永远不会设置为等于 2。因此,当我们将 a
的值打印到控制台时,发布的是 1 而不是 2。
输出:
1
何时在 c#
中使用 goto
然而,goto
现在并不常用,因为它被批评为恶化代码的可读性,因为如果它需要你跳转到完全不同的部分,逻辑流程就不那么清晰了。
然而,仍有一些情况下 goto
可以带来好处并提高可读性。例如,它可以转义嵌套循环和 switch
语句。
例子:
using system;
namespace goto_example2 {
class program {
static void main(string[] args) {
// intialize the integer variable a
int a = 2;
// run the function test input
testinput(a);
// change the value of a and see how it affects the output from testinput()
a = 1;
testinput(a);
a = 3;
testinput(a);
console.readline();
}
static void testinput(int input) {
// in this example function, we only want to accept either 1 or 2 as values.
// if we accept the value 1, we want to add to it and then run case 2;
// print the original value
console.writeline("input being tested: " input.tostring());
switch (input) {
case 1:
input ;
// if goto isn't called, the input value will not be modified, and its final value would
// have been unchanged
goto case 2;
case 2:
input = input / 2;
break;
default:
break;
}
console.writeline("final value: " input.tostring() "\n");
}
}
}
在上面的示例中,我们创建了一个示例函数来根据传递的值执行不同的操作。正在考虑三个案例。第一个是值是否等于 1。如果是这种情况,我们将添加到输入值,然后使用 goto
函数继续到情况 2。如果没有调用 goto
,输入值将保持不变。
在情况 2 中,我们将输入除以 2。最后,传递的任何其他值都将属于默认情况,根本不会被修改。最后,打印出最终值并显示案例 1 和 2 将产生相同的绝对值。由于 goto
,即使最初不符合案例规范,也可以通过跳转到其语句来应用案例。
输出:
input being tested: 2
final value: 1
input being tested: 1
final value: 1
input being tested: 3
final value: 3
转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处
本文地址:
相关文章
发布时间:2024/03/16 浏览次数:198 分类:编程语言
-
在 c# 中,有两种主要方法可用于将 list
转换为字符串变量,linq 方法和 string.join()函数。
发布时间:2024/03/16 浏览次数:171 分类:编程语言
-
在 c# 中,有两种主要方法可用于将 list
转换为字符串变量,linq 方法和 string.join()函数。
发布时间:2024/03/16 浏览次数:187 分类:编程语言
-
在 c# 中,有两种主要方法可用于将 list
转换为字符串变量,linq 方法和 string.join()函数。
在 c# 中发出 http post web 请求
发布时间:2024/02/04 浏览次数:131 分类:编程语言
-
在 c# 中,可以使用 3 种主要方法来发出 http post web 请求:webclient 类,httpwebrequest 类和 httpclient 类。本教程将讨论在 c# 中发出 http post web 请求的方法。使用 c# 中的 webclient 类发出 http post web 请求
发布时间:2024/02/04 浏览次数:130 分类:编程语言
-
process 类可用于在 c# 中运行命令提示符命令。在 c# 中使用 process.start() 函数运行命令提示符命令
发布时间:2024/02/04 浏览次数:203 分类:编程语言
-
有两种主要方法可用于在 c# 中调整图像的大小,bitmap 类构造函数和 graphics.drawimage()函数。在本教程中,我们将讨论在c#中调整图像大小的方法。我们将带您完成整个过程,从加载原始图像到保
发布时间:2024/02/04 浏览次数:138 分类:编程语言
-
有 3 种主要方法可用于下载 c# 中的图片,webclient.downloadfile()函数,bitmap 类和 image.fromstream()函数。在 c# 中使用 webclient 类下载图片 webclient 类提供了用于向 c# 中的 url 发送数据和从 url 接收数据
发布时间:2024/02/04 浏览次数:139 分类:编程语言
-
我们可以使用 stopwatch 类来计算 c# 中的经过时间。使用 c# 中的秒表类计算经过时间 stopwatch 类在 c# 中准确测量经过的时间。
发布时间:2024/02/04 浏览次数:200 分类:编程语言
-
有 3 种主要方法可用于获取 c# 中程序的可执行路径,即 assembly 类,appdomain 类和 path 类。本教程将介绍获取 c# 代码的可执行路径的方法。使用 c# 中的 assembly 类获取可执行路径