C#_循环的中断continue,goto和return结束循环

循环的中断 continue(终止当前循环继续下一个循环)

使用continue,只会终止当次循环,继续运行下次循环

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
37
38
39
40
41
42
43
44
45
46
47
48
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _28
{
class Program
{
static void Main(string[] args)
{
//int a = 1;
//while (true)
//{
// a++;
// if (a==5)
// {
// continue;//跳过这次,不运行下面的代码
// }
// if (a==10)
// {
// break;//直接退出循环体
// }
// Console.WriteLine(a); //当index==5的时候,使用了continue关键字,那么continue后面的代码就不会去执行了,直接会进行循环的条件判断,根据判断结果判定是否执行下一次循环
//}

//接受用户输入的整数,如果用户输入的是大于0的偶数,就相加,如果用户输入的是大于0的奇数就不相加,如果用户输入的是0,就把和输出并退出程序
int sum = 0;
while (true)
{
int num = Convert.ToInt32(Console.ReadLine());
if (num==0)
{
break;
}
if (num%2==1)//如果和2求余结果是1,那么就是奇数,则跳转不继续下面的代码
{
continue;
}
sum += num;

}
Console.WriteLine(sum);
Console.ReadKey();
}
}
}

循环的中断 goto可以直接跳到某一个位置

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _29
{
class Program
{
static void Main(string[] args)
{
//接受用户输入,如果输入的0,就使用goto退出循环
while (true)
{
int mun = Convert.ToInt32(Console.ReadLine());
if (mun==0)
{
goto mylabel;
}
}
mylabel: Console.WriteLine("跳出循环了");
Console.ReadKey();
}
}
}

循环的中断 return跳出循环(跳出函数)

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _29
{
class Program
{
static void Main(string[] args)
{
//接受用户输入,如果输入的0,就使用return退出循环
while (true)
{
int num = Convert.ToInt32(Console.ReadLine());
if (num==0)
{
return;//用来终止方法的,表示方法运行结束,剩余的代码不执行了
}
}
Console.WriteLine("跳转循环了");
Console.ReadKey();
}
}
}

C#_循环的终止break语句

循环的中断 break(终止当前循环)

使用break立即跳出循环

练习

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
37
38
39
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _27
{
class Program
{
static void Main(string[] args)
{
//从1到9输出
int index = 1;
while (true)
{
Console.WriteLine(index);
if (index==9)
{
break;//跳出最近的循环体结构,执行下一行代码
}
index++;
}


//接受用户输入,并显示到屏幕,当用户输入0时退出循环体
while (true)
{
string str = Console.ReadLine();
Console.WriteLine(str);
if (str== "0")
{
break;
}
}
//Console.ReadKey();
}
}
}

C#_for循环

for循环语法结构

1
2
3
4
5
6
for(<initialization;<condition>;<operation>>){ 
<code to loop>;
}
<initialization>初始化,这里可以定义一个变量,也可以给变量赋值
<condition>判断是否执行循环条件
<operation>每次执行完循环都会执行operation代码

for循环练习

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _26
{
class Program
{
static void Main(string[] args)
{
//从1到9
for (int i = 1; i <= 9; i++)
{
Console.WriteLine(i);
}

//方法一//从9到1
for (int z = 9; z >= 1; z--)
{
Console.WriteLine(z);
}


//方法二 从9到1
for (int a = 9; a >= 1;)
{
Console.WriteLine(a);
a--;
}

//方法三 从9到1
int y = 9;
for (; y >= 1;)
{
Console.WriteLine(y);
y--; //把y--放到语句后面也是可以的
}

//无限循环
for(; ; )//默认条件为true //无限循环
{
Console.WriteLine("111");
}

Console.ReadKey();
}
}
}

C#_while循环和do while循环

while循环语法结构

1
2
3
while(<test>){
<code to be looped>
}

练习-使用while输出1-9

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
37
38
39
40
41
42
43
44
45
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _25
{
class Program
{
static void Main(string[] args)
{
while (true)//一直执行循环体,根本停不下来
{
Console.WriteLine("循环");
}



//从1输出到9
int i=0;
while (i<9)
{
i++;
Console.WriteLine(i);
}
//从9输出到0
int z = 10;
while (z>0)
{
z--;
Console.WriteLine(z);
}
//从10输出到1
int x = 10;
while (x > 0)
{
Console.WriteLine(x);
x--;
}
Console.ReadKey();

}
}
}

do while循环语法结构

1
2
3
4
do
{
Cosole.WriteLine("这是里do while的循环体");//do while会先执行一次循环体,然后进行条件判断
}while(true);//当条件为true的时候是一个无限循环

do while练习

1
2
3
4
5
6
7
8
//从1输出到9
int index = 1;
do
{
Console.WriteLine(index);
index++;
} while (index<10);
Console.ReadKey();

总结

do while循环会首先执行一次循环体,然后进行条件判断 //循环体的执行次数>=1,不管条件如何 一开始都会先执行一次
while循环会进行条件判断,然后根据判断结果去判定是否去执行循环体 //循环体执行次数>=0,如果条件不满足则一次也不会执行

C#_switch语句-基本语法

switch语句-基本语法

switch语句类似于if语句,switch可以用来将测试变量跟多个值进行比较。switch的语法结构如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
switch(<testvar>){
case<comparisonvar1>:
<code to execute if<testvar>==<comparisionvar1>>
break;
case<comparisonvar2>:
<code to execute if<testvar>==<comparisionvar2>>
break;
...
default://如果上面的都不成立,则运行默认的代码
<code to execute if<testvar>!=<comparisionvals>>
break;
}
<testvar>这里不管直接放一个字面值还是变量,它的类型是数值类型跟char类型

switch语句-练习

定义一个int类型存储游戏状态
0代表开始界面 1战斗中 2暂停 3游戏胜利 4游戏失败
使用switch判断游戏状态,并输出游戏状态

实例

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
37
38
39
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _24
{
class Program
{
static void Main(string[] args)
{
int str = 3;
switch (str)
{
case 0:
Console.WriteLine("开始界面");
break;
case 1:
Console.WriteLine("战斗中");
break;
case 2:
Console.WriteLine("暂停");
break;
case 3:
Console.WriteLine("游戏胜利");
break;
case 4:
case 5: //当值是4或者5的时候执行下面那句
Console.WriteLine("游戏失败");
break;
default:
Console.WriteLine("当前str超出了游戏状态的取值范围");
break;
}
Console.ReadKey();
}
}
}

C#_算法练习-在数组里取第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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _22
{
class Program
{ //在数组里取第2大的数
public static int Getmax(int[] arr)
{
int max = arr[1], mix = arr[1];
for (int i = 0; i < arr.Length; i++)
{
if (arr[i]>mix)
{
mix = arr[i];
if (mix>max)
{
max += mix;
mix = max - mix;
max -= mix;

}
}
}
if (max == mix)
throw new Exception("no second max!");
else
return mix;
}

static void Main(string[] args)
{
int[] ar = { 1, 2, 3, 4, 5, 6 };
try
{
Console.WriteLine(Getmax(ar).ToString());
}
catch (Exception exc )
{
Console.WriteLine(exc.Message);
}
Console.ReadKey();
}
}
}

C#_使用if else语句进行多条件判断

1
2
3
4
5
6
7
8
if(){    
}else if(){
}else if(){
}else{
}

else if可以有0或者多个
else 只能有0或者1个

实例

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _23
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("您考了多少分?");
string str = Console.ReadLine();
int score = Convert.ToInt32(str);
if (score>=90)
{
Console.WriteLine("优秀");
}
else if (score >= 80 && score < 90)
{
Console.WriteLine("良");
}
else if (score>=60&&score<80)
{
Console.WriteLine("中");
}
else
{
Console.WriteLine("差,继续努力喔!");
}
Console.ReadKey();
}
}
}

C#_三元运算符

语法

1
2
<test>?<resultlfTrue>:<resultlfFalse>
<条件>?<如果满足>:<如果不满足>

测试结果如下图1

测试结果如下图2

实例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _21
{
class Program
{
static void Main(string[] args)
{
int num = 10;
string restr = (num < 10)
? "满足条件输出 Less than 10"
: "不满足条件输出 Greater than or equal to 10";
Console.WriteLine(restr);
Console.ReadKey();
}
}
}

C#_if else语句

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
if else 语法
if(<test>)
<code executed if <test> is true>//满足条件执行段段
else
<code executed if <test> is false>//不满足条件执行这段

如果if和else要执行的代码有多行,要加上{}组成一个块
if(<test>)
{
<code1 executed if <test> is true>
<code2 executed if <test> is true>
}
else
{
<code1 executed if <test> is false>
<code2 executed if <test> is false>
}

##实例

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _20
{
class Program
{
static void Main(string[] args)
{
string str = Console.ReadLine();
int num1 = Convert.ToInt32(str);
if (num1 > 50)
{
num1++;
Console.WriteLine("大于50");
}
else
{
num1--;
Console.WriteLine("小于50");
}

Console.ReadKey();

}
}
}

C#_分支—if语句

使用if语句可以有条件的执行某段代码

if的语法

1
2
3
if(<test>)
code executed if <test> is true;// 如果只有一行代码也可以不需要{}这个括号,有多行代码就要加上{}
先执行if括号里的test,如果满足条件,则执行if的下一行代码

实例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _19
{
class Program
{
static void Main(string[] args)
{
bool var = true;
if (var)//如果满足条件,则执行下面这段代码
Console.WriteLine("-------");//执行这段

Console.WriteLine("if语句后面的语句");
Console.ReadKey();

}
}
}