Loops/For with a specified step: Difference between revisions

From Rosetta Code
Content added Content deleted
(Created page with '{{task|Iteration}} Demonstrate a for loop where the step value is greater than one. =={{header|Ruby}}== <lang ruby>2.step(8,2) {|n| print "#{n}, "} puts "who do we appreciate?"<…')
 
(Added Java and BASIC)
Line 1: Line 1:
{{task|Iteration}}
{{task|Iteration}}
Demonstrate a for loop where the step value is greater than one.
Demonstrate a for loop where the step value is greater than one.
=={{header|BASIC}}==

{{works with|QuickBasic|4.5}}
<lang qbasic>for i = 2 to 8 step 2
print i; ", ";
next i
print "who do we appreciate?"</lang>
=={{header|Java}}==
<lang java>for(int i = 2; i <= 8;i += 2){
System.out.print(i + ", ");
}
System.out.println("who do we appreciate?");</lang>
=={{header|Ruby}}==
=={{header|Ruby}}==
<lang ruby>2.step(8,2) {|n| print "#{n}, "}
<lang ruby>2.step(8,2) {|n| print "#{n}, "}

Revision as of 18:25, 10 July 2009

Task
Loops/For with a specified step
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

Works with: QuickBasic version 4.5

<lang qbasic>for i = 2 to 8 step 2

  print i; ", ";

next i print "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>

Ruby

<lang ruby>2.step(8,2) {|n| print "#{n}, "} puts "who do we appreciate?"</lang> Output

2, 4, 6, 8, who do we appreciate?