{"id":27486,"date":"2020-08-02T08:27:50","date_gmt":"2020-08-02T00:27:50","guid":{"rendered":"http:\/\/www.idc.net\/help\/27486\/"},"modified":"2020-08-02T08:27:50","modified_gmt":"2020-08-02T00:27:50","slug":"%e8%af%a6%e8%a7%a3lua-%e4%b8%ad%e7%9a%84%e4%b8%89%e7%9b%ae%e8%bf%90%e7%ae%97%e7%ac%a6","status":"publish","type":"post","link":"https:\/\/idc.net\/help\/27486\/","title":{"rendered":"\u8be6\u89e3Lua \u4e2d\u7684\u4e09\u76ee\u8fd0\u7b97\u7b26"},"content":{"rendered":"<div class=\"idc_table\">\n<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin: 0.8em 0;font-size: 16px;color: #353535\">Lua \u662f\u4e00\u79cd\u8f7b\u91cf\u5c0f\u5de7\u7684\u811a\u672c\u8bed\u8a00\uff0c\u7528\u6807\u51c6C\u8bed\u8a00\u7f16\u5199\u5e76\u4ee5\u6e90\u4ee3\u7801\u5f62\u5f0f\u5f00\u653e\uff0c \u5176\u8bbe\u8ba1\u76ee\u7684\u662f\u4e3a\u4e86\u5d4c\u5165\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u4ece\u800c\u4e3a\u5e94\u7528\u7a0b\u5e8f\u63d0\u4f9b\u7075\u6d3b\u7684\u6269\u5c55\u548c\u5b9a\u5236\u529f\u80fd\u3002<\/p>\n<p><img decoding=\"async\" class=\"aligncenter\" style=\"border-radius: 6px;margin: 20px auto;max-width: 95%\" src=\"https:\/\/lxlinux.superbed.verylink.top\/item\/626162d5239250f7c5c434e6.jpg\">  <\/p>\n<h2><span style=\"font-size: 18px;font-weight: bold;color: #222;padding-left: 10px;border-left: 5px solid #f83929\">\u4e09\u76ee\u8fd0\u7b97<\/span><\/h2>\n<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin: 0.8em 0;font-size: 16px;color: #353535\">\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<pre><code>int&nbsp;a,&nbsp;b,&nbsp;ret;\r\n\/\/<span style=\"color: #333;font-weight: bold;line-height: 26px\">if<\/span>-else\r\n<span style=\"color: #333;font-weight: bold;line-height: 26px\">if<\/span>&nbsp;(a&nbsp;&gt;&nbsp;b)\r\n&nbsp;&nbsp;&nbsp;ret&nbsp;=&nbsp;a;\r\n<span style=\"color: #333;font-weight: bold;line-height: 26px\">else<\/span>\r\n&nbsp;&nbsp;&nbsp;ret&nbsp;=&nbsp;b;\r\n\r\n\/\/\u4e09\u76ee\u8fd0\u7b97\u7b26\r\nret&nbsp;=&nbsp;a&nbsp;&gt;&nbsp;b&nbsp;?&nbsp;a&nbsp;:&nbsp;b;\r\n<\/code><\/pre>\n<h2><span style=\"font-size: 18px;font-weight: bold;color: #222;padding-left: 10px;border-left: 5px solid #f83929\">Lua \u4e2d\u7684\u4e09\u76ee\u8fd0\u7b97<\/span><\/h2>\n<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin: 0.8em 0;font-size: 16px;color: #353535\">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<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin: 0.8em 0;font-size: 16px;color: #353535\">Lua \u4e2d and \u548c or \u90fd\u4f7f\u7528\u201d\u77ed\u8def\u6c42\u503c(short_cur evaluation)\u201d\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<pre><code><span style=\"color: #0086b3;line-height: 26px\">local<\/span>&nbsp;a,&nbsp;b,&nbsp;ret;\r\nret&nbsp;=&nbsp;a&nbsp;&gt;&nbsp;b&nbsp;and&nbsp;a&nbsp;or&nbsp;b\r\n<\/code><\/pre>\n<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin: 0.8em 0;font-size: 16px;color: #353535\">\u7a77\u4e3e\u6240\u6709\u53ef\u80fd\u6027\uff1a<\/p>\n<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin: 0.8em 0;font-size: 16px;color: #353535\">a &gt; b \u65f6:<\/p>\n<pre><code>a&nbsp;&gt;&nbsp;b&nbsp;and&nbsp;a&nbsp;\u2013&gt;&nbsp;<span style=\"color: #008080;line-height: 26px\">true<\/span>\r\na&nbsp;or&nbsp;b&nbsp;\u2013&gt;&nbsp;a\r\n<\/code><\/pre>\n<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin: 0.8em 0;font-size: 16px;color: #353535\">a <\/p>\n<pre><code>a&nbsp;&gt;&nbsp;b&nbsp;and&nbsp;a&nbsp;\u2013&gt;&nbsp;<span style=\"color: #008080;line-height: 26px\">false<\/span>\r\na&nbsp;or&nbsp;b&nbsp;\u2013&gt;&nbsp;b\r\n<\/code><\/pre>\n<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin: 0.8em 0;font-size: 16px;color: #353535\">\u5b8c\u7f8e\uff01<\/p>\n<h2><span style=\"font-size: 18px;font-weight: bold;color: #222;padding-left: 10px;border-left: 5px solid #f83929\">Lua \u4e2d\u7684\u4e09\u76ee\u8fd0\u7b97\u7b26\u9677\u9631<\/span><\/h2>\n<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin: 0.8em 0;font-size: 16px;color: #353535\">\u6309\u7167\u4ece\u7279\u6b8a\u5230\u4e00\u822c\u7684\u539f\u5219:<\/p>\n<ul>\n<li>\n<section>\n<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin: 0.8em 0;font-size: 16px;color: #353535\">\u4e09\u76ee\u8fd0\u7b97\u7684\u4e00\u822c\u5f62\u5f0fa ? b : c<\/p>\n<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin: 0.8em 0;font-size: 16px;color: #353535\">a = true,\u7ed3\u679c\u4e3ab a = false,\u7ed3\u679c\u4e3ac<\/p>\n<\/section>\n<\/li>\n<li>\n<section>\n<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin: 0.8em 0;font-size: 16px;color: #353535\">\u5bf9\u5e94Lua\u4e2d\u7684a and b or c<\/p>\n<\/section>\n<\/li>\n<li>\n<section>\n<ul>\n<li>\n<section>\n       a = true<br \/>\n     <\/section>\n<\/li>\n<li>\n<section>\n       a = false<br \/>\n     <\/section>\n<\/li>\n<li>\n<section>\n       a and b \u2013&gt; false<br \/>\n     <\/section>\n<\/li>\n<li>\n<section>\n       b or c \u2013&gt; c<br \/>\n     <\/section>\n<\/li>\n<li>\n<section>\n       a and b \u2013&gt; false<br \/>\n     <\/section>\n<\/li>\n<li>\n<section>\n       b or c \u2013&gt; c<br \/>\n     <\/section>\n<\/li>\n<li>\n<section>\n       a = true<br \/>\n     <\/section>\n<\/li>\n<li>\n<section>\n       a = false<br \/>\n     <\/section>\n<\/li>\n<li>\n<section>\n       a and b \u2013&gt; true<br \/>\n     <\/section>\n<\/li>\n<li>\n<section>\n       b or c \u2013&gt; b<br \/>\n     <\/section>\n<\/li>\n<li>\n<section>\n       a and b \u2013&gt; false<br \/>\n     <\/section>\n<\/li>\n<li>\n<section>\n       b or c \u2013&gt; c<br \/>\n     <\/section>\n<\/li>\n<li>\n<section>\n       b = true<br \/>\n     <\/section>\n<\/li>\n<li>\n<section>\n       b = false<br \/>\n     <\/section>\n<\/li>\n<\/ul>\n<\/section>\n<\/li>\n<\/ul>\n<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin: 0.8em 0;font-size: 16px;color: #353535\">\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<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin: 0.8em 0;font-size: 16px;color: #353535\">\u300aLua\u7a0b\u5e8f\u8bbe\u8ba1\u300b\u4e5f\u5efa\u8bae\u8fd9\u79cd\u60c5\u51b5\u4f7f\u7528 if-else \u6765\u907f\u514d\u3002<\/p>\n<h2><span style=\"font-size: 18px;font-weight: bold;color: #222;padding-left: 10px;border-left: 5px solid #f83929\">\u4e00\u822c\u5316\u7684 a and b or c<\/span><\/h2>\n<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin: 0.8em 0;font-size: 16px;color: #353535\">\u90a3\u4e48\u6709\u6ca1\u6709\u529e\u6cd5\u53ef\u4ee5\u89e3\u51b3 b = false \u5931\u6548\u7684\u95ee\u9898\u5462\uff1f<\/p>\n<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin: 0.8em 0;font-size: 16px;color: #353535\">\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<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin: 0.8em 0;font-size: 16px;color: #353535\">\u5728\u6821\u65f6\u7b2c\u4e00\u6b21\u770b\u5230\u672c\u9898\uff0c\u7b2c\u4e00\u53cd\u5e94\u5982\u4e0b\uff1a<\/p>\n<pre><code><span style=\"color: #998;font-style: italic;line-height: 26px\">#define&nbsp;MIN(A,B)&nbsp;A&nbsp; <\/span><\/code><\/pre>\n<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin: 0.8em 0;font-size: 16px;color: #353535\">\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 <\/p>\n<pre><code><span style=\"color: #998;font-style: italic;line-height: 26px\">#define&nbsp;MIN(A,B)&nbsp;((A)&nbsp; <\/span><\/code><\/pre>\n<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin: 0.8em 0;font-size: 16px;color: #353535\">\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<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin: 0.8em 0;font-size: 16px;color: #353535\">\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<ol>\n<li>\n<section>\n    and\u7684\u8fd0\u7b97\u4f18\u5148\u7ea7\u9ad8\u4e8eor\uff0c\u7b80\u5355\u7684\u6539\u53d8\u8fd0\u7b97\u987a\u5e8f\u5e76\u6ca1\u6709\u7528\u3002<br \/>\n  <\/section>\n<\/li>\n<li>\n<section>\n    \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 a,b,c\u90fd\u66ff\u6362\u4e3atable\uff1a{a} and {b} or {c}\u3002<br \/>\n  <\/section>\n<\/li>\n<li>\n<section>\n    \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 {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<br \/>\n  <\/section>\n<\/li>\n<li>\n<section>\n    \u7efc\u4e0a\u6240\u8ff0\uff0c\u66f4\u4e00\u822c\u5316\u7684Lua\u4e09\u76ee\u8fd0\u7b97\u4e3a\uff1a(a and {b} or {c})[1]<br \/>\n  <\/section>\n<\/li>\n<\/ol>\n<h2><span style=\"font-size: 18px;font-weight: bold;color: #222;padding-left: 10px;border-left: 5px solid #f83929\">\u603b\u7ed3<\/span><\/h2>\n<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin: 0.8em 0;font-size: 16px;color: #353535\">\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":"<p>Lua \u662f\u4e00\u79cd\u8f7b\u91cf\u5c0f\u5de7\u7684\u811a\u672c\u8bed\u8a00\uff0c\u7528\u6807\u51c6C\u8bed\u8a00\u7f16\u5199\u5e76\u4ee5\u6e90\u4ee3\u7801\u5f62\u5f0f\u5f00\u653e\uff0c \u5176\u8bbe\u8ba1\u76ee\u7684\u662f\u4e3a\u4e86\u5d4c\u5165\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u4ece\u800c\u4e3a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":27487,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[34],"tags":[],"class_list":["post-27486","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/27486","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/comments?post=27486"}],"version-history":[{"count":0,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/27486\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media\/27487"}],"wp:attachment":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media?parent=27486"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/categories?post=27486"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/tags?post=27486"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}