RosettaCodeData/Task/Loops-Increment-loop-index-within-loop-body/00-TASK.txt
2023-07-01 11:58:00 -04:00

53 lines
1.7 KiB
Text

Sometimes, one may need   (or want)   a loop which
its   ''iterator''   (the index
variable)   is modified within the
<br>loop body &nbsp; '' in addition to the normal incrementation by the &nbsp; ('''do''') &nbsp; loop structure index''.
;Goal:
Demonstrate the best way to accomplish this.
<!-- ··· with a nod to Douglas Adams (42 being the ultimate answer to THE question). !-->
;Task:
Write a loop which:
::* &nbsp; starts the index (variable) at &nbsp; '''42'''
::* &nbsp; (at iteration time) &nbsp; increments the index by unity
::* &nbsp; if the index is prime:
::::* &nbsp; displays the count of primes found (so far) and the prime &nbsp; (to the terminal)
::::* &nbsp; increments the index such that the new index is now the (old) index plus that prime
::* &nbsp; terminates the loop when &nbsp; '''42''' &nbsp; primes are shown
Extra credit: &nbsp; because of the primes get rather large, use commas
within the displayed primes to ease comprehension.
Show all output here.
;Note:
Not all programming languages allow the modification of a
loop's index. &nbsp; If that is the case, then use whatever method that
is appropriate or idiomatic for that language. &nbsp; Please add a note
if the loop's index isn't modifiable.
;Related tasks:
* &nbsp; [[Loop over multiple arrays simultaneously]]
* &nbsp; [[Loops/Break]]
* &nbsp; [[Loops/Continue]]
* &nbsp; [[Loops/Do-while]]
* &nbsp; [[Loops/Downward for]]
* &nbsp; [[Loops/For]]
* &nbsp; [[Loops/For with a specified step]]
* &nbsp; [[Loops/Foreach]]
* &nbsp; [[Loops/Infinite]]
* &nbsp; [[Loops/N plus one half]]
* &nbsp; [[Loops/Nested]]
* &nbsp; [[Loops/While]]
* &nbsp; [[Loops/with multiple ranges]]
* &nbsp; [[Loops/Wrong ranges]]
<br><br>