Qt:switch 港控/mmm° 2023-01-12 10:10 16阅读 0赞 #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(); } ![在这里插入图片描述][20210128101545232.png] #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(); } ![在这里插入图片描述][20210128101636631.png] 总结: * 总结: 如果没有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(); } ![在这里插入图片描述][20210128101800531.png] 总结: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(); } ![在这里插入图片描述][20210128102028833.png] #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(); } ![在这里插入图片描述][20210128102433904.png] fallthrough 语句让 case 之后的语句会按顺序继续运行,且不论条件是否满足都会执行。 但是break优先级大于fallthrough [20210128101545232.png]: /images/20221119/3379ce3f8c1e4b73b0c8b1d47c8ec888.png [20210128101636631.png]: /images/20221119/ab20f18ef0e94ad4b22f8e33fcf8aa0c.png [20210128101800531.png]: /images/20221119/863dac6fed2442a4a05237522b4ad528.png [20210128102028833.png]: /images/20221119/0b145c6ec8e4451599765e6b39be2eba.png [20210128102433904.png]: /images/20221119/d7bb9d2f6cce44ecb891efd02c9340bb.png
还没有评论,来说两句吧...