#include <QCoreApplication>
#ifndef FALLTHROUGH
#if __has_cpp_attribute(fallthrough)
#define FALLTHROUGH [[fallthrough]]
#elif __has_cpp_attribute(clang::fallthrough)
#define FALLTHROUGH [[clang::fallthrough]]
#else
#define FALLTHROUGH
#endif
#endif
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
int n = 1;
switch (n) {
case 1:
printf("----------1--------\n");
case 2:
printf("----------2--------\n");
case 3:
printf("----------3--------\n");
case 4:
printf("----------4--------\n");
default:
printf("----------o--------\n");
}
printf("---------j----------");
return a.exec();
}
#include <QCoreApplication>
#ifndef FALLTHROUGH
#if __has_cpp_attribute(fallthrough)
#define FALLTHROUGH [[fallthrough]]
#elif __has_cpp_attribute(clang::fallthrough)
#define FALLTHROUGH [[clang::fallthrough]]
#else
#define FALLTHROUGH
#endif
#endif
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
int n = 1;
switch (n) {
case 1:
printf("----------1--------\n");
case 2:
printf("----------2--------\n");
break;
case 3:
printf("----------3--------\n");
case 4:
printf("----------4--------\n");
default:
printf("----------o--------\n");
}
printf("---------j----------");
return a.exec();
}
总结:
- 总结: 如果没有break,switch会一路滑下来,无论条件是否满足
- break会跳出switch
在大多数语言中,switch 语句块中,case 要紧跟 break,否则 case 之后的语句会顺序运行
#include <QCoreApplication>
#ifndef FALLTHROUGH
#if __has_cpp_attribute(fallthrough)
#define FALLTHROUGH [[fallthrough]]
#elif __has_cpp_attribute(clang::fallthrough)
#define FALLTHROUGH [[clang::fallthrough]]
#else
#define FALLTHROUGH
#endif
#endif
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
int n = 1;
switch (n) {
case 1:
printf("----------1--------\n");
case 2:
printf("----------2--------\n");
return 0;
case 3:
printf("----------3--------\n");
case 4:
printf("----------4--------\n");
default:
printf("----------o--------\n");
}
printf("---------j----------");
return a.exec();
}
总结:return会跳出整个函数
#include <QCoreApplication>
#ifndef FALLTHROUGH
#if __has_cpp_attribute(fallthrough)
#define FALLTHROUGH [[fallthrough]]
#elif __has_cpp_attribute(clang::fallthrough)
#define FALLTHROUGH [[clang::fallthrough]]
#else
#define FALLTHROUGH
#endif
#endif
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
int n = 1;
switch (n) {
case 1:
printf("----------1--------\n");
FALLTHROUGH; case 2:
printf("----------2--------\n");
FALLTHROUGH;case 3:
printf("----------3--------\n");
FALLTHROUGH;case 4:
printf("----------4--------\n");
FALLTHROUGH; default:
printf("----------o--------\n");
}
printf("---------j----------");
return a.exec();
}
#include <QCoreApplication>
#ifndef FALLTHROUGH
#if __has_cpp_attribute(fallthrough)
#define FALLTHROUGH [[fallthrough]]
#elif __has_cpp_attribute(clang::fallthrough)
#define FALLTHROUGH [[clang::fallthrough]]
#else
#define FALLTHROUGH
#endif
#endif
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
int n = 3;
switch (n) {
case 1:
printf("----------1--------\n");
break;
FALLTHROUGH; case 2:
printf("----------2--------\n");
break;
FALLTHROUGH;case 3:
printf("----------3--------\n");
break;
FALLTHROUGH;case 4:
printf("----------4--------\n");
break;
FALLTHROUGH; default:
printf("----------o--------\n");
}
printf("---------j----------");
return a.exec();
}
fallthrough 语句让 case 之后的语句会按顺序继续运行,且不论条件是否满足都会执行。 但是break优先级大于fallthrough