In most imperative computer programming languages, a for loop is a control structure which allows code to be executed iteratively. For loops, unlike while loops, are typically used when the number of iterations is known before entering the loop.

Contents

  • 1 Examples
    • 1.1 QBasic or Visual Basic
    • 1.2 C or C++
    • 1.3 Python
    • 1.4 Java, Csharp
    • 1.5 JavaScript
    • 1.6 Perl
    • 1.7 Pascal
  • 2 Equivalence with while loops
  • 3 See also

Examples

These for loops will calculate the factorial of the number five, 5!, by iterating through all the numbers 1 through 5 and keeping a running product:

QBasic or Visual Basic

 Dim factorial As Long : factorial = 1
 Dim counter As Integer
 For counter = 1 To 5
   factorial = factorial * counter
 Next
 Print factorial

C or C++

 unsigned long factorial = 1;
 for (unsigned int counter = 1; counter <= 5; counter++)
   factorial *= counter;
 printf("%i", factorial);

Python

 factorial = 1
 for counter in range(1, 6):    # range() gives values to one less than the second argument
   factorial *= counter
 print factorial

Java, Csharp

The syntax for the for loop is the same for Java and Csharp

 long factorial = 1;
 for (int counter = 1; counter <= 5; counter++)
   factorial *= counter;

Output of the result in Java

 System.out.println(factorial);

In Csharp

 System.Console.WriteLine(factorial);

JavaScript

var factorial = 1;
for (var counter = 1; counter <= 5; counter++){
  factorial *= counter;
document.write(factorial);
}

Perl

my $factorial = 1;
for ( my $counter = 1; $counter <= 5; $counter++ ) {
    $factorial *= $counter;
}
print $factorial;


Pascal

 program Factorial
 var
   Counter, Factorial: integer;
 begin
   Factorial := 1;
   for Counter := 1 to 5 do begin
     Factorial := Factorial * Counter;
   end;
   Write(Factorial);
 end.

Equivalence with while loops

A for loop can always be converted into an equivalent while loop by incrementing a counter variable directly. The following pseudocode illustrates this technique:

 factorial := 1
 for counter from 1 to 5:
     factorial := factorial * counter

is easily translated into the following while loop:

 factorial := 1
 counter := 1
 while counter <= 5:
    factorial := factorial * counter
    counter := counter + 1

See also

  • While loop
  • Do while loop
  • Loop counterde:For-Schleife

ja:For文

"For_loop"

 

Related News



Top Related Terms

1. all i want for christmas is you
2. teen for cash
3. for sale
4. body for life
5. tears for fear
6. curve for woman
7. for better or for worse
8. room for rent
9. crazy for you
10. what you waiting for
11. for eyes
12. all for one
13. for better or worse
14. for you
15. thats what friend are for
16. one for all
17. a rose for emily
18. bowling for columbine
19. equipment used for boating
20. just for foot
21. all for you
22. for your eyes only
23. to die for
24. blood for blood
25. for dummy
26. equipment used for snowboarding
27. ready for love
28. waiting for godot
29. time for kids.com
30. for woman
31. equipment used for skiing
32. ill be there for you
33. free for all
34. equipment used for hiking
35. in the mood for love
36. code for game
37. home for sale.com
38. first for woman
39. for child
40. for people
41. for once in my life
42. free game for pc
43. move for free
44. for the bird
45. for me
46. for eyes optical
47. a feast for crow
48. for life
49. for what
50. for always
51. for all
52. speaker for sale
53. out for justice
54. out for a kill
55. what for
56. for a
57. for the
58. out for blood
59. up for grab
60. home for sale portland or
61. crazy for love
62. for me and my
63. code for pc game
64. a is for
65. for your
66. sign in for msn hotmail
67. all for
68. for this cause
69. home for sale in portland or
70. equipment used for bicycling
71. for the masses
72. how for all
73. all for me
74. crazy for cross stitch
75. on for
76. the hunt for bin laden
77. do it for love
78. be for you
79. for my
80. me for you
81. out for me
82. me for me
83. for an
84. r is for ricochet
85. do for
86. on for a
87. on for the
88. to and for
89. apartment for rent portland or
90. careful what you wish for