c# 循环语句

 if 语句

int a = 5;
int b = 3;
if (a + b > 10)
    Console.WriteLine("The answer is greater than 10");
else
    Console.WriteLine("The answer is not greater than 10");

&& 表示“且”。 也就是说,两个条件必须都为 true

|| 表示“或”

while 语句检查条件,并执行 while 后面的语句。 除非条件为 false,否则它会重复检查条件,并重复执行这些语句

int counter = 0;
while (counter < 10)
{
  Console.WriteLine($"Hello World! The counter is {counter}");
  counter++;
}

while 循环先测试条件,然后再执行 while 后面的代码。 do ... while 循环先执行代码,然后再检查条件。 代码如下所示:

int counter = 0;
do
{
  Console.WriteLine($"Hello World! The counter is {counter}");
  counter++;
} while (counter < 10);

使用 for 循环

for(int counter = 0; counter < 10; counter++)
{
  Console.WriteLine($"Hello World! The counter is {counter}");
}

第一部分是 for 初始值设定项:int counter = 0; 声明 counter 是循环变量,并将它的初始值设置为 0

中间部分是 for 条件:counter < 10 声明只要计数器值小于 10,此 for 循环就会继续执行。

最后一部分是 for 迭代器:counter++ 指定在执行 for 语句后面的代码块后,如何修改循环变量。 在此示例中,它指定 counter 应在代码块每次执行时递增 1。

列循环

for (char column = 'a'; column < 'k'; column++)
{
  Console.WriteLine($"The column is {column}");
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值