網路城邦
上一篇 回創作列表 下一篇   字體:
夏肇毅知識運算網-一次學會五種程式設計語言-Part 2 For..Next
2016/04/23 08:36:19瀏覽274|回應0|推薦3

[夏肇毅知識運算網 Mark Hsia's Knowledge Computing Web]

一個指令
當我們想重複做很多次時
我們可以重複複製很多次
或者就用 For....Next 迴圈
指定一個變數
讓它由起值變到終值
如  For i=1 to 5
就是讓i由1 變到5
每次加1
執行到 Next 時
則跳回重複
當i>5 時
就不再跳回
直接向下走了

以下就是讓 i 由1 到5
同時把自己列印出來的例子

2016-04-19

Learn Coding Five Languages at once: For Statement

If we want to perform an instruction many times, we can copy this instruction many times or use a "For...Next" block. This "For" statement assigns an initial value to a variable and change it up to the end value stepping with +1 or -1.

For example, "For i=1 to 5 " statement assign value 1 to the variable i and loop it up to the end value 5 with stepping value 1 each time. When program meets the "Next"  statement, it will jump back to the "For" statement. But if the variable exceeds the end value, the program will jump to the statement next to "Next".

Here we show the example of looping five times and print the value of the variable i in five different programming languages:

BASIC:
        ' *******************************************
        ' for loop statement (For..Next)
        ' *******************************************
        Sub ForLoop()
            ' Declare local variable
            Dim i as Integer
            Debug.Print " ";"**** For statement ***";
            Debug.Print
            For i=1 to 5
                Debug.Print " ";"i=";
                Debug.Print " ";i;
                Debug.Print
            Next
        End Sub

C++:
        /* ******************************************* */
        /* for loop statement (For..Next) */
        /* ******************************************* */
        void ForLoop() {
            /* Declare local variable */
            int i;
            printf(" %s" , "**** For statement ***");
            printf("\n" );
            for (i=1; i<= 5="" i="" span="">
                printf(" %s" , "i=");
                printf(" %d" , i);
                printf("\n" );
            }
        }

JAVA:
        // *******************************************
        // for loop statement (For..Next)
        // *******************************************
        public void ForLoop()  {
            // Declare local variable
            int i;
            System.out.print(" "+"**** For statement ***" );
            System.out.println("");
            for (i=1; i<= 5="" i="" span="">
                System.out.print(" "+"i=" );
                System.out.print(" "+i );
                System.out.println("");
            }
        }
   
C#:
        /* ******************************************* */
        /* for loop statement (For..Next) */
        /* ******************************************* */
        public void ForLoop()  {
            /* Declare local variable */
            int i;
            Console.Write(" "+"**** For statement ***");
            Console.WriteLine("");
            for (i=1; i<= 5="" i="" span="">
                Console.Write(" "+"i=");
                Console.Write(" "+i);
                Console.WriteLine("");
            }
        }

PHP:   
        /* ******************************************* */
        /* for loop statement (For..Next) */
        /* ******************************************* */
        function ForLoop()  {
            /* Declare local variable */
            echo " "."**** For statement ***";
            echo "";
            for ($i=1; $i<= 5="" i="" span="">
                echo " "."i=";
                echo " ".$i;
                echo "";
            }
        }
   
( 知識學習隨堂筆記 )
回應 推薦文章 列印 加入我的文摘
上一篇 回創作列表 下一篇

引用
引用網址:https://classic-blog.udn.com/article/trackback.jsp?uid=markhsia&aid=54150567