Docstoc

For Loops In Programming Languages

Document Sample
For Loops In Programming Languages Powered By Docstoc
					Syntax

Given an action that must be repeated, for instanc
e, five times, different languages' for loops will
 be written differently. The syntax for a three-ex
pression for loop is nearly identical in all langu
ages that have it, after accounting for different
styles of block termination and so on.

Action Script 3
for (var counter:int = 1; counter <= 5; counter++)
  //statement;

Bash
# first form
for i in 1 2 3 4 5
do
     # must have at least one command in loop*
     echo $i # just print value of i
done

BASIC
For I = 1 to 5;
Print I;
Next I

C
for (initialization; condition;increment/decrements
)
  {
     statement 1;
     statement 2;
         .
         .
  }
    statement n;


C, C++, C# and D
for (int counter = 1; counter <= 5; counter++)
  //statement;
  //can use the statement 'break;' to exit early;
The ISO/IEC 9899:1999 publication (commonly known
as C99) also allows initial declarations in for lo
ops.
[edit]FORTRAN
While using the keyword do instead of for, this ty
pe of FORTRAN do loop behaves similarly to the thr
ee argument for loop in other languages. This exam
ple behaves the same as the others, initializing t
he counter variable to 1, incrementing by 1 each i
teration of the loop and stopping at five (inclusi
ve).
do counter = 1,5,1
  write(*,'(i2)') counter
end do

Java
for(int i = 0; i < 5; i++){
     //perform functions within the loop;
     //can use the statement 'break;' to exit early;

}

# second form
for (( i = 1; i <= 5; i++ ))
do
     # must have at least one command in loop*
     echo $i # just print value of i
done
*Note: In these above languages, an empty loop (i.
e., one with no commands between do and done) is a
 syntax error. If the above loops contained only c
omments, execution would result in the message "sy
ntax error near unexpected token 'done'".

Lua
for i = 1,5 do
     -- statements
end
Generic for-loop making use of closures:
for name,phone,address in contacts() do
     -- contacts() must be an iterator function
end
Pascal
for Counter := 1 to 5 do
  (*statement*);
The numeric-range for loop varies somewhat more. P
ascal would use the above-displayed code, whereas
Perl would use the following:

Perl
for ($counter = 1; $counter <= 5; $counter++) {
  # statements;
}
(Note that for(1..5) { } is really a foreach in Per
l.)

PHP
for ($i=0; $i<5; $i++) {
  # statements;
}

foreach (range(1,5) as $i)
  # statements;

PL/I
do counter = 1 to 5 by 1; /* "by 1" is the default
if not specified */
  /*statements*/;
  end;
The LEAVE statement may be used to exit the loop.
Loops can be labeled, and leave may leave a specif
ic labeled loop in a group of nested loops. Some P
L/I dialects include the ITERATE statement to term
inate the current loop iteration and begin the next.

Python
for counter in range(1, 6): # range(1, 6) gives va
lues from 1 inclusive to 6 exclusive
  # statements

Ruby
for counter in 1..5
  # statements
end

5.times do |counter|   # counter iterates from 0 to
4
  # statements
end

1.upto(5) do |counter|
  # statements
end
Ruby has several possible syntaxes, including the a
bove samples.

Smalltalk
1 to: 5 do: [ :counter | "statements" ]
Contrary to other languages, in Smalltalk a for lo
op is not a language construct but defined in the
class Number as a method with two parameters, the
end value and a closure, using self as start value.

				
DOCUMENT INFO
Shared By:
Stats:
views:10
posted:6/20/2012
language:English
pages:4
Description: This book shows For Loop in most of programming languages
Sameera Dissanayaka Sameera Dissanayaka Mr www.hackkat.blogspot.com
About Hi im Sameera Madushan Dissanayaka .Im undergraduate Student of University Of moratuwa .I love Programming Lnaguages Like Java,C,C++,C# i love to browse internet and love to animals