C语言break语句在实际开发中的例子

   2024-10-07 2610
核心提示:在C语言中,break语句通常用于中断循环或switch语句的执行,以下是一些在实际开发中使用break语句的例子:中断循环:#include st

在C语言中,break语句通常用于中断循环或switch语句的执行,以下是一些在实际开发中使用break语句的例子:

中断循环:
#include <stdio.h>int main() {    int i;    for (i = 1; i <= 10; i++) {        if (i == 5) {            break;        }        printf("%d ", i);    }        return 0;}

输出结果为:1 2 3 4

中断switch语句:
#include <stdio.h>int main() {    int choice = 2;    switch(choice) {        case 1:            printf("You chose option 1\n");            break;        case 2:            printf("You chose option 2\n");            break;        case 3:            printf("You chose option 3\n");            break;        default:            printf("Invalid choice\n");    }        return 0;}

输出结果为:You chose option 2

这些例子展示了在实际开发中如何使用break语句来中断循环或switch语句的执行。

 
举报打赏
 
更多>同类物流大全
推荐图文
推荐物流大全
点击排行

网站首页  |  关于我们  |  联系方式网站留言    |  赣ICP备2021007278号