# 通过分支和循环语句了解条件逻辑Learn conditional logic with branch and loop statements

## 使用 if 语句做出决定Make decisions using the if statement

dotnet new console -n BranchesAndLoops -o .


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


int b = 3;


## 让 if 和 else 完美配合Make if and else work together

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");


ifelse 语句下的缩进是为了方便读者阅读。The indentation under the if and else statements is for human readers. C# 语言忽略缩进或空格。The C# language doesn't treat indentation or white space as significant. ifelse 关键字后面的语句根据条件决定是否执行。The statement following the if or else keyword will be executed based on the condition. 本教程中的所有示例都遵循了常见做法，根据语句的控制流缩进代码行。All the samples in this tutorial follow a common practice to indent lines based on the control flow of statements.

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");
}


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 equal to the second");
}
else
{
Console.WriteLine("The answer is not greater than 10");
Console.WriteLine("Or the first number is not equal to the second");
}


== 符号执行相等 测试。The == symbol tests for equality. 使用 == 将相等测试与赋值测试区分开来，如在 a = 5 中所见。Using == distinguishes the test for equality from assignment, which you saw in a = 5.

&& 表示“且”。The && represents "and". 也就是说，两个条件必须都为 true，才能执行 true 分支中的语句。It means both conditions must be true to execute the statement in the true branch. 这些示例还表明，可以在每个条件分支中添加多个语句，前提是将它们用 {} 括住。These examples also show that you can have multiple statements in each conditional branch, provided you enclose them in { and }.

if ((a + b + c > 10) || (a == b))
{
Console.WriteLine("The answer is greater than 10");
Console.WriteLine("Or the first number is equal to the second");
}
else
{
Console.WriteLine("The answer is not greater than 10");
Console.WriteLine("And the first number is not equal to the second");
}


using System;

namespace BranchesAndLoops
{
class Program
{
static void ExploreIf()
{
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");
}

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");
}

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");
}
}

static void Main(string[] args)
{
ExploreIf();
}
}
}


//ExploreIf();


// 在 C# 中启动 注释。The // starts a comment in C#. 注释是你想要保留在源代码中但不能作为代码执行的任何文本。Comments are any text you want to keep in your source code but not execute as code. 编译器不会从注释中生成任何可执行代码。The compiler does not generate any executable code from comments.

## 使用循环重复执行运算Use loops to repeat operations

int counter = 0;
while (counter < 10)
{
Console.WriteLine($"Hello World! The counter is {counter}"); counter++; }  while 语句会检查条件，并执行 while 后面的语句或语句块。The while statement checks a condition and executes the statement or statement block following the while. 除非条件为 false，否则它会重复检查条件并执行这些语句。It repeatedly checks the condition and executing those statements until the condition is false. 此示例新引入了另外一个运算符。There's one other new operator in this example. counter 变量后面的 ++ 是增量 运算符。The ++ after the counter variable is the increment operator. 它将 counter 值加 1，并将计算后的值存储在 counter 变量中。It adds 1 to the value of counter and stores that value in the counter variable. 重要 请确保 while 循环条件在你执行代码时更改为 false。Make sure that the while loop condition changes to false as you execute the code. 否则，创建的就是无限循环 ，即程序永不结束。Otherwise, you create an infinite loop where your program never ends. 本示例中没有演示上述情况，因为你必须使用 CTRL-C 或其他方法强制退出程序。That is not demonstrated in this sample, because you have to force your program to quit using CTRL-C or other means. while 循环先测试条件，然后再执行 while 后面的代码。The while loop tests the condition before executing the code following the while. do ... while 循环先执行代码，然后再检查条件。The do ... while loop executes the code first, and then checks the condition. 下面的代码对 Do While 循环进行了演示：The do while loop is shown in the following code: int counter = 0; do { Console.WriteLine($"Hello World! The counter is {counter}");
counter++;
} while (counter < 10);


do 循环和前面的 while 循环生成的输出结果相同。This do loop and the earlier while loop produce the same output.

## 使用 for 循环Work with the for loop

For 循环通常用在 C# 中。The for loop is commonly used in C#. 请在 Main() 方法中试用以下代码：Try this code in your Main() method:

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


• 将初始值设定项更改为其他初始值。Change the initializer to start at a different value.
• 将结束条件设定项更改为其他值。Change the condition to stop at a different value.

## 结合使用分支和循环Combine branches and loops

• % 运算符可用于获取除法运算的余数。The % operator gives you the remainder of a division operation.
• if 语句中的条件可用于判断是否应将数字计入总和。The if statement gives you the condition to see if a number should be part of the sum.
• for 循环有助于对 1 到 20 中的所有数字重复执行一系列步骤。The for loop can help you repeat a series of steps for all the numbers 1 through 20.