Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
|
|
@ -1,7 +1,10 @@
|
|||
Quite often one needs loops which, in the last iteration, execute only part of the loop body. The goal of this task is to demonstrate the best way to do this.
|
||||
Quite often one needs loops which, in the last iteration,
|
||||
execute only part of the loop body.
|
||||
The goal of this task is to demonstrate the best way to do this.
|
||||
|
||||
Write a loop which writes the comma-separated list
|
||||
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
|
||||
using separate output statements for the number and the comma from within the body of the loop.
|
||||
using separate output statements for the number
|
||||
and the comma from within the body of the loop.
|
||||
|
||||
See also: [[Loop/Break]]
|
||||
|
|
|
|||
|
|
@ -1,2 +1,4 @@
|
|||
---
|
||||
category:
|
||||
- Simple
|
||||
note: Iteration
|
||||
|
|
|
|||
|
|
@ -1,2 +1 @@
|
|||
$ awk 'BEGIN{for(i=1;i<=10;i++){printf i;if(i<10)printf ", "};print}'
|
||||
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
BEGIN {
|
||||
n=10
|
||||
for(i=1;i<=n;i++) {
|
||||
printf i;
|
||||
if(i<n) printf ", "
|
||||
}
|
||||
print
|
||||
}
|
||||
|
|
@ -6,7 +6,7 @@ func main() {
|
|||
for i := 1; ; i++ {
|
||||
fmt.Print(i)
|
||||
if i == 10 {
|
||||
fmt.Println("")
|
||||
fmt.Println()
|
||||
break
|
||||
}
|
||||
fmt.Print(", ")
|
||||
|
|
|
|||
|
|
@ -0,0 +1,5 @@
|
|||
for i = 1:10
|
||||
print(i)
|
||||
i == 10 && break
|
||||
print(", ")
|
||||
end
|
||||
|
|
@ -0,0 +1 @@
|
|||
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
> for i to 10 do printf( "%d%s", i, `if`( i = 10, "\n", ", " ) ) end:
|
||||
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
(for (i 0 10)
|
||||
(print i)
|
||||
(unless (= i 10)
|
||||
(print ", ")))
|
||||
|
|
@ -1,14 +1,20 @@
|
|||
program numlist(output);
|
||||
|
||||
const MAXNUM: integer = 10;
|
||||
var
|
||||
i: integer;
|
||||
|
||||
begin
|
||||
for i := 1 to 10 do
|
||||
{ loop 1: w/ if branching }
|
||||
for i := 1 to MAXNUM do
|
||||
begin
|
||||
write(i);
|
||||
if i <> 10 then
|
||||
if i <> MAXNUM then
|
||||
write(', ')
|
||||
end;
|
||||
writeln;
|
||||
{ loop 2: w/o if branching }
|
||||
for i := 1 to MAXNUM-1 do
|
||||
write(i, ', ');
|
||||
writeln(MAXNUM);
|
||||
end.
|
||||
|
|
|
|||
|
|
@ -0,0 +1,7 @@
|
|||
list='aa bb cc dd'
|
||||
sep=', '
|
||||
Do i=1 By 1 While list<>''
|
||||
If i>1 Then Call charout ,sep
|
||||
Parse Var list item list
|
||||
Call charout ,item
|
||||
End
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
(1..10).each do |i|
|
||||
print i
|
||||
break if i == 10
|
||||
print ", "
|
||||
end
|
||||
puts
|
||||
|
|
@ -0,0 +1 @@
|
|||
puts (1..10).join(", ")
|
||||
|
|
@ -1,6 +0,0 @@
|
|||
for i in 1..10 do
|
||||
print i
|
||||
break if i == 10
|
||||
print ", "
|
||||
end
|
||||
puts
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
object LoopAndHalf extends App {
|
||||
println((1 to 10).mkString(", "))
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
for i in range(1, 10)
|
||||
echon i
|
||||
if (i != 10)
|
||||
echon ", "
|
||||
endif
|
||||
endfor
|
||||
Loading…
Add table
Add a link
Reference in a new issue