課程
/后端開發
/C
/C語言入門
為什么要有break呢
2019-02-16
源自:C語言入門 6-6
正在回答
如果不用break;跳出,就得把要找的數放在數組最后讓程序自己跳出:
#include <stdio.h>
int getIndex(int arr[4],int value)
{
? ? int i;
? ? int index;
? ? for(i=0;i<4;i++)//循環次數隨數組下標個數更改
? ? {
? ? ? ?/* 請完善數組查詢功能 */
? ? ? ?if(arr[i]==value)? ? ? ?
? ? ? ?{
? ? ? ? ? ?index=i;
? ? ? ? ? ?//break;//程序依次對比直到找到數組最后的8然后自己退出,所以可以省略break;
? ? ? ?}? ?
? ? ? else
? ? ? {
? ? ? ? ? index=-1;
? ? ? }
? ? ??
? ? }
? ? return index;
}
int main()
? ? int arr[4]={3,12,9,8};//更改數組大小,將要找的8放在最后
? ? int value = 8;
? ? int index = getIndex(arr , value);? ? ? //這里應該傳什么參數呢?
? ? if(index!=-1)
? ? ? ? printf("%d在數組中存在,下標為:%d\n",value,index);? ? ? ? ? ? ?
? ? else
? ? ? ? printf("%d在數組中不存在。\n",value);? ??
? ? return 0;? ??
要跳出for循環
舉報
C語言入門視頻教程,帶你進入編程世界的必修課-C語言
2 回答為什么需要break呢
1 回答為什么要用break
3 回答這里為什么要加break;
2 回答為什么if體里需要break?
1 回答if里面為什么要加break
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯系客服咨詢優惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
2019-03-15
如果不用break;跳出,就得把要找的數放在數組最后讓程序自己跳出:
#include <stdio.h>
int getIndex(int arr[4],int value)
{
? ? int i;
? ? int index;
? ? for(i=0;i<4;i++)//循環次數隨數組下標個數更改
? ? {
? ? ? ?/* 請完善數組查詢功能 */
? ? ? ?if(arr[i]==value)? ? ? ?
? ? ? ?{
? ? ? ? ? ?index=i;
? ? ? ? ? ?//break;//程序依次對比直到找到數組最后的8然后自己退出,所以可以省略break;
? ? ? ?}? ?
? ? ? else
? ? ? {
? ? ? ? ? index=-1;
? ? ? }
? ? ??
? ? }
? ? return index;
}
int main()
{
? ? int arr[4]={3,12,9,8};//更改數組大小,將要找的8放在最后
? ? int value = 8;
? ? int index = getIndex(arr , value);? ? ? //這里應該傳什么參數呢?
? ? if(index!=-1)
? ? {
? ? ? ? printf("%d在數組中存在,下標為:%d\n",value,index);? ? ? ? ? ? ?
? ? }
? ? else
? ? {
? ? ? ? printf("%d在數組中不存在。\n",value);? ??
? ? }
? ? return 0;? ??
}
2019-02-16
要跳出for循環