这里简单介绍视频中反复学习的集中循环语句,欢迎大家在下方评论发表建议与想法!
while循环
while语句:
static void WhileStatement(string[] args)
{
int i = 0;
while (i < args.Length)
{
Console.WriteLine(args[i]);
i++;
}
}
do语句:
static void DoStatement(string[] args)
{
string s;
do
{
s = Console.ReadLine();
Console.WriteLine(s);
} while (!string.IsNullOrEmpty(s));
}
一些例子:
int i = 0;
while (i < 100)
{
Console.WriteLine("hello,world");
i++;
}
Console.ReadKey();
string name = "";
string pwd = "";
do
{
Console.WriteLine("请输入账号");
name = Console.ReadLine();
Console.WriteLine("请输入密码");
pwd = Console.ReadLine();
} while (name != "admin" || pwd != "111111");
Console.WriteLine("程序结束");
Console.ReadKey();
while和do-while的区别:
-while先判断后执行;do-while先执行后判断。
-初始值不满足循环条件时:
while循环一次都不执行;
do-while循环会执行一次。
break
-break用于循环中,退出当前循环。
static void BreakStatement(string[] args)
{
while (true)
{
string s = Console.ReadLine();
if (string.IsNullOrEmpty(s))
break;
Console.WriteLine(s);
}
}
continue
-立即结束本次循环,判断循环条件,若果成立,则进入下一次循环,否则退出循环。
static void ContinueStatement(string[] args)
{
for (int i = 0; i < args.Length; i++)
{
if (args[i].StartsWith("/"))
continue;
Console.WriteLine(args[i]);
}
}
for循环
for语句:
for(表达式1;表达式2;表达式3)
{
循环体
}
转换为for循环:
for (int i = 0; i < 100; i++)
{
Console.WriteLine("hello,world");
}
Console.ReadKey();
更多更专业的内容请点击:C#语句