.NET基础之C#流程控制

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_40945965/article/details/83217929

(一)Convert类型转换

int a = 1;
double b = 2.0;
string str = "123";
b = a;(隐式转换)
a = (int)b;(显式转换)
a = str;(错误)
b = str;(错误)
a = Convert.ToInt16(str);(正确)
b = Convert.ToDouble(str);(正确)

(二)++/–运算符
如果在一个表达式中,既有一元运算符,又有二元运算符,首先计算一元运算符:

int a = 5;
int b = a++ + ++a * 2 + --a + a++;//5 + 7 * 2 + 6 + 6
Console.WriteLine(a);//输出结果:7
Console.WriteLine(b);//输出结果:31
Console.ReadKey();

(二)流程控制语句

int a = 10;
while (a < 20)
{
    Console.WriteLine("a的值为:{0}", a);
    a++;
}
  
do
{
    Console.WriteLine("a的值为:{0}", a);
    a++;
} while (a < 20);

int[] array = new int[] { 1, 1, 2, 3, 5, 8, 13 };
foreach (int i in array)
{
    Console.WriteLine("{0}", i);
}

for (int i = 0; i < array.Length; i++)
{
    Console.WriteLine("{0}", array[i]);
}

for (; ; ) ;
{
    Console.WriteLine("无限循环");
}

猜你喜欢

转载自blog.csdn.net/qq_40945965/article/details/83217929
今日推荐