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 ""; } }
|