Ruby For Loop

Ruby For Loop

The Ruby For Loop statement syntax and example.

For loop syntax

for value in expression do
	-- Ruby code 
end

For loop example

$x = 1

for $x in [1,2,3,4,5] do
   puts("The value of x = #$x" )
end

Output

The value of x = 1

The value of x = 2

The value of x = 3

The value of x = 4

The value of x = 5