Conditional and Control Flow 狀態與流程

Conditional and Control Flow 狀態與流程

Comparisons 比較運算子

So far we've seen: 目前為止,我們見過:

* strings 字串 (e.g. "dogs go woof!”) PS : e.g.表示例如 * numbers 數字 (e.g. 4, 10)

Now let's learn about comparison operators. 現在來學習關於比較運算子。

List of comparison operators: 以下是比較運算子列表

  • > Greater than 大於
  • < Less than 小於
  • <= Less than or equal to 小於等於
  • >= Greater than or equal to 大於等於
  • == Equal to 相等
  • != Not equal to 不相等

Instructions 操作指引

On line 8, use a comparison operator to compare two numbers. Make sure to end your line of code with a semicolon. 在第8行,使用比較運算子來比較兩個數字。記得最後要加上分號。


If statements 條件判斷式

Nice work on comparisons! Now let's see how we can use comparisons to ask yes or no questions. 比較練習做的好!現在讓我們看看利用比較來問點是非題。

Say we want to write a program that asks whether your name is longer than 7 letters. If the answer is yes, we can respond with "You have a long name!" We can do this with an if statement: 比方說來寫一個程式碼用來檢查你的名字是否大於7個字元長度。如果答案為是,我們將回應 “You have a long name!”,意即你的名字很長。我們可以利用條件判斷式來做到:


  $age = 17;

  if( $age > 16 ) {
    echo "You can drive!";
  }

An if statement is made up of the if keyword, a condition like we've seen before, and a pair of curly braces { }. If the answer to the condition is yes, the code inside the curly braces will run. 一個條件判斷式乃是由if 保留字,一個 condition(也就是狀態比較),和一個由大括號包起來的區域所組成。當condition的結果為是,被包在大括號的區域裡頭的程式碼將會被執行。

Instructions 操作指引

1. On line 7, set $items equal to a number greater than 5. Make sure to put a semicolon at the end of the line. 2. On line 9, edit the condition so that your program will print out You get a 10% discount! 1.在第7行,設定 $items 變數 存入一個大於5的數字,記得用分號結束。 2.在第9行,編輯 condition狀態比較,確保結果為是,從而讓結果能印出 You get a 10% discount! (你得到10%的折扣)


Adding an Else 加上Else

Great! We used an if statement to do something if the answer to the condition was yes, or true as we say in PHP. 太棒了!我們使用條件判斷式來作一些事情,當condition狀態比較為是的時候,一般在PHP裡頭我們會用 true 來稱呼。

In addition to doing something when the condition is true, we can do something else if the condition is false. We can do this using an if / else statement: 除了可以在condition狀態為 true 的時候作一些事情,我們也能夠在條件狀態為 false(否)的時候做事情,透過 if / else 判斷式:


  $name = "Edgar";

  if ($name == "Simon") {
    print "I know you!";
  }
  else {
    print "Who are you?";
  }

Just like before, if the condition is true, then only the code inside the first pair of curly braces will run. Otherwise, the condition is false, so only the code inside the second pair of curly braces after the else keyword will run. 就像之前,假如狀態比較是 true ,那只有被包在 if 保留字後面的大括號區域內容程式碼會被執行。否則,當狀態結果為 false ,則只有在 else 保留字後面的大括號區域內容程式碼被執行。

In the example above the condition $name== “Simon” evaluates to false since $name is Edgar. Since the condition is false, only the code inside the curly braces after the else keyword runs, and prints out Who are you? 在上面例子,條件比較 $name 是否相等於 “Simon” ,因為 $name 的值是 Edgar ,Condition 比較為 false ,所以只有在 else 保留字後面的大括號程式碼被執行,因而印出 “Who are you?” ,意即你是誰?

Instructions 操作指引

Under your if statement on line 12, write an else statement to capture the people who are only buying 5 items or fewer. In their case, use echo to output "You get a 5% discount!" 在你第12行 if 條件式下面,加上 else 條件式,來捕捉當用戶只買5件或更少的情況。在這種情況下,使用 echo 來輸出 “You get a 5% discount!”,意即你得到5%的折扣!


All Together Now! 結合起來練習

Great work! Now let's practice using if / else statements. Do as much as you can by yourself, but if you need a reminder, click the "Stuck? Get a hint!" button below. 幹得好! 現在來練習 if / else 條件判斷式。可以的話多作一些練習嘗試,如果需要一點提醒的話,可以按下 “Stuck? Get a hint!”按鈕,就在下方

Instructions 操作指引

01.On line 8, write an if / elsestatement, just like we did in the last exercise. Here's what the outline of the code looked like: 在第8行,寫一個 if / else 判斷式,就如同我們之前所做的練習。程式碼大體就像是下方的程式:


  if (this condition is true) {
    // do this code
  }
  else {
    // do this code instead
  }

01.If your condition is true, your code should echo "The condition is true” 假如你的狀態比較是 true ,你的程式碼應該要輸出 “The condition is true”

02.Otherwise (else) when it is false, your code should echo "The condition is false”. 否則當狀態為 false ,你的程式碼應該要輸出 “The condition is false”

03.Make sure your condition evaluates to false, so that your program prints out "The condition is false”. 確保你的狀態比較結果為 false ,這樣的話,結果應該要印出 “The condition is false”

分享這篇文章:

發佈自 林品爵

林品爵
一個堅信程式教育能夠改變下一代未來的狂熱者,青少年揚帆計畫的推動者。十年以上全端程式開發,上百場程式教學經驗,累積學生數達百人以上。 成大學士畢業,使用過J2EE.PHP.MySQL.CoronaSDK.Android.Objective-C.Swift.Python等技術知識,歷任友訊科技總部Java工程師首席.黑快馬股份有限公司研發經理.諾亞數碼娛樂執行長等職位,現任哥布林程式教育學苑創辦人,所開發過專案涵括產品管理系統.POS系統.拍賣平台.擴增實境.問券.社區App等等。 程式啟蒙班講師 . Laravel網頁程式班講師 . App程式班講師

關聯文章:

留下留言