{"id":221419,"date":"2021-07-05T10:00:11","date_gmt":"2021-07-05T02:00:11","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=221419"},"modified":"2021-06-25T09:57:17","modified_gmt":"2021-06-25T01:57:17","slug":"introduction-to-ternary","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/introduction-to-ternary.html","title":{"rendered":"Lua \u4e2d\u7684\u4e09\u76ee\u8fd0\u7b97\u7b26\u4ecb\u7ecd"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nLua \u539f\u751f\u7684\u8bed\u4e49\u5e76\u6ca1\u6709\u5b9e\u73b0\u4e09\u76ee\u8fd0\u7b97\uff0c\u4e00\u822c\u662f\u901a\u8fc7\u903b\u8f91\u8fd0\u7b97\u7b26 and \u548c or \u6765\u6a21\u62df\u4e09\u76ee\u8fd0\u7b97\u7b26\u7684\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

\u4e09\u76ee\u8fd0\u7b97<\/strong><\/div>\n

\u719f\u6089 C\/C++ \u7684\u8001\u53f8\u673a\u90fd\u77e5\u9053\u4e09\u76ee\u8fd0\u7b97 a ? b : c\uff0c\u8fd9\u79cd\u5199\u6cd5\u66ff\u6362\u7b80\u5355\u7684\u6761\u4ef6\u5224\u65ad\u8bed\u53e5\u53ef\u4ee5\u5728\u4e0d\u589e\u52a0\u9605\u8bfb\u96be\u5ea6\u7684\u60c5\u51b5\u4e0b\uff0c\u4f7f\u4ee3\u7801\u5c3d\u91cf\u4fdd\u6301\u7b80\u6d01\u3002<\/p>\n

int a, b, ret;\r\n\/\/if-else\r\nif (a > b)\r\n    ret = a;\r\nelse\r\n    ret = b;\r\n\r\n\/\/\u4e09\u76ee\u8fd0\u7b97\u7b26\r\nret = a > b ? a : b;<\/pre>\n
Lua \u4e2d\u7684\u4e09\u76ee\u8fd0\u7b97<\/strong><\/div>\n

Lua \u539f\u751f\u7684\u8bed\u4e49\u5e76\u6ca1\u6709\u5b9e\u73b0\u4e09\u76ee\u8fd0\u7b97\uff0c\u4e00\u822c\u662f\u901a\u8fc7\u903b\u8f91\u8fd0\u7b97\u7b26 and \u548c or \u6765\u6a21\u62df\u4e09\u76ee\u8fd0\u7b97\u7b26\u7684\u3002<\/p>\n

Lua \u4e2d and \u548c or \u90fd\u4f7f\u7528\"\u77ed\u8def\u6c42\u503c(short_cur evaluation)\"\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u5b83\u4eec\u53ea\u4f1a\u5728\u9700\u8981\u65f6\u624d\u53bb\u8bc4\u4f30\u7b2c\u4e8c\u4e2a\u64cd\u4f5c\u6570\u3002\uff08\u300aLua\u7a0b\u5e8f\u8bbe\u8ba1\u300b\uff09<\/p>\n

local a, b, ret;\r\nret = a > b and a or b<\/pre>\n

\u7a77\u4e3e\u6240\u6709\u53ef\u80fd\u6027\uff1a<\/p>\n

a > b \u65f6:<\/p>\n

a > b and a \u2013> true \r\na or b \u2013> a<\/pre>\n

a <= b\u65f6:\n\n\n\n

a > b and a \u2013> false \r\na or b \u2013> b<\/pre>\n

\u5b8c\u7f8e\uff01<\/p>\n

Lua \u4e2d\u7684\u4e09\u76ee\u8fd0\u7b97\u7b26\u9677\u9631<\/strong><\/div>\n

\u6309\u7167\u4ece\u7279\u6b8a\u5230\u4e00\u822c\u7684\u539f\u5219:<\/p>\n

    \n
  • \n

    \u4e09\u76ee\u8fd0\u7b97\u7684\u4e00\u822c\u5f62\u5f0fa ? b : c<\/p>\n

    a = true,\u7ed3\u679c\u4e3ab
    \na = false,\u7ed3\u679c\u4e3ac<\/p>\n<\/li>\n

  • \n

    \u5bf9\u5e94Lua\u4e2d\u7684a and b or c<\/p>\n

      \n
    • b = true \n
        \n
      • a = true \n
          \n
        • a and b \u2013> true<\/li>\n
        • b or c \u2013> b<\/li>\n<\/ul>\n<\/li>\n
        • a = false \n
            \n
          • a and b \u2013> false <\/li>\n
          • b or c \u2013> c<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n
          • b = false \n
              \n
            • a = true \n
                \n
              • a and b \u2013> false<\/li>\n
              • b or c \u2013> c<\/li>\n<\/ul>\n<\/li>\n
              • a = false \n
                  \n
                • a and b \u2013> false<\/li>\n
                • b or c \u2013> c <\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n

                  \u53ef\u4ee5\u770b\u5230\u5f53 b = false \u65f6\uff0cLua\u6a21\u62df\u7684 a and b or c \u59cb\u7ec8\u8fd4\u56de c \u5e76\u4e0d\u80fd\u8fd8\u539f\u4e09\u76ee\u8fd0\u7b97\u7b26\u7684\u539f\u8c8c\u3002<\/p>\n

                  \u300aLua\u7a0b\u5e8f\u8bbe\u8ba1\u300b\u4e5f\u5efa\u8bae\u8fd9\u79cd\u60c5\u51b5\u4f7f\u7528 if-else \u6765\u907f\u514d\u3002<\/p>\n

                  \u4e00\u822c\u5316\u7684 a and b or c<\/strong><\/div>\n

                  \u90a3\u4e48\u6709\u6ca1\u6709\u529e\u6cd5\u53ef\u4ee5\u89e3\u51b3 b = false \u5931\u6548\u7684\u95ee\u9898\u5462\uff1f<\/p>\n

                  \u7531\u6b64\u6211\u8054\u60f3\u5230 C \u8bed\u8a00\u6709\u4e00\u9053\u5e38\u89c4\u7684\u8003\u9898\uff1a\u8bf7\u4f7f\u7528\u5b8f\u5b9a\u4e49\u5199\u4e00\u4e2a\u8fd4\u56de\u4e24\u4e2a\u503c\u4e2d\u8f83\u5c0f\u503c\u7684\u65b9\u6cd5\u3002<\/p>\n

                  \u5728\u6821\u65f6\u7b2c\u4e00\u6b21\u770b\u5230\u672c\u9898\uff0c\u7b2c\u4e00\u53cd\u5e94\u5982\u4e0b\uff1a<\/p>\n

                  #define<\/span> MIN<\/span>(<\/span>A<\/span>,<\/span>B<\/span>)<\/span> A <\/span><<\/span> B <\/span>?<\/span> A <\/span>:<\/span> B<\/span><\/pre>\n

                  \u7136\u800c\u8fd9\u79cd\u5199\u6cd5\u5728\u5f88\u591a\u5d4c\u5957\u7684\u4f7f\u7528\u73af\u5883\u4e2d\u90fd\u4e0d\u80fd\u8fd4\u56de\u6b63\u786e\u7684\u7ed3\u679c\u3002\u6bd4\u5982\uff1a2 * MIN(3, 4)\u5c55\u5f00\u4e3a 2 * 3 < 4 ? 3 : 4 = 6 < 4 ? 3 : 4,\u7ed3\u679c\u4e3a4\u3002\n\u66f4\"\u5de5\u7a0b\u5e08\"\u7684\u5199\u6cd5\u662f\uff1a\n\n\n

                  #define<\/span> MIN<\/span>(<\/span>A<\/span>,<\/span>B<\/span>)<\/span> <\/span>((<\/span>A<\/span>)<\/span> <\/span><<\/span> <\/span>(<\/span>B<\/span>)<\/span> <\/span>?<\/span> <\/span>(<\/span>A<\/span>)<\/span> <\/span>:<\/span> <\/span>(<\/span>B<\/span>))<\/span><\/pre>\n

                  \u8fd8\u6709\u597d\u5947\u5fc3\u7684\u540c\u5b66\u53ef\u4ee5\u53c2\u8003\uff1a\u5b8f\u5b9a\u4e49\u7684\u6b63\u786e\u5199\u6cd5\uff0c\u4e09\u76ee\u8fd0\u7b97\u7684\u5b8f\u5b9a\u4e49<\/p>\n

                  \u4ece\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u5c31\u5728\u60f3\u5982\u4f55\u80fd\u4fdd\u8bc1 a and b or c \u4e2d b \u4e3a\u771f\u6216\u8005 b \u4e0d\u4ea7\u751f\u6b67\u4e49\u5462\uff1f<\/p>\n

                    \n
                  1. and\u7684\u8fd0\u7b97\u4f18\u5148\u7ea7\u9ad8\u4e8eor\uff0c\u7b80\u5355\u7684\u6539\u53d8\u8fd0\u7b97\u987a\u5e8f\u5e76\u6ca1\u6709\u7528\u3002<\/li>\n
                  2. \u8fd9\u65f6\u5c31\u60f3\u5230\u4e86lua\u4e2d\u4e07\u80fd\u7684table\uff0c\u80fd\u4e0d\u80fd\u628aa,b,c\u90fd\u653e\u5230table\u4e2d\u6765\u6539\u53d8b\u7684\u5b58\u5728\u5462\uff1f\u8981\u6ce8\u610f{nil}\u4e5f\u662f\u4e00\u4e2a\u4e3atrue\u7684\u5bf9\u8c61\u3002
                    \na,b,c\u90fd\u66ff\u6362\u4e3atable\uff1a{a} and {b} or {c}\u3002<\/li>\n
                  3. \u4e09\u76ee\u8fd0\u7b97\u4e2da\u662f\u6761\u4ef6\uff0c\u7ed3\u679c\u662fb\u6216\u8005c\u3002\u5176\u5b9ea\u5e76\u4e0d\u9700\u8981\u653e\u5165table\u4e2d\uff0c\u5426\u5219{a}\u5c31\u59cb\u7ec8\u4e3atrue\u4e86\uff0c\u5931\u53bb\u4e86\u6761\u4ef6\u7684\u610f\u4e49\u3002\u800c{b} or
                    \n{c}\u7684\u7ed3\u679c\u4e5f\u5fc5\u7136\u662f\u4e00\u4e2atable\uff0c\u8be5table\u53ea\u6709\u4e00\u4e2a\u5143\u7d20\u3002\u90a3\u4e48\u901a\u8fc7[1]\u5373\u53ef\u8bbf\u95ee\u3002<\/li>\n
                  4. \u7efc\u4e0a\u6240\u8ff0\uff0c\u66f4\u4e00\u822c\u5316\u7684Lua\u4e09\u76ee\u8fd0\u7b97\u4e3a\uff1a(a and {b} or {c})[1]<\/li>\n<\/ol>\n
                    \u603b\u7ed3<\/strong><\/div>\n

                    \u8bda\u7136\uff0c\u8981\u60f3\u77e5\u9053Lua\u4e2d\u4e09\u76ee\u8fd0\u7b97\u7684\u5f62\u5f0f\uff0c\u53ea\u9700\u901a\u8fc7\u641c\u7d22\u5c31\u53ef\u4ee5\u627e\u5230\u60f3\u8981\u7684\u7b54\u6848\u3002\u4e0a\u8ff0\u7684\u63a8\u7406\u8fc7\u7a0b\u591a\u5c11\u6709\u70b9\u5148\u77e5\u9053\u7b54\u6848\uff0c\u6545\u610f\u5f80\u7b54\u6848\u4e0a\u9760\u7684\u5acc\u7591\u3002\u4e0d\u8fc7\u6211\u575a\u4fe1\u8bc1\u660e\u4e00\u4e2a\u95ee\u9898\uff0c\u7ed3\u8bba\u5e76\u4e0d\u662f\u6700\u91cd\u8981\u7684\uff0c\u91cd\u8981\u7684\u662f\u8bba\u8bc1\u7684\u8fc7\u7a0b\u3002\u672c\u6587\u4ec5\u4ee5\u6211\u5bf9Lua\u7c97\u6d45\u7684\u8ba4\u77e5\u6765\u5c1d\u8bd5\u8fd8\u539f\u4e00\u822c\u5316\u7684Lua\u4e09\u76ee\u8fd0\u7b97\u7684\u63a8\u5bfc\u8fc7\u7a0b\uff0c\u5982\u6709\u66f4\u597d\u7684\u63a8\u7406\u8fc7\u7a0b\uff0c\u53ef\u4ee5\u4e00\u8d77\u4ea4\u6d41\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

                    \u719f\u6089 C\/C++ \u7684\u8001\u53f8\u673a\u90fd\u77e5\u9053\u4e09\u76ee\u8fd0\u7b97 a ? b : c\uff0c\u8fd9\u79cd\u5199\u6cd5\u66ff\u6362\u7b80\u5355\u7684\u6761\u4ef6\u5224\u65ad\u8bed\u53e5\u53ef\u4ee5\u5728\u4e0d\u589e\u52a0\u9605\u8bfb\u96be […]<\/p>\n","protected":false},"author":321,"featured_media":221421,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-221419","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/221419","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/321"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=221419"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/221419\/revisions"}],"predecessor-version":[{"id":221424,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/221419\/revisions\/221424"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/221421"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=221419"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=221419"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=221419"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}