声明:个人学习笔记,仅供学习交流和借鉴。
一些共性的编程基础知识拷贝于菜鸟教程 ,但进行了相应的删减和改动并加入了自己的理解。
判断 if 语句 一个 if 语句 由一个布尔表达式后跟一个或多个语句组成。
1 2 3 4 if (boolean_expression){ }
实例 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 using System;namespace DecisionMaking { class Program { static void Main (string [] args ) { int a = 10 ; if (a < 20 ) { Console.WriteLine("a 小于 20" ); } Console.WriteLine("a 的值是 {0}" , a); Console.ReadLine(); } } }
if…else 语句 一个 if 语句 后可跟一个可选的 else 语句 ,else 语句在布尔表达式为假时执行。
1 2 3 4 5 6 7 8 if (boolean_expression){ } else { }
实例 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 using System;namespace DecisionMaking { class Program { static void Main (string [] args ) { int a = 100 ; if (a < 20 ) { Console.WriteLine("a 小于 20" ); } else { Console.WriteLine("a 大于 20" ); } Console.WriteLine("a 的值是 {0}" , a); Console.ReadLine(); } } }
嵌套 if 语句 在 C# 中,嵌套 if-else 语句是合法的,这意味着您可以在一个 if 或 else if 语句内使用另一个 if 或 else if 语句。
1 2 3 4 5 6 7 8 if ( boolean_expression 1 ){ if (boolean_expression 2 ) { } }
实例 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 using System;namespace DecisionMaking { class Program { static void Main (string [] args ) { int a = 100 ; int b = 200 ; if (a == 100 ) { if (b == 200 ) { Console.WriteLine("a 的值是 100,且 b 的值是 200" ); } } Console.WriteLine("a 的准确值是 {0}" , a); Console.WriteLine("b 的准确值是 {0}" , b); Console.ReadLine(); } } }
1 2 3 a 的值是 100 ,且 b 的值是 200 a 的准确值是 100 b 的准确值是 200
switch 语句 一个 switch 语句允许测试一个变量等于多个值时的情况。每个值称为一个 case,且被测试的变量会对每个 switch case 进行检查。
1 2 3 4 5 6 7 8 9 10 11 12 13 switch (expression){ case constant-expression : statement(s); break ; case constant-expression : statement(s); break ; default : statement(s); break ; }
switch 语句必须遵循下面的规则:
switch 语句中的 expression 必须是一个整型或枚举类型,或者是一个 class 类型,其中 class 有一个单一的转换函数将其转换为整型或枚举类型。
在一个 switch 中可以有任意数量的 case 语句。每个 case 后跟一个要比较的值和一个冒号。
case 的 constant-expression 必须与 switch 中的变量具有相同的数据类型,且必须是一个常量。
当被测试的变量等于 case 中的常量时,case 后跟的语句将被执行,直到遇到 break 语句为止。
当遇到 break 语句时,switch 终止,控制流将跳转到 switch 语句后的下一行。
不是每一个 case 都需要包含 break 。如果 case 语句为空,则可以不包含 break ,控制流将会 继续 后续的 case,直到遇到 break 为止。
C# 不允许从一个开关部分继续执行到下一个开关部分。如果 case 语句中有处理语句,则必须包含 break 或其他跳转语句。
一个 switch 语句可以有一个可选的 default case,出现在 switch 的结尾。default case 可用于在上面所有 case 都不为真时执行一个任务。default case 中的 break 语句不是必需的。
C# 不支持从一个 case 标签显式贯穿到另一个 case 标签。如果要使 C# 支持从一个 case 标签显式贯穿到另一个 case 标签,可以使用 goto 一个 switch-case 或 goto default。
实例 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 using System;namespace DecisionMaking { class Program { static void Main (string [] args ) { char grade = 'B' ; switch (grade) { case 'A' : Console.WriteLine("很棒!" ); break ; case 'B' : case 'C' : Console.WriteLine("做得好" ); break ; case 'D' : Console.WriteLine("您通过了" ); break ; case 'F' : Console.WriteLine("最好再试一下" ); break ; default : Console.WriteLine("无效的成绩" ); break ; } Console.WriteLine("您的成绩是 {0}" , grade); Console.ReadLine(); } } }
嵌套 switch 语句 把一个 switch 作为一个外部 switch 的语句序列的一部分,即可以在一个 switch 语句内使用另一个 switch 语句。即使内部和外部 switch 的 case 常量包含共同的值,也没有矛盾。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 switch (ch1){ case 'A' : printf("这个 A 是外部 switch 的一部分" ); switch (ch2) { case 'A' : printf("这个 A 是内部 switch 的一部分" ); break ; case 'B' : } break ; case 'B' : }
实例 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 using System;namespace DecisionMaking { class Program { static void Main (string [] args ) { int a = 100 ; int b = 200 ; switch (a) { case 100 : Console.WriteLine("这是外部 switch 的一部分" ); switch (b) { case 200 : Console.WriteLine("这是内部 switch 的一部分" ); break ; } break ; } Console.WriteLine("a 的准确值是 {0}" , a); Console.WriteLine("b 的准确值是 {0}" , b); Console.ReadLine(); } } }
1 2 3 4 这是外部 switch 的一部分 这是内部 switch 的一部分 a 的准确值是 100 b 的准确值是 200
循环 while 循环 只要给定的条件为真,C# 中的 while 循环语句会重复执行一个目标语句。
1 2 3 4 while (condition){ statement(s); }
实例 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 using System;namespace Loops { class Program { static void Main (string [] args ) { int a = 10 ; while (a < 20 ) { Console.WriteLine("a 的值: {0}" , a); a++; } Console.ReadLine(); } } }
1 2 3 4 5 6 7 8 9 10 a 的值: 10 a 的值: 11 a 的值: 12 a 的值: 13 a 的值: 14 a 的值: 15 a 的值: 16 a 的值: 17 a 的值: 18 a 的值: 19
do…while 循环 do…while 循环与 while 循环类似,但是 do…while 循环会确保至少执行一次循环。
1 2 3 4 5 do { statement(s); }while ( condition );
实例 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 using System;namespace Loops { class Program { static void Main (string [] args ) { int a = 10 ; do { Console.WriteLine("a 的值: {0}" , a); a = a + 1 ; } while (a == 100 ); Console.ReadLine(); } } }
for循环 一个 for 循环是一个允许您编写一个执行特定次数的循环的重复控制结构。
1 2 3 4 for ( init ; condition; increment ){ statement(s); }
init 会首先被执行,且只会执行一次。这一步允许您声明并初始化任何循环控制变量。您也可以不在这里写任何语句,只要有一个分号出现即可。
接下来,会判断 condition 。如果为真,则执行循环主体。如果为假,则不执行循环主体,且控制流会跳转到紧接着 for 循环的下一条语句。
在执行完 for 循环主体后,控制流会跳回上面的 increment 语句。该语句允许您更新循环控制变量。该语句可以留空,只要在条件后有一个分号出现即可。
条件再次被判断。如果为真,则执行循环,这个过程会不断重复(循环主体,然后增加步值,再然后重新判断条件)。在条件变为假时,for 循环终止。
实例 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 using System;namespace Loops { class Program { static void Main (string [] args ) { for (int a = 10 ; a < 20 ; a = a + 1 ) { Console.WriteLine("a 的值: {0}" , a); } Console.ReadLine(); } } }
1 2 3 4 5 6 7 8 9 10 a 的值: 10 a 的值: 11 a 的值: 12 a 的值: 13 a 的值: 14 a 的值: 15 a 的值: 16 a 的值: 17 a 的值: 18 a 的值: 19
foreach C# 也支持 foreach 循环,使用foreach可以迭代数组或者一个集合对象。
以下实例有三个部分:
通过 foreach 循环输出整型数组中的元素。
通过 for 循环输出整型数组中的元素。
foreach 循环设置数组元素的计算器。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 class ForEachTest { static void Main (string [] args ) { int [] fibarray = new int [] { 0 , 1 , 1 , 2 , 3 , 5 , 8 , 13 }; foreach (int element in fibarray) { System.Console.WriteLine(element); } System.Console.WriteLine(); for (int i = 0 ; i < fibarray.Length; i++) { System.Console.WriteLine(fibarray[i]); } System.Console.WriteLine(); int count = 0 ; foreach (int element in fibarray) { count += 1 ; System.Console.WriteLine("Element #{0}: {1}" , count, element); } System.Console.WriteLine("Number of elements in the array: {0}" , count); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 0 1 1 2 3 5 8 13 0 1 1 2 3 5 8 13 Element #1: 0 Element #2: 1 Element #3: 1 Element #4: 2 Element #5: 3 Element #6: 5 Element #7: 8 Element #8: 13 Number of elements in the array: 8
嵌套循环 C# 允许在一个循环内使用另一个循环,下面演示几个实例来说明这个概念。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 using System;namespace Loops { class Program { static void Main (string [] args ) { int i, j; for (i = 2 ; i < 100 ; i++) { for (j = 2 ; j <= (i / j); j++) if ((i % j) == 0 ) break ; if (j > (i / j)) Console.WriteLine("{0} 是质数" , i); } Console.ReadLine(); } } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 2 是质数3 是质数5 是质数7 是质数11 是质数13 是质数17 是质数19 是质数23 是质数29 是质数31 是质数37 是质数41 是质数43 是质数47 是质数53 是质数59 是质数61 是质数67 是质数71 是质数73 是质数79 是质数83 是质数89 是质数97 是质数