一、while语句
while语句按不同条件执行一次或多次
先对循环条件进行值
while 语句循环体可能一次都不执行
static void Main(string[] args)
{
int score = 0;
bool canContiue = true;
while (canContiue)
{
Console.WriteLine("Please input first number");
string str1 = Console.ReadLine();
int x = int.Parse(str1);
Console.WriteLine("Please Input second number");
string str2 = Console.ReadLine();
int y = int.Parse(str2);
int sum = x + y;
if (sum==100)
{
score++;
Console.WriteLine("Correct! {0}+{1}={2}", x, y, sum);
}
else
{
Console.WriteLine("Error {0}+{1}={2}", x, y, sum);
canContiue = false;
}
}
Console.WriteLine("Your score is {0}", score);
Console.WriteLine("Game over");
}
}
二、do语句
do-while
- 格式:do embedded-statement while (booleam-expression);
do语句必然执行一次
static void Main(string[] args)
{
int score = 0;
int sum = 0;
do
{
Console.WriteLine("Please input first number");
string str1 = Console.ReadLine();
int x = int.Parse(str1);
Console.WriteLine("Please Input second number");
string str2 = Console.ReadLine();
int y = int.Parse(str2);
sum = x + y;
if (sum==100)
{
score++;
Console.WriteLine("Correct! {0}+{1}={2}", x, y, sum);
}
else
{
Console.WriteLine("Error {0}+{1}={2}", x, y, sum);
}
} while (sum==100);
Console.WriteLine("Your score is {0}", score);
Console.WriteLine("Game over");
}
}
三、跳转语句
1.continue语句
- 放弃当前这次循环,立刻开始下一次循环
static void Main(string[] args)
{
int score = 0;
int sum = 0;
do
{
Console.WriteLine("Please input first number");
string str1 = Console.ReadLine();
int x = 0;
try
{
x = int.Parse(str1);
}
catch
{
Console.WriteLine("First number has problem! Restart");
continue;
}
Console.WriteLine("Please Input second number");
string str2 = Console.ReadLine();
int y = 0;
try
{
y = int.Parse(str2);
}
catch
{
Console.WriteLine("Second number has problem! Restart");
continue;
}
sum = x + y;
if (sum==100)
{
score++;
Console.WriteLine("Correct! {0}+{1}={2}", x, y, sum);
}
else
{
Console.WriteLine("Error {0}+{1}={2}", x, y, sum);
}
} while (sum==100);
Console.WriteLine("Your score is {0}", score);
Console.WriteLine("Game over");
}
}
break 语句
- 结束循环语句,程序会继续执行循环语句后面的语句
static void Main(string[] args)
{
int score = 0;
int sum = 0;
do
{
Console.WriteLine("Please input first number");
string str1 = Console.ReadLine();
int x = 0;
if (str1.ToLower()=="end")
{
break;
}
try
{
x = int.Parse(str1);
}
catch
{
Console.WriteLine("First number has problem! Restart");
continue;
}
Console.WriteLine("Please Input second number");
string str2 = Console.ReadLine();
int y = 0;
if (str2.ToLower()=="end")
{
break;
}
try
{
y = int.Parse(str2);
}
catch
{
Console.WriteLine("Second number has problem! Restart");
continue;
}
sum = x + y;
if (sum==100)
{
score++;
Console.WriteLine("Correct! {0}+{1}={2}", x, y, sum);
}
else
{
Console.WriteLine("Error {0}+{1}={2}", x, y, sum);
}
} while (sum==100);
Console.WriteLine("Your score is {0}", score);
Console.WriteLine("Game over");
}
}
3.for语句
- 计数循环,某个循环,循环次数是固定的,由一个循环变量进行控制
-格式
for( for-initializae; for-condition; for -iterator)//初始化器先执行一次,for-condition判断是否执行,可以执行后,去执行语句块,执行完进行for -iterator
{
}
class Program
{
static void Main(string[] args)
{
//int counter = 0;
//while (counter<10)
//{
// Console.WriteLine("Hello World");
// counter++;
//}
//do
//{
// Console.WriteLine("Hello World");
// counter++;
//} while (counter < 10);
for (int counter = 0; counter < 10; counter++)
{
Console.WriteLine("Hello World");
}
}
}
打印99乘法表
static void Main(string[] args)
{
for(int a=1;a<=9; a++)
{
for (int b = 1; b<= a; b++)
{
//if (b>a)
//{
// break;
//}
Console.Write("{0}*{1}={2}\t", a, b, a * b);
}
Console.WriteLine();
}
}
}
打印星号
static void Main(string[] args)
{
for(int a=1;a<=9; a++)
{
for (int b = 1; b<= a; b++)
{
//if (b>a)
//{
// break;
//}
Console.Write("*\t");
}
Console.WriteLine();
}
}
}
foreach
- 用于枚举一个集合的元素,并对该集合中的每一个元素执行一次相关的嵌入语句
- 什么样的集合可以遍历-数组和泛型
C#中,所有数组类型的基类都是array - 最佳应用场景,对集合进行遍历
static void Main(string[] args)
{
int[] intArray = new int[] { 1,2,3,4,5,6,7,8};
List<int> intList = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, };
foreach (var current in intList)
{
Console.WriteLine(current);
}
}