Logo

Programming-Idioms

History of Idiom 2 > diff from v16 to v17

Edit summary for version 17 by :

Version 16

2015-10-25, 19:10:31

Version 17

2015-10-29, 14:05:11

Idiom #2 Print Hello 10 times

Loop to execute some code a constant number of times

Idiom #2 Print Hello 10 times

Loop to execute some code a constant number of times

Code
10.times do
  p "Hello"
end
Code
10.times do
  p "Hello"
end
Code
(dotimes [_ 10]
  (println "Hello"))
Code
(dotimes [_ 10]
  (println "Hello"))
Comments bubble
The character _ means we don't want to bind the value to a symbol.
Comments bubble
The character _ means we don't want to bind the value to a symbol.
Doc URL
http://clojuredocs.org/clojure.core/dotimes
Doc URL
http://clojuredocs.org/clojure.core/dotimes
Imports
import std.stdio
Imports
import std.stdio
Code
foreach(i; 0..10)
	writeln("Hello");
Code
foreach(i; 0..10)
	writeln("Hello");
Comments bubble
use foreach with a numeric range instead of for whenever possible to avoid subtle errors. Also allows nice type inferrence.
Comments bubble
use foreach with a numeric range instead of for whenever possible to avoid subtle errors. Also allows nice type inferrence.
Code
for _ in 0..10 { println!("Hello"); }
Code
for _ in 0..10 { println!("Hello"); }
Comments bubble
0..10 syntax creates range iterator.

You can leave out variable name with underscore if you do not want to use it.
Comments bubble
0..10 syntax creates range iterator.

You can leave out variable name with underscore if you do not want to use it.
Demo URL
https://play.rust-lang.org/?code=fn%20main()%20%7B%0A%20%20%20%20for%20_%20in%200..10%20%7B%0A%20%20%20%20%20%20%20%20println!(%22Hello%22)%3B%0A%20%20%20%20%7D%0A%7D&version=stable
Demo URL
https://play.rust-lang.org/?code=fn%20main()%20%7B%0A%20%20%20%20for%20_%20in%200..10%20%7B%0A%20%20%20%20%20%20%20%20println!(%22Hello%22)%3B%0A%20%20%20%20%7D%0A%7D&version=stable
Code
print "Hello\n" for 1 .. 10;
Code
print "Hello\n" for 1 .. 10;
Comments bubble
Note the necessary newline (\n), not to print every time on the same line
Comments bubble
Note the necessary newline (\n), not to print every time on the same line
Code
for (var i = 0; i < 10; i++) { 
  print("Hello");
} 
Code
for (var i = 0; i < 10; i++) { 
  print("Hello");
} 
Doc URL
https://www.dartlang.org/docs/dart-up-and-running/contents/ch02.html#for-loops
Doc URL
https://www.dartlang.org/docs/dart-up-and-running/contents/ch02.html#for-loops
Imports
import "fmt"
Imports
import "fmt"
Code
for i:=0 ; i<10 ; i++ {
	fmt.Println("Hello")
}
Code
for i:=0 ; i<10 ; i++ {
	fmt.Println("Hello")
}