Loops/For with a specified step: Difference between revisions
(Logo) |
(added php, python3) |
||
Line 31: | Line 31: | ||
for [i 2 8 2] [type :i type "|, |] print [who do we appreciate?] |
for [i 2 8 2] [type :i type "|, |] print [who do we appreciate?] |
||
</lang> |
</lang> |
||
=={{header|PHP}}== |
|||
<lang php><?php |
|||
foreach (range(2, 8, 2) as $i) |
|||
echo "$i, "; |
|||
echo "who do we appreciate?\n"; |
|||
?></lang> |
|||
Output |
|||
<pre>2, 4, 6, 8, who do we appreciate?</pre> |
|||
=={{header|Python}}== |
=={{header|Python}}== |
||
{{works with|Python|2.x}} |
|||
⚫ | |||
<lang python>for i in xrange(2, 9, 2): |
|||
print "%d," % i, |
print "%d," % i, |
||
print "who do we appreciate?"</lang> |
print "who do we appreciate?"</lang> |
||
{{works with|Python|3.x}} |
|||
⚫ | |||
print("%d, " % i, end="") |
|||
print("who do we appreciate?")</lang> |
|||
Output |
Output |
||
<pre>2, 4, 6, 8, who do we appreciate?</pre> |
<pre>2, 4, 6, 8, who do we appreciate?</pre> |
Revision as of 19:06, 11 July 2009
![Task](http://static.miraheze.org/rosettacodewiki/thumb/b/ba/Rcode-button-task-crushed.png/64px-Rcode-button-task-crushed.png)
You are encouraged to solve this task according to the task description, using any language you may know.
Demonstrate a for loop where the step value is greater than one.
BASIC
<lang qbasic>for i = 2 to 8 step 2
print i; ", ";
next i print "who do we appreciate?"</lang>
Forth
<lang forth>
- test
9 2 do i . 2 +loop ." who do we appreciate?" cr ;
</lang>
Haskell
<lang haskell>import Control.Monad (forM_) main = do forM_ [2,4..8] (\x -> putStr (show x ++ ", "))
putStrLn "who do we appreciate?"</lang>
Java
<lang java>for(int i = 2; i <= 8;i += 2){
System.out.print(i + ", ");
} System.out.println("who do we appreciate?");</lang>
Logo
<lang logo> for [i 2 8 2] [type :i type "|, |] print [who do we appreciate?] </lang>
PHP
<lang php><?php foreach (range(2, 8, 2) as $i)
echo "$i, ";
echo "who do we appreciate?\n"; ?></lang> Output
2, 4, 6, 8, who do we appreciate?
Python
<lang python>for i in xrange(2, 9, 2):
print "%d," % i,
print "who do we appreciate?"</lang>
<lang python>for i in range(2, 9, 2):
print("%d, " % i, end="")
print("who do we appreciate?")</lang> Output
2, 4, 6, 8, who do we appreciate?
Ruby
<lang ruby>2.step(8,2) {|n| print "#{n}, "} puts "who do we appreciate?"</lang> or: <lang ruby>(2..8).step(2) {|n| print "#{n}, "} puts "who do we appreciate?"</lang> Output
2, 4, 6, 8, who do we appreciate?
Tcl
<lang tcl>for {set i 2} {$i <= 8} {incr i 2} {
puts -nonewline "$i, "
} puts "enough with the cheering already!"</lang>