编程语言这些基础的东西都一样一样的。
判断
if
-
if
-
if else
-
if else嵌套
switch
-
switch
-
switch 嵌套
switch(ch1)
{
case 'A':
printf("这个 A 是外部 switch 的一部分" );
switch(ch2)
{
case 'A':
printf("这个 A 是内部 switch 的一部分" );
break;
case 'B': /* 内部 B case 代码 */
}
break;
case 'B': /* 外部 B case 代码 */
}
位运算符
C语言里也叫三目运算符,也算是一种判断
Exp1 ? Exp2 : Exp3;
循环
循环语句
-
while 循环
-
for 循环
-
foreach 循环
class ForEachTest
{
static void Main(string[] args)
{
// 初始化一个数组fibarray
int[] fibarray = new int[] { 0, 1, 1, 2, 3, 5, 8, 13 };
// for each 循环
foreach (int element in fibarray)
{
System.Console.WriteLine(element);
}
System.Console.WriteLine();
// for 循环
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);
}
}
就是个for in 循环,C# 里面叫了个foreach(for each),换名不换逻辑。
- do…while 循环
do
{
statement(s);
}while( condition );
using System;
namespace Loops
{
class Program
{
static void Main(string[] args)
{
/* 局部变量定义 */
int a = 10;
/* do 循环执行 */
do
{
Console.WriteLine("a 的值: {0}", a);
a = a + 1;
} while (a < 20);
Console.ReadLine();
}
}
}
循环控制语句
-
break
-
continue 语句