晶文摘

電腦五國語言基礎1: 賦值

給晶新聞一個讚


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

一般傳統的電腦語言 BASIC 與 C++,
雖然古老,但一個簡單,一個廣泛。
要拋棄,還真有點困難。
比較新一點的 JAVA, C#, PHP,基本上大同小異,一通百通。
電腦語言的第一步,就是'賦值語句' Assignment Statement。
它就是把一個值,塞入另一變數內。
如 a=1,就是把1 放到a 中。
又如 a=a+1 這不是解方程式,
而是把a+1 的值再放回 a。

以下就是 BASIC,C++, JAVA, C# 與 PHP 等語言的示範。
基本內容就是:

' Value Assignment 數值設定
iA = 1*2+3/4-5
iB = 1*(2+3)/4-5
iC =iA  Mod  2

' 列印 iA,iB,iC

' String Assignment 字串設定
sS="abc"

' String Catenation 字串連接
sS=sS+"<>"
sS=sS+"def"

' 列印 sS


BASIC:
        ' *******************************************
        '  Assignment Statement BASIC
        ' *******************************************
        Sub AssignmentStatement()
            Dim iA as Integer
            Dim iB as Integer
            Dim iC as Integer
            Dim sS as String
            ' Value Assignment
            iA = 1*2+3/4-5
            iB = 1*(2+3)/4-5
            iC =iA  Mod  2
            Debug.Print " ";iA;
            Debug.Print " ";iB;
            Debug.Print " ";iC;
            Debug.Print
            ' String Assignment
            sS="abc"
            ' String Catenation
            sS=sS+"<>"
            sS=sS+"def"
            Debug.Print " ";"sS=";
            Debug.Print " ";sS;
            Debug.Print
        End Sub

C++:
        /* ******************************************* */
        /* Assignment Statement  C++                                    */
        /* ******************************************* */
        void AssignmentStatement() {
            int iA;
            int iB;
            int iC;
            char* sS;
            /* Value Assignment */
            iA = 1*2+3/4-5;
            iB = 1*(2+3)/4-5;
            iC =iA % 2;
            printf(" %d" , iA);
            printf(" %d" , iB);
            printf(" %d" , iC);
            printf("\n\r" );
            /* char* Assignment */
            vstrcpy((char**)&sS, "abc");
            /* char* Catenation */
            vstrcat((char**)&sS, "<>");
            vstrcat((char**)&sS, "def");
            printf(" %s" , "sS=");
            printf(" %s" , sS);
            printf("\n\r" );
            vfree((char*)sS);
        }

JAVA:
        // *******************************************
        // Assignment Statement JAVA
        // *******************************************
        public void AssignmentStatement()  {
            int iA;
            int iB;
            int iC;
            String sS = null;
            // Value Assignment
            iA = 1*2+3/4-5;
            iB = 1*(2+3)/4-5;
            iC =iA % 2;
            System.out.print(" "+iA );
            System.out.print(" "+iB );
            System.out.print(" "+iC );
            System.out.println("");
            // String Assignment
            sS="abc";
            // String Catenation
            sS=sS+"<>";
            sS=sS+"def";
            System.out.print(" "+"sS=" );
            System.out.print(" "+sS );
            System.out.println("");
        }

C#:
        /* ******************************************* */
        /* Assignment Statement C#                                        */
        /* ******************************************* */
        public void AssignmentStatement()  {
            int iA;
            int iB;
            int iC;
            String sS = null;
            /* Value Assignment */
            iA = 1*2+3/4-5;
            iB = 1*(2+3)/4-5;
            iC =iA % 2;
            Console.Write(" "+iA);
            Console.Write(" "+iB);
            Console.Write(" "+iC);
            Console.WriteLine("");
            /* String Assignment */
            sS="abc";
            /* String Catenation */
            sS=sS+"<>";
            sS=sS+"def";
            Console.Write(" "+"sS=");
            Console.Write(" "+sS);
            Console.WriteLine("");
        }

PHP:
        /* ******************************************* */
        /* Assignment Statement  PHP                                    */
        /* ******************************************* */
        function AssignmentStatement()  {
            /* Value Assignment */
            $iA = 1*2+3/4-5;
            $iB = 1*(2+3)/4-5;
            $iC =$iA % 2;
            echo " ".$iA;
            echo " ".$iB;
            echo " ".$iC;
            echo "";
            /* String Assignment */
            $sS="abc";
            /* String Catenation */
            $sS.="<>";
            $sS.="def";
            echo " "."sS=";
            echo " ".$sS;
            echo "";
        }