迭代语句
while语句
while(bool类型) 为真时才执行
有可能执行0次或者多次
实例:运用输入数字相加和判定是否为100,成功继续,错误直接中断,并输出正确的次数
int x = 0;
int y = 0;
const int sum =100;
int score = 0;
Console.WriteLine("please input first number!");
string num1 = Console.ReadLine();
Console.WriteLine("please input second number!");
string num2 = Console.ReadLine();
try
{
x = Int32.Parse(num1);
}
catch
{
Console.WriteLine("输入的第一个数字错误重新输入");
}
try
{
y = Int32.Parse(num2);
}
catch
{
Console.WriteLine("输入的第二个数字错误重新输入");
}
while (x+y==sum)
{
score++;
}
Console.WriteLine( "计算失误,正确次数为{0}",score );
Console.ReadKey();
Do语句
do 关键字 while(bool);
无论如何都会执行一次;
实例:
int sum =0;
int score = 0;
do
{
int x = 0;
Console.WriteLine("please input first number!");
string num1 = Console.ReadLine();
try
{
x = Int32.Parse(num1);
}
catch
{
Console.WriteLine("输入的数字错误重新输入");
continue;
}
int y = 0;
Console.WriteLine("please input second number!");
string num2 = Console.ReadLine();
try
{
y = Int32.Parse(num2);
}
catch
{
Console.WriteLine("输入的数字错误重新输入");
continue;
}
sum = x + y;
if (sum==100)
{
score++;
}
else
{
Console.WriteLine("你的得分是{0}", score);
Console.WriteLine("GAME OVER!");
Console.ReadKey();
}
} while (sum==100);
此程序必须在第一次成功以后才能进行重新跳转,
For语句
如果for内部的条件全部省略,则无限循环
for (; ; )
{
Console.WriteLine("hello,World!!");
}
无限循环模式输出;
Foreach语句
遍历:数组可以进行遍历,array类 继承了IEnumerable就是可以遍历的特征;
跳转语句
continue语句:重新开始的意思,在语句中出现错误时,可用continue进行程序重新执行;只是放弃本次循环,立即进行下次循环;
break语句:整条语句执行结束,进行下一句语句
goto 语句
throw语句
return语句:尽早return,先进行条件判断语句,判断语句如果符合要求尽早return;必须进行返回值 var my=getnumber("nm")
1.break 退出直接封闭它的switch、while、do、for或foreach语句。
当有嵌套时,break只退出最里层的语句块。
break不能跳出finally语句块。
2.continue 用来结束while、do....while、for和foreach语句的当前循环,继续下一轮循环。
continue不等跳出finally语句块。
3.goto goto语句和break语句相似,只是它直接跳转到指定的位置,常用于将程序流转移出嵌套范围。
goto语句包含goto default、goto case、gotolabel语句。
goto default和goto case 用于switch语句块中,用于跳转到指定的case或者直接跳转到default。
例如:
switch(a)
{
case 1:
goto case 2;
case 2: Console.WriteLine("goto case");
case 3:goto default;
default:Console.WriteLine("goto default");
}
goto label:用于跳转到指定的标签,标签用 标签名:表示。
例如:
mark:Console.WriteLine("goto label");
int a=1;
goto mark;
其中mark为标签。