分支和循环
使用 if 语句做出决定
传统的if
语句,格式见下面代码
int a = 5;
int b = 6;
if (a + b > 10)
Console.WriteLine("The answer is greeater than 10.");
The answer is greeater than 10.
让 if 和 else 完美配合
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");
}
The answer is not greater than 10
&&
表示且
int a = 5;
int b = 3;
int c = 4;
if ((a + b + c > 10) && (a > b))
{
Console.WriteLine("The answer is greater than 10");
Console.WriteLine("And the first number is greater than the second");
}
else
{
Console.WriteLine("The answer is not greater than 10");
Console.WriteLine("Or the first number is not greater than the second");
}
The answer is greater than 10
And the first number is greater than the second
||
表示或
int a = 5;
int b = 3;
int c = 4;
if ((a + b + c > 10) || (a > b))
{
Console.WriteLine("The answer is greater than 10");
Console.WriteLine("Or the first number is greater than the second");
}
else
{
Console.WriteLine("The answer is not greater than 10");
Console.WriteLine("And the first number is not greater than the second");
}
The answer is greater than 10
Or the first number is greater than the second
使用循环重复执行运算
while
循环
++
运算符:自加1
int counter = 0;
while (counter < 10)
{
Console.WriteLine($"Hello World! The counter is {counter}");
counter++;
}
Hello World! The counter is 0
Hello World! The counter is 1
Hello World! The counter is 2
Hello World! The counter is 3
Hello World! The counter is 4
Hello World! The counter is 5
Hello World! The counter is 6
Hello World! The counter is 7
Hello World! The counter is 8
Hello World! The counter is 9
do-while
循环:先执行,后判断
int counter = 0;
do
{
Console.WriteLine($"Hello World! The counter is {counter}");
counter++;
}while (counter < 10);
Hello World! The counter is 0
Hello World! The counter is 1
Hello World! The counter is 2
Hello World! The counter is 3
Hello World! The counter is 4
Hello World! The counter is 5
Hello World! The counter is 6
Hello World! The counter is 7
Hello World! The counter is 8
Hello World! The counter is 9
使用 for 循环
for(初始值;循环条件;循环后操作)
for(int counter = 0; counter < 10; counter++)
{
Console.WriteLine($"Hello World! The counter is {counter}");
}
Hello World! The counter is 0
Hello World! The counter is 1
Hello World! The counter is 2
Hello World! The counter is 3
Hello World! The counter is 4
Hello World! The counter is 5
Hello World! The counter is 6
Hello World! The counter is 7
Hello World! The counter is 8
Hello World! The counter is 9
注意3个语句执行顺序,经测试,如果把for
语句写成如下形式
for(①;②;③),那么执行顺序为
①②-代码块
③②-代码块
③②-代码块
③②-代码块
…
练习:
//我的答案
int sum = 0;
for(int counter=0;counter < 21; counter++)
{
if(counter%3 == 0)
{
sum += counter;
}
}
Console.WriteLine(sum)
63
//官网答案
int sum = 0;
for (int number = 1; number < 21; number++)
{
if (number % 3 == 0)
{
sum = sum + number;
}
}
Console.WriteLine($"The sum is {sum}");
The sum is 63
总结
1个判断语句,2个循环语句
1个判断语句if
-if-else
2个循环语句for
,while
-do-while
使用范式
if
if(条件)
{
条件为True代码块
}
if-else
if(条件)
{
条件为True代码块
}
else
{
条件为False代码块
}
for
for(初始条件;条件;循环完成后的操作)
{
代码块
}
注意3个语句执行顺序,经测试,如果把for
语句写成如下形式
for(①;②;③),那么执行顺序为
①②-代码块
③②-代码块
③②-代码块
③②-代码块
…
while
while(条件)
{
条件为True代码块
}
while
-do-while
do
{
代码块
}while(条件);
杂项
&&
:与
||
:或
++
:自加1