c语言switch语句用法

阅读: 评论:0

2024年1月31日发(作者:)

c语言switch语句用法

c语言switch语句用法

C语言中的switch语句是一种非常常用的控制语句,它可以根据不同的条件执行不同的代码块。在本文中,我们将详细介绍switch语句的用法,并列举一些常见的应用场景。

1. 基本用法

switch语句的基本用法非常简单,它由一个switch关键字、一个表达式和多个case语句组成。表达式的值会被依次与每个case语句的值进行比较,如果匹配成功,则执行对应的代码块。如果没有匹配成功,则执行default语句(如果有的话)。

下面是一个简单的示例:

```c

#include

int main()

{

int num = 2;

switch (num)

{

case 1:

printf("num is 1n");

break;

case 2:

printf("num is 2n");

break;

case 3:

printf("num is 3n");

break;

default:

printf("num is not 1, 2 or 3n");

break;

}

return 0;

}

```

输出结果为:

```

num is 2

```

2. 多个case语句共用一个代码块

在某些情况下,多个case语句可能需要执行相同的代码块。为了避免重复编写代码,我们可以将这些case语句共用一个代码块。在代码块的最后,我们需要使用break语句来跳出switch语句。

下面是一个示例:

```c

#include

int main()

{

int num = 2;

switch (num)

{

case 1:

case 2:

case 3:

printf("num is between 1 and 3n");

break;

default:

printf("num is not between 1 and 3n");

break;

}

return 0;

}

```

输出结果为:

```

num is between 1 and 3

```

3. case语句中使用常量表达式

在case语句中,我们可以使用常量表达式来匹配值。常量表达式是指在编译时就可以确定值的表达式,例如整数、字符、枚举等。

下面是一个示例:

```c

#include

#define NUM 2

int main()

{

int num = 2;

switch (num)

{

case NUM:

printf("num is equal to NUMn");

break;

default:

printf("num is not equal to NUMn");

break;

}

return 0;

}

```

输出结果为:

```

num is equal to NUM

```

4. case语句中使用枚举

枚举是一种常用的数据类型,它可以将一组相关的常量定义为一个类型。在case语句中,我们可以使用枚举来匹配值。

下面是一个示例:

```c

#include

enum Color {RED, GREEN, BLUE};

int main()

{

enum Color color = GREEN;

switch (color)

{

case RED:

printf("color is redn");

break;

case GREEN:

printf("color is greenn");

break;

case BLUE:

printf("color is bluen");

break;

default:

printf("color is not red, green or bluen");

break;

}

return 0;

}

```

输出结果为:

```

color is green

```

5. case语句中使用字符

在case语句中,我们可以使用字符来匹配值。字符必须用单引号括起来。

下面是一个示例:

```c

#include

int main()

{

char ch = 'a';

switch (ch)

{

case 'a':

printf("ch is an");

break;

case 'b':

printf("ch is bn");

break;

case 'c':

printf("ch is cn");

break;

default:

printf("ch is not a, b or cn");

break;

}

return 0;

}

```

输出结果为:

```

ch is a

```

6. case语句中使用范围

在某些情况下,我们需要匹配一定范围内的值。在case语句中,我们可以使用范围来匹配值。范围的格式为“a..b”,表示从a到b的所有值。

下面是一个示例:

```c

#include

int main()

{

int num = 2;

switch (num)

{

case 1..3:

printf("num is between 1 and 3n");

break;

default:

printf("num is not between 1 and 3n");

break;

}

return 0;

}

```

输出结果为:

```

num is between 1 and 3

```

7. case语句中使用变量

在case语句中,我们可以使用变量来匹配值。变量必须是常量表达式,即在编译时就可以确定值的变量。

下面是一个示例:

```c

#include

int main()

{

const int NUM = 2;

int num = 2;

switch (num)

{

case NUM:

printf("num is equal to NUMn");

break;

default:

printf("num is not equal to NUMn");

break;

}

return 0;

}

```

输出结果为:

```

num is equal to NUM

```

8. case语句中使用表达式

在case语句中,我们可以使用表达式来匹配值。表达式必须是常量

表达式,即在编译时就可以确定值的表达式。

下面是一个示例:

```c

#include

int main()

{

const int NUM = 2;

int num = 2;

switch (num + 1)

{

case NUM + 1:

printf("num is equal to NUMn");

break;

default:

printf("num is not equal to NUMn");

break;

}

return 0;

}

```

输出结果为:

```

num is equal to NUM

```

9. case语句中使用goto语句

在case语句中,我们可以使用goto语句来跳转到其他代码块。这种用法不太常见,但在某些情况下可能会很有用。

下面是一个示例:

```c

#include

int main()

{

int num = 2;

switch (num)

{

case 1:

printf("num is 1n");

goto end;

case 2:

printf("num is 2n");

goto end;

case 3:

printf("num is 3n");

goto end;

default:

printf("num is not 1, 2 or 3n");

goto end;

}

end:

printf("end of programn");

return 0;

}

```

输出结果为:

```

num is 2

end of program

```

10. case语句中使用函数

在case语句中,我们可以调用函数来执行一些操作。这种用法非常灵活,可以根据具体情况来决定是否使用。

下面是一个示例:

```c

#include

void print_num(int num)

{

printf("num is %dn", num);

}

int main()

{

int num = 2;

switch (num)

{

case 1:

print_num(1);

break;

case 2:

print_num(2);

break;

case 3:

print_num(3);

break;

default:

printf("num is not 1, 2 or 3n");

break;

}

return 0;

}

```

输出结果为:

```

num is 2

```

总结

c语言switch语句用法

本文发布于:2024-01-31 16:41:24,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170669048429925.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:语句   使用   表达式   匹配
留言与评论(共有 0 条评论)
   
验证码:
排行榜

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23