晶文摘

電腦五國語言基礎3: 如果 則 否則 IF Then Else

給晶新聞一個讚




(夏肇毅 製圖)

作者:夏肇毅 2010-12-16 初版

要根據不同狀況
叫電腦做不同的事
就要用 IF 判斷
如果 "If  判斷式 Then" 判斷式正確
那就會往下執行
否則
就會找下個 "ElseIf ...Then" 繼續判斷
如果還是不對
那就只好執行 "Else" 以後的指令了
如果沒有 "Else"
那就會執行 End If 以後的指令
BASIC之外的語言
會用 "{...}" 把一串指令包成一組
如果要執行的話 就執行這一整組指令

BASIC:
        ' *******************************************
        ' Condition statement (If..ElseIf..Else..End If)
        ' *******************************************
        Sub IfStatement()
            Dim dPara1 as Double
            Dim dPara2 as Double
            dPara1 = 1.0
            dPara2 = 2.0
            ' Condition statement (If..ElseIf..Else..End If)
            If dPara1 > dPara2 Then
                ' Is Greater Than
                Debug.Print " ";"dPara1 > dPara2";
                Debug.Print
            ElseIf dPara1  = dPara2 Then
                ' Equals to
                Debug.Print " ";"dPara1 ==dPara2";
                Debug.Print
            ElseIf dPara1 < dPara2 Then
                ' Is Less Than
                Debug.Print " ";"dPara1 < dPara2";
                Debug.Print
            End If
            If dPara1 <> dPara2 Then
                ' not equal to
                Debug.Print " ";"dPara1 <> dPara2";
                Debug.Print
            End If
            ' And Logic
            If dPara1 > dPara2 AND dPara1  = dPara2 Then
                Debug.Print " ";"dPara1 >=dPara2";
                Debug.Print
            ' Or Logic
            ElseIf dPara1 > dPara2 OR dPara1 < dPara2 Then
                Debug.Print " ";"dPara1 <> dPara2";
                Debug.Print
            End If
        End Sub
 

C++:
        /* ******************************************* */
        /* Condition statement (If..ElseIf..Else..End If) */
        /* ******************************************* */
        void IfStatement() {
            double dPara1;
            double dPara2;
            dPara1 = 1.0;
            dPara2 = 2.0;
            /* Condition statement (If..ElseIf..Else..End If) */
            if (dPara1 > dPara2) {
                /* Is Greater Than */
                printf(" %s" , "dPara1 > dPara2");
                printf("\n\r" );
            } else if (dPara1 ==dPara2) {
                /* Equals to */
                printf(" %s" , "dPara1 ==dPara2");
                printf("\n\r" );
            } else if (dPara1 < dPara2) {
                /* Is Less Than */
                printf(" %s" , "dPara1 < dPara2");
                printf("\n\r" );
            }
            if (dPara1 != dPara2) {
                /* not equal to */
                printf(" %s" , "dPara1 <> dPara2");
                printf("\n\r" );
            }
            /* And Logic */
            if (dPara1 > dPara2 && dPara1 ==dPara2) {
                printf(" %s" , "dPara1 >=dPara2");
                printf("\n\r" );
            /* Or Logic */
            } else if (dPara1 > dPara2 || dPara1 < dPara2) {
                printf(" %s" , "dPara1 <> dPara2");
                printf("\n\r" );
            }
        }

JAVA:   
        // *******************************************
        // Condition statement (If..ElseIf..Else..End If)
        // *******************************************
        public void IfStatement()  {
            double dPara1;
            double dPara2;
            dPara1 = 1.0;
            dPara2 = 2.0;
            // Condition statement (If..ElseIf..Else..End If)
            if (dPara1 > dPara2) {
                // Is Greater Than
                System.out.print(" "+"dPara1 > dPara2" );
                System.out.println("");
            } else if (dPara1 ==dPara2) {
                // Equals to
                System.out.print(" "+"dPara1 ==dPara2" );
                System.out.println("");
            } else if (dPara1 < dPara2) {
                // Is Less Than
                System.out.print(" "+"dPara1 < dPara2" );
                System.out.println("");
            }
            if (dPara1 != dPara2) {
                // not equal to
                System.out.print(" "+"dPara1 <> dPara2" );
                System.out.println("");
            }
            // And Logic
            if (dPara1 > dPara2 && dPara1 ==dPara2) {
                System.out.print(" "+"dPara1 >=dPara2" );
                System.out.println("");
            // Or Logic
            } else if (dPara1 > dPara2 || dPara1 < dPara2) {
                System.out.print(" "+"dPara1 <> dPara2" );
                System.out.println("");
            }
        }
   

C#:
        /* ******************************************* */
        /* Condition statement (If..ElseIf..Else..End If) */
        /* ******************************************* */
        public void IfStatement()  {
            double dPara1;
            double dPara2;
            dPara1 = 1.0;
            dPara2 = 2.0;
            /* Condition statement (If..ElseIf..Else..End If) */
            if (dPara1 > dPara2) {
                /* Is Greater Than */
                Console.Write(" "+"dPara1 > dPara2");
                Console.WriteLine("");
            } else if (dPara1 ==dPara2) {
                /* Equals to */
                Console.Write(" "+"dPara1 ==dPara2");
                Console.WriteLine("");
            } else if (dPara1 < dPara2) {
                /* Is Less Than */
                Console.Write(" "+"dPara1 < dPara2");
                Console.WriteLine("");
            }
            if (dPara1 != dPara2) {
                /* not equal to */
                Console.Write(" "+"dPara1 <> dPara2");
                Console.WriteLine("");
            }
            /* And Logic */
            if (dPara1 > dPara2 && dPara1 ==dPara2) {
                Console.Write(" "+"dPara1 >=dPara2");
                Console.WriteLine("");
            /* Or Logic */
            } else if (dPara1 > dPara2 || dPara1 < dPara2) {
                Console.Write(" "+"dPara1 <> dPara2");
                Console.WriteLine("");
            }
        }


PHP:   
        /* ******************************************* */
        /* Condition statement (If..ElseIf..Else..End If) */
        /* ******************************************* */
        function IfStatement()  {
            $dPara1 = 1.0;
            $dPara2 = 2.0;
            /* Condition statement (If..ElseIf..Else..End If) */
            if ($dPara1 > $dPara2) {
                /* Is Greater Than */
                echo " "."dPara1 > dPara2";
                echo "";
            } elseif ($dPara1 ==$dPara2) {
                /* Equals to */
                echo " "."dPara1 ==dPara2";
                echo "";
            } elseif ($dPara1 < $dPara2) {
                /* Is Less Than */
                echo " "."dPara1 < dPara2";
                echo "";
            }
            if ($dPara1 != $dPara2) {
                /* not equal to */
                echo " "."dPara1 <> dPara2";
                echo "";
            }
            /* And Logic */
            if ($dPara1 > $dPara2 && $dPara1 ==$dPara2) {
                echo " "."dPara1 >=dPara2";
                echo "";
            /* Or Logic */
            } elseif ($dPara1 > $dPara2 || $dPara1 < $dPara2) {
                echo " "."dPara1 <> dPara2";
                echo "";
            }
        }