https://zyrastory.com/wp-includes/js/jquery/jquery.min.js
https://zyrastory.com/wp-includes/js/jquery/jquery-migrate.min.js
(function(c,l,a,r,i,t,y){
c[a]=c[a]||function(){(c[a].q=c[a].q||[]).push(arguments)};
t=l.createElement(r);t.async=1;t.src="https://www.clarity.ms/tag/"+i;
y=l.getElementsByTagName(r)[0];y.parentNode.insertBefore(t,y);
})(window, document, "clarity", "script", "btkbh92jgl");
(function(m,e,t,r,i,k,a){m[i]=m[i]||function(){(m[i].a=m[i].a||[]).push(arguments)};
m[i].l=1*new Date();
for (var j = 0; j < document.scripts.length; j++) {if (document.scripts[j].src === r) { return; }}
k=e.createElement(t),a=e.getElementsByTagName(t)[0],k.async=1,k.src=r,a.parentNode.insertBefore(k,a)})
(window, document, "script", "https://mc.yandex.ru/metrika/tag.js", "ym");ym(93103491, "init", {
clickmap:true,
trackLinks:true,
accurateTrackBounce:true
});
(function(c,l,a,r,i,t,y){
c[a]=c[a]||function(){(c[a].q=c[a].q||[]).push(arguments)};t=l.createElement(r);t.async=1;
t.src="https://www.clarity.ms/tag/"+i+"?ref=wordpress";y=l.getElementsByTagName(r)[0];y.parentNode.insertBefore(t,y);
})(window, document, "clarity", "script", "btkbh92jgl");
跳至主要內容
文章觀看次數: 1,943
上一次講完基本的資料結構(型態)
這一期我們來說一下程式的基本,邏輯判斷相關的東西
邏輯判斷,顧名思義,最簡單的就是bool的true跟false,也就是簡單的「是」 以及「不是」
運算子 (== 、!=、&&、||、! )再往下繼續之前得先對常見的運算子有些了解
分別是
== (Equal) ,即為是否相等 != (Not Equal),== 的反義詞,是否不相等 && (And) ,「和」的英文,前後的條件(布林)是否均成立 || (Or) , 「或」的英文,前後的條件(布林)任一成立即可 ! (Not) ,基本上可以認為是反轉 分別舉例一下
#1 . 1 是否等於2 ➜ 1 == 2 ,自然不成立,故回傳false
#2. 蝨子跟獅子是不一樣的嗎 ➜ 蝨子 != 獅子,答案是肯定的(不同物種的吧),故回傳true
#3 「1是否等於2」跟「2是否等於2」 ➜ (1 == 2) && (2 == 2)
If – Else 邏輯判斷的老大哥,底下又有
if 如果(第一個) else if 如果前面都不成立 else 剩下的我全要的
而其中只有 if 可以單獨存在 ,而其他兩個脫離了if 就是缺了水的魚一樣沒辦法存在
語法的話 if 跟 else if 都必須包含一個布林值來做判斷(else 就是別人不要的,全盤接收這樣)
舉例來說,我們現在想要吃午餐
但我們有一套吃午餐的邏輯,那就是根據禮拜幾來覺得要吃什麼 (假設今天是禮拜二)
如果禮拜一就吃「義大利麵」,禮拜五吃「咖哩飯」,其他日子就是吃一般便當
這時我們應該怎麼表示呢?
你可能會想說,那不就只能吃便當嗎?
對啊,可是你這樣跟電腦說…它怎麼聽得懂啊 (電腦心理苦…但它不說)
所謂的程式語言即是把我們的語言轉成電腦的語言
int weekDay = 2; //初始化今天禮拜幾
if(weekDay == 1)
{
return "義大利麵";
}
else if(weekDay == 5)
{
return "咖哩飯";
}
else
{
return "一般便當";
}
可以看到條件從上依序往下,如果有任意一個成立就會回傳(就不會繼續往下了)
以這題範例來說,
遇上第一個條件 ( 2 == 1) ,那當然是false,不成立所以繼續往下 第二個條件 (2 == 5) ,也不成立餒,只能在看下後面了 最後只能進到貪心的else 裡面了(可能是垃圾桶也不一定),所以最後回傳的就是「一般便當」 所以可以得出結論
If (條件) 、Else If(條件)、Else(不用條件)
Switch 大家看著 If的範例會不會想說,那如果有很多很多條件,難道我也只能寫很多If 嗎?
當然不是!
switch就可以很好的解決這個問題,不是大家都愛的這個switch
(我也想要一台) switch的英文即為開關、轉換,即可直接依照符合運算式來選取要執行區塊
以剛剛的午餐範例來說
int weekDay = 2; //初始化今天禮拜幾
string lunch; //結果
switch(weekDay) //switch(x) ➜ x為要判斷的參數
{
case 1:
lunch = "義大利麵";
break;
case 5:
lunch = "咖哩飯";
break;
default: //預設,即為無任何條件符合
lunch = "一般便當";
break;
}
return lunch;
可以看到Switch寫起來跟If 還是有不太一樣的地方
第一是需要加上break ,如此才會在找到符合之後離開switch區塊
第二是沒有Else ,但有跟其作用類似的defalut (一個是其餘的,一個是預設的)
如果現在有多個相似的條件(本來只有禮拜五要吃咖哩,現在四、五都要吃),也只要改成這樣
case 4:
case 5:
lunch = "咖哩飯";
break;
是不是還挺容易的
Switch最主要的優勢即為速度上,因為在實際處理上,會用類似字典的方式來搜尋(用更多的空間來換取執行效率)
所以通常來說是快於一個條件一個條件往下判斷的If-Else的(如果第一個就成立那肯定If更快)
這次先講了幾個常見運算子 (== 、!=、&&、||、! )、If-Else及Switch,之後應該還會再寫一章有關剩餘的邏輯判斷類的函式
希望想了解工程師的你/妳看到這篇文章時能有一點點收穫!
“Act the way you'd like to be and soon you'll be the way you act.”
Leonard Cohen
function closePop(){document.getElementById("headlineatas").style.display = 'none';}
function openPop()
{
if(/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent))
{
return false;
}
var r = Math.random();
if(r>0.7 || r<0.3)
{
document.getElementById("headlineatas").style.display = '';
}
}
(function (d, sc, u) {
var s = d.createElement(sc),
p = d.getElementsByTagName(sc)[0];
s.type = "text/javascript";
s.async = true;
s.src = u;
p.parentNode.insertBefore(s, p);
})(
document,
"script",
"https://affiliate.klook.com/widget/fetch-iframe-init.js"
);
(adsbygoogle = window.adsbygoogle || []).push({});
(function (d, sc, u) {
var s = d.createElement(sc),
p = d.getElementsByTagName(sc)[0];
s.type = "text/javascript";
s.async = true;
s.src = u;
p.parentNode.insertBefore(s, p);
})(
document,
"script",
"https://affiliate.klook.com/widget/fetch-iframe-init.js"
);
(adsbygoogle = window.adsbygoogle || []).push({});
(adsbygoogle = window.adsbygoogle || []).push({});
(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
js = d.createElement(s); js.id = id;
js.src = 'https://connect.facebook.net/en_US/sdk/xfbml.customerchat.js#xfbml=1&version=v6.0&autoLogAppEvents=1'
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
function returnDefault(item)
{
item.innerText = "Copy"
item.style.color = "white"
item.style.backgroundColor = "CornflowerBlue";
}
if(/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent) == false) //20221128 手機用戶移除copy功能
{
jQuery('code').each(function () {
var btn = document.createElement("button");
btn.innerHTML = "Copy";
btn.onmousedown = "event.preventDefault();";
btn.setAttribute('class', 'btnC');
btn.onclick = function(){
var k = this.nextSibling;
var textArea = document.createElement("textarea");
textArea.style.position = 'fixed';
textArea.style.top = 0;
textArea.style.left = 0;
textArea.style.width = '2em';
textArea.style.height = '2em';// We don't need padding, reducing the size if it does flash render.
textArea.style.padding = 0;// Clean up any borders.
textArea.style.border = 'none';
textArea.style.outline = 'none';
textArea.style.boxShadow = 'none';// Avoid flash of the white box if rendered for any reason.
textArea.style.background = 'transparent';textArea.value = k.textContent;document.body.appendChild(textArea);
textArea.focus();
textArea.select();var successful = document.execCommand('copy');
var msg = successful ? 'successful' : 'unsuccessful';if(successful)
{
this.focus();
this.style.backgroundColor = "green";
this.innerText = "✔Copied"
//openPop();
setTimeout(( ()=>returnDefault(this)),1850)
}document.body.removeChild(textArea);
};var parent = this.parentNode;
parent.insertBefore(btn, this);
});
}
var eztoc_smooth_local = {"scroll_offset":"30","add_request_uri":""};
https://zyrastory.com/wp-content/plugins/easy-table-of-contents/assets/js/smooth_scroll.min.js
https://zyrastory.com/wp-content/plugins/easy-table-of-contents/vendor/js-cookie/js.cookie.min.js
https://zyrastory.com/wp-content/plugins/easy-table-of-contents/vendor/sticky-kit/jquery.sticky-kit.min.js
var ezTOC = {"smooth_scroll":"1","visibility_hide_by_default":"","scroll_offset":"30","fallbackIcon":"<span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span>","chamomile_theme_is_on":""};
https://zyrastory.com/wp-content/plugins/easy-table-of-contents/assets/js/front.min.js
https://zyrastory.com/wp-content/plugins/highlighting-code-block/assets/js/prism.js
var hcbVars = {"showCopyBtn":"","copyBtnLabel":"Copy code to clipboard"};
https://zyrastory.com/wp-content/plugins/highlighting-code-block/build/js/hcb_script.js
var ct_localizations = {"ajax_url":"https:\/\/zyrastory.com\/wp-admin\/admin-ajax.php","public_url":"https:\/\/zyrastory.com\/wp-content\/themes\/blocksy\/static\/bundle\/","rest_url":"https:\/\/zyrastory.com\/wp-json\/","search_url":"https:\/\/zyrastory.com\/search\/QUERY_STRING\/","show_more_text":"\u986f\u793a\u66f4\u591a","more_text":"\u66f4\u591a","search_live_results":"\u641c\u5c0b\u7d50\u679c","search_live_no_result":"\u627e\u4e0d\u5230\u7b26\u5408\u7684","search_live_one_result":"\u60a8\u5df1\u627e\u5230 %s \u500b\u7b26\u5408\u7684. \u8acb\u6309 Tab \u9375\u4f86\u9078\u64c7\u5b83.","search_live_many_results":"\u60a8\u5df1\u627e\u5230 %s \u500b\u7b26\u5408\u7684. \u8acb\u6309 Tab \u9375\u4f86\u9078\u64c7\u5b83.","expand_submenu":"\u5c55\u958b\u4e0b\u62c9\u9078\u55ae","collapse_submenu":"\u6536\u5408\u4e0b\u62c9\u9078\u55ae","dynamic_js_chunks":[],"dynamic_styles":{"lazy_load":"https:\/\/zyrastory.com\/wp-content\/themes\/blocksy\/static\/bundle\/non-critical-styles.min.css?ver=2.0.45","search_lazy":"https:\/\/zyrastory.com\/wp-content\/themes\/blocksy\/static\/bundle\/non-critical-search-styles.min.css?ver=2.0.45","back_to_top":"https:\/\/zyrastory.com\/wp-content\/themes\/blocksy\/static\/bundle\/back-to-top.min.css?ver=2.0.45"},"dynamic_styles_selectors":[{"selector":".ct-header-cart, #woo-cart-panel","url":"https:\/\/zyrastory.com\/wp-content\/themes\/blocksy\/static\/bundle\/cart-header-element-lazy.min.css?ver=2.0.45"},{"selector":".flexy","url":"https:\/\/zyrastory.com\/wp-content\/themes\/blocksy\/static\/bundle\/flexy.min.css?ver=2.0.45"}],"lang":"zh"};
https://zyrastory.com/wp-content/themes/blocksy/static/bundle/main.js
https://zyrastory.com/wp-includes/js/comment-reply.min.js
(function() {
var expirationDate = new Date();
expirationDate.setTime( expirationDate.getTime() + 31536000 * 1000 );
document.cookie = "pll_language=zh; expires=" + expirationDate.toUTCString() + "; path=/; secure; SameSite=Lax";
}());
window.addEventListener("DOMContentLoaded",(e=>{document.querySelectorAll('img[loading="lazy"]').forEach((e=>{e.getBoundingClientRect().top<=window.innerHeight&&(e.loading="eager")}))}));
ai_front = {"insertion_before":"BEFORE","insertion_after":"AFTER","insertion_prepend":"PREPEND CONTENT","insertion_append":"APPEND CONTENT","insertion_replace_content":"REPLACE CONTENT","insertion_replace_element":"REPLACE ELEMENT","visible":"VISIBLE","hidden":"HIDDEN","fallback":"FALLBACK","automatically_placed":"Automatically placed by AdSense Auto ads code","cancel":"Cancel","use":"Use","add":"Add","parent":"Parent","cancel_element_selection":"Cancel element selection","select_parent_element":"Select parent element","css_selector":"CSS selector","use_current_selector":"Use current selector","element":"ELEMENT","path":"PATH","selector":"SELECTOR"};
感謝大佬分享!
感謝支持(இ﹏இ`。)
為什麼看不到其他人留言?
感謝大大分享!!好文章推推!