了解C#中的控制结构
编程需要使用代码来解决复杂的问题,为了能够解决这些问题,你需要学习编写好程序的基本构件。这些构件将帮助人们在程序中做出各种决定。
一个例子是希望获得一个积极的结果,而不考虑消极的结果。另一个例子是,人们希望程序重新运行代码的某一部分,这就需要使用控制结构。
这些知识将帮助你作为一个程序员选择一个特定的路径或方向。在这篇文章中,你将通过各种例子了解如何在C#中使用控制结构。
先决条件
要跟上本教程,读者需要具备以下条件。
- 具有C和C++编程语言的基本知识。
- 在你的电脑上安装一个代码编辑器
选择语句
首先,我们需要回顾一下语句的定义。在编程中,语句可以是由用户发起的指令。
比如说。
console.writeline("this is my statement");
在这个子话题中,我们要看的是;if、else、else if和switch语句。
如果语句
它有一个规范,只执行一个真实的语句。
语法
if(condition)
{
statement;// it must be true
}
以程序的形式写一个例子。
using System;
namespace MyApplication
{
class demo
{
static void Main(string[] args)
{
if (90> 9) // note that 'if' should be in lowercase otherwise it will bring an error
{
Console.WriteLine("yes i agree 90 is greater");//what will be displayed
}
}
}
}
上面的程序已经被执行了,因为给出的条件是真的(90>9),否则不会执行。
其他 "语句
这个语句与上面的 "如果 "相反。如果条件是假的,它就会执行。编译器必须首先检查if是否满足条件。如果不符合,我们就会进入else语句(编译器会逐行评估)。
语法
if(condition)
{
statement;//executes if conditiom is true
}
else
{
statement;// executes if conditiom is false
}
一个例子
using System;
namespace MyApplication
{
class demo
{
static void Main(string[] args)
{
if (9>90)
// note that it should be in lowercase otherwise it will bring an error
{
Console.WriteLine("yes i agree 90 is greater");
// this will not be excuted
}
else
{
Console.WriteLine("yes 9 is not greater 90");
//this is what will be displayed.
}
}
}
}
从上面的代码来看,如果第一条语句不符合条件,编译器将进入下一条,也就是 "else语句",并执行它。
else if 语句
要执行这个语句,该程序中的其他上述条件(如果条件)应该是假的。
语法
if(condition1)
{
statement;
}
else if(condition2)
{
statement;
}
else(condition3)
// this is executed when condition 1 and 2 are both false
{
statement;
}
一个例子
using System;
namespace MyApplication
{
class demo
{
static void Main(string[] args)
{
int age = 18;
if (age < 10)
{
Console.WriteLine("still child.");
// do not meet condition
}
else if (age < 15)
{
Console.WriteLine("adolescence stage.");
//do not meet condition
}
else
{
Console.WriteLine("you are a man.");
// meets condition
}
}
}
}
代码输出。
you are man
在上面的程序中,编译器不会执行第一个和第二个语句,因为它们不符合条件。因此,第三条语句将被执行。
开关语句
在这里,当表达式与case中的值匹配时,就会执行。关键字break有助于节省执行时间,因为编译器会中断下一个case(表达式)。
语法
switch(expression)
{
case: 1
statement;
break;
case: 2
statement;
break;
.
.
.
case: n
statement;
break;
}
一个例子
using System;
namespace MyApplication
{
class demo
{
static void Main(string[] args)
{
int year = 2;
switch (year)
{
case 1:
Console.WriteLine("2021");
break;
case 2:
Console.WriteLine("2022");
//it will output this we have switched it.
break;
case 3:
Console.WriteLine("2023");
break;
case 4:
Console.WriteLine("2024");
break;
}
}
}
代码输出。
2022
迭代语句
迭代的意思是重复某件事情数次。同样,编程迭代是指当你发现一个算法重复几次,直到它达到或满足特定条件。迭代语句也就是所谓的循环。
While语句
程序员发现很容易学习和理解while语句。在这里,只要语句满足条件(真实条件),它就会一直迭代下去。
语法
while(condition)
statement;
一个例子
using System;
namespace MyApplication
{
class demo
{
static void Main(string[] args)
{
int j = 10;
// we assign a value
while (j <20 )
// then the condition
{
Console.WriteLine(j);
j++;
//incrementing
}
}
}
}
上面的代码将执行从10到19的数字,因为条件说当j<20 ,就输出j ,而我们已经知道我们的j 是等于10的。
Do while
Do while与其他的while循环不同,在那里我们首先要看条件。Do while首先做/执行语句,然后再看该语句是否为真,之后再检查是否为真来重复循环。
语法
do
{
statement;
}
while (condition)
一个例子
using System;
namespace MyApplication
{
class demo
{
static void Main(string[] args)
{
int j = 10;
do
{
Console.WriteLine(j);
//we output before checking the condition
j++;
} while(j<20);
}
}
}
For 循环
For 循环的好处是,人们可以很容易地知道系统将迭代多少次,或者在什么时候停止并给出反馈。
句法
for(initialization;condition; increment/decrement)
{
statement;
}
一个例子
using System;
namespace MyApplication
{
class demo
{
static void Main(string[] args)
{
int j = 10;
for (j=10; j<15;j++)//the loop
{
Console.WriteLine(j);
}
}
}
}
我们首先将'j'初始化为等于10,然后条件是'j'可以在9到15之间取值,因为已经涉及到10,所以它将输出10到14的值。
跳转语句
顾名思义,跳转语句将根据条件将控制从一个地方转移到另一个地方。在本节中,本专题将学习一些跳转语句。
中断语句
当需要代码终止某些语句并进入下一个语句时,就会使用break语句。就像在switch语句中,它被用来终止一个语句并进入下一个语句或终止整个程序。
一个例子
using System;
class demo
{
static public void Main()
{
for (int j = 0; j < 5; j++)
{
if (j == 4)
// it will break when it reaches here
break;
Console.WriteLine("hello");
}
}
}
代码输出。
hello
hello
hello
hello
上面的代码输出将显示'hello,'四次,因为条件说它应该在达到第四次时脱离循环。
转到语句
这是一个一旦遇到就会跳到指定位置的语句。下面我们将学习(通过一个例子)如何实现goto语句。
using System;
namespace demo
{
class Program
{
static void Main(string[] args)
{
int j;
for (j=5; j<15;j++)
{
if(j ==10 )
{
goto endloop;//it will end the program once it gets that j is equal to ten
}
Console.WriteLine("j value: {0}", j);//outputing j
}
endloop: Console.WriteLine("Thank you for your time");
Console.ReadLine();
}
}
}
代码输出。
j value: 5
j value:6
j value:7
j value:8
j value:9
Thank you for your time
上面的程序将显示5到9,因为'j'我们已经启动了它并给了它5。因此,编译器将跳转到10,因为我们的goto语句指向了10。
继续语句
这类语句主要用于程序员希望程序跳过执行的某一部分,例如在循环语句中。在这里,一旦编译器遇到继续语句,它就会按照条件语句的要求跳转,再次开始循环。
一个例子
using System;
class demo{
public static void Main()// Main Method
{
for (int j = 1; j <= 7; j++)
{
if (j == 3) // once the compiler reaches 3,it will skip it and continue with the next number after 3 which is 4
continue;
Console.WriteLine(j); // j is values from 1 to 7 , when 3 is excluded
}
}
}
返回语句
通常情况下,C#中的return语句是用来停止它出现的方法的执行,并将控制权返回给调用的函数。
比如说。
using System;
class demo {
static int sub(int b)
{
int sub = b - b;
// the return statement
return sub;
}
// Main Method
static public void Main()
{
int number = 4;
// now calling subtraction function
int result = sub(number);
Console.WriteLine("The answer is {0}", result);
}
}
上面的程序将返回0,因为我们已经声明了相同的数字4,所以我们的答案将是0。
总结
在这篇文章中,你了解了C#中的控制结构。我们看了如何使用选择语句、迭代语句(循环)和跳转语句,在每一种情况下,你都得到了如何实现它们的例子。