【Note 】C++ if break, if continue, if return 的区别

本文详细解析了C++中ifbreak、ifcontinue及ifreturn的使用方法,通过具体代码示例展示了如何利用这些语句来控制循环的流程,包括终止循环、跳过当前循环迭代和提前退出函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

老是会忘记if continue的作用,查了一下用法做个记录。

1. if break 用来终止循环,例如

#include <iostream>
using namespace std;
int main()
{
    for(int i=0;i<5;i++)
	{
		
	   if(i==3)
	   break;
           cout<<"a"<<i<<endl;
	}
	cout<<"Randy is a genius."<<endl;
  
}

输出:

a0

a1

a2

Randy is a genius.

 

2. if continue 用来跳过此次循环不执行后面的部分,执行下一个循环

#include <iostream>
using namespace std;
int main()
{
    for(int i=0;i<5;i++)
	{
		
	if(i==3)
	continue;
        cout<<"a"<<i<<endl;
	}
	cout<<"Randy is a genius."<<endl;
  
}

输出:

a0

a1

a2

a4

Randy is a genius.

 

3. if return 用来结束此函数,后面的所有代码都不再执行

#include <iostream>
using namespace std;
int main()
{
    for(int i=0;i<5;i++)
	{
		
	if(i==3)
	return;
        cout<<"a"<<i<<endl;
	}
	cout<<"Randy is a genius."<<endl;
  
}

输出:

a0

a1

a2

 

遇到 i = 3 时函数return, Randy is a genius.也不再执行。

 

希望这次之后能记住!!!!!!!!!!!!!!!!!!

分析以下代码: uint32_t FAST_FUNC getopt32(char **argv, const char *applet_opts, ...) { int argc; unsigned flags = 0; unsigned requires = 0; t_complementary complementary[33]; /* last stays zero-filled */ char first_char; int c; const unsigned char *s; t_complementary *on_off; va_list p; #if ENABLE_LONG_OPTS || ENABLE_FEATURE_GETOPT_LONG const struct option *l_o; struct option *long_options = (struct option *) &bb_null_long_options; #endif unsigned trigger; char **pargv; int min_arg = 0; int max_arg = -1; #define SHOW_USAGE_IF_ERROR 1 #define ALL_ARGV_IS_OPTS 2 #define FIRST_ARGV_IS_OPT 4 int spec_flgs = 0; /* skip 0: some applets cheat: they do not actually HAVE argv[0] */ argc = 1; while (argv[argc]) argc++; va_start(p, applet_opts); c = 0; on_off = complementary; memset(on_off, 0, sizeof(complementary)); /* skip bbox extension */ first_char = applet_opts[0]; if (first_char == '!') applet_opts++; /* skip GNU extension */ s = (const unsigned char *)applet_opts; if (*s == '+' || *s == '-') s++; while (*s) { if (c >= 32) break; on_off->opt_char = *s; on_off->switch_on = (1 << c); if (*++s == ':') { on_off->optarg = va_arg(p, void **); while (*++s == ':') continue; } on_off++; c++; } #if ENABLE_LONG_OPTS || ENABLE_FEATURE_GETOPT_LONG if (applet_long_options) { const char *optstr; unsigned i, count; count = 1; optstr = applet_long_options; while (optstr[0]) { optstr += strlen(optstr) + 3; /* skip NUL, has_arg, val */ count++; } /* count == no. of longopts + 1 */ long_options = alloca(count * sizeof(*long_options)); memset(long_options, 0, count * sizeof(*long_options)); i = 0; optstr = applet_long_options; while (--count) { long_options[i].name = optstr; optstr += strlen(optstr) + 1; long_options[i].has_arg = (unsigned char)(*optstr++); /* long_options[i].flag = NULL; */ long_options[i].val = (unsigned char)(*optstr++); i++; } for (l_o = long_options; l_o->name; l_o++) { if (l_o->flag) continue; for (on_off = complementary; on_off->opt_char; on_off++) if (on_off->opt_char == l_o->val) goto next_long; if (c >= 32) break; on_off->opt_char = l_o->val; on_off->switch_on = (1 << c); if (l_o->has_arg != no_argument) on_off->optarg = va_arg(p, void **); c++; next_long: ; } /* Make it unnecessary to clear applet_long_options * by hand after each call to getopt32 */ applet_long_options = NULL; } #endif /* ENABLE_LONG_OPTS || ENABLE_FEATURE_GETOPT_LONG */ for (s = (const unsigned char *)opt_complementary; s && *s; s++) { t_complementary *pair; unsigned *pair_switch; if (*s == ':') continue; c = s[1]; if (*s == '?') { if (c < '0' || c > '9') { spec_flgs |= SHOW_USAGE_IF_ERROR; } else { max_arg = c - '0'; s++; } continue; } if (*s == '-') { if (c < '0' || c > '9') { if (c == '-') { spec_flgs |= FIRST_ARGV_IS_OPT; s++; } else spec_flgs |= ALL_ARGV_IS_OPTS; } else { min_arg = c - '0'; s++; } continue; } if (*s == '=') { min_arg = max_arg = c - '0'; s++; continue; } for (on_off = complementary; on_off->opt_char; on_off++) if (on_off->opt_char == *s) goto found_opt; /* Without this, diagnostic of such bugs is not easy */ bb_error_msg_and_die("NO OPT %c!", *s); found_opt: if (c == ':' && s[2] == ':') { on_off->param_type = PARAM_LIST; continue; } if (c == '+' && (s[2] == ':' || s[2] == '\0')) { on_off->param_type = PARAM_INT; s++; continue; } if (c == ':' || c == '\0') { requires |= on_off->switch_on; continue; } if (c == '-' && (s[2] == ':' || s[2] == '\0')) { flags |= on_off->switch_on; on_off->incongruously |= on_off->switch_on; s++; continue; } if (c == *s) { on_off->counter = va_arg(p, int *); s++; } pair = on_off; pair_switch = &pair->switch_on; for (s++; *s && *s != ':'; s++) { if (*s == '?') { pair_switch = &pair->requires; } else if (*s == '-') { if (pair_switch == &pair->switch_off) pair_switch = &pair->incongruously; else pair_switch = &pair->switch_off; } else { for (on_off = complementary; on_off->opt_char; on_off++) if (on_off->opt_char == *s) { *pair_switch |= on_off->switch_on; break; } } } s--; } opt_complementary = NULL; va_end(p); if (spec_flgs & (FIRST_ARGV_IS_OPT | ALL_ARGV_IS_OPTS)) { pargv = argv + 1; while (*pargv) { if (pargv[0][0] != '-' && pargv[0][0] != '\0') { /* Can't use alloca: opts with params will * return pointers to stack! * NB: we leak these allocations... */ char *pp = xmalloc(strlen(*pargv) + 2); *pp = '-'; strcpy(pp + 1, *pargv); *pargv = pp; } if (!(spec_flgs & ALL_ARGV_IS_OPTS)) break; pargv++; } } /* In case getopt32 was already called: * reset the libc getopt() function, which keeps internal state. * run_nofork_applet() does this, but we might end up here * also via gunzip_main() -> gzip_main(). Play safe. */ #ifdef __GLIBC__ optind = 0; #else /* BSD style */ optind = 1; /* optreset = 1; */ #endif /* optarg = NULL; opterr = 0; optopt = 0; - do we need this?? */ /* Note: just "getopt() <= 0" will not work well for * "fake" short options, like this one: * wget $'-\203' "Test: test" http://kernel.org/ * (supposed to act as --header, but doesn't) */ #if ENABLE_LONG_OPTS || ENABLE_FEATURE_GETOPT_LONG while ((c = getopt_long(argc, argv, applet_opts, long_options, NULL)) != -1) { #else while ((c = getopt(argc, argv, applet_opts)) != -1) { #endif /* getopt prints "option requires an argument -- X" * and returns '?' if an option has no arg, but one is reqd */ c &= 0xff; /* fight libc's sign extension */ for (on_off = complementary; on_off->opt_char != c; on_off++) { /* c can be NUL if long opt has non-NULL ->flag, * but we construct long opts so that flag * is always NULL (see above) */ if (on_off->opt_char == '\0' /* && c != '\0' */) { /* c is probably '?' - "bad option" */ goto error; } } if (flags & on_off->incongruously) goto error; trigger = on_off->switch_on & on_off->switch_off; flags &= ~(on_off->switch_off ^ trigger); flags |= on_off->switch_on ^ trigger; flags ^= trigger; if (on_off->counter) (*(on_off->counter))++; if (optarg) { if (on_off->param_type == PARAM_LIST) { llist_add_to_end((llist_t **)(on_off->optarg), optarg); } else if (on_off->param_type == PARAM_INT) { //TODO: xatoi_positive indirectly pulls in printf machinery *(unsigned*)(on_off->optarg) = xatoi_positive(optarg); } else if (on_off->optarg) { *(char **)(on_off->optarg) = optarg; } } } /* check depending requires for given options */ for (on_off = complementary; on_off->opt_char; on_off++) { if (on_off->requires && (flags & on_off->switch_on) && (flags & on_off->requires) == 0 ) { goto error; } } if (requires && (flags & requires) == 0) goto error; argc -= optind; if (argc < min_arg || (max_arg >= 0 && argc > max_arg)) goto error; option_mask32 = flags; return flags; error: if (first_char != '!') bb_show_usage(); return (int32_t)-1; }
最新发布
08-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值