# C(++) For Loop Equivalents

**URL:** <https://rubytalk.org/t/c-for-loop-equivalents/36277>\
**Category:** ruby-talk\
**Created:** [20 March 2007 00:30 UTC](https://rubytalk.org/t/c-for-loop-equivalents/36277 "2007-03-20T00:30:05Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![\_Stud](https://avatars.discourse-cdn.com/v4/letter/_/f05b48/32.png) [@\_Stud](https://rubytalk.org/u/_Stud)\
**Post date:** [20 March 2007 00:30 UTC](https://rubytalk.org/t/c-for-loop-equivalents/36277/1 "2007-03-20T00:30:05Z")

</div>

What is the most efficient way in Ruby to create a conditional loop in  
Ruby that looks like this in C(++):

for(int i=0; i\*j/2\<maxNum; i++)  
{  
&nbsp;&nbsp;//cool method stuff  
}

I was looking at the .upto() method but it doesn't like the comparison  
operator :-/

Thanks!

---

<div class="post-metadata">

**Author:** ![Austin\_Ziegler5](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/austin_ziegler5/32/1985_2.png) [@Austin\_Ziegler5](https://rubytalk.org/u/Austin_Ziegler5)\
**Post date:** [20 March 2007 00:36 UTC](https://rubytalk.org/t/c-for-loop-equivalents/36277/2 "2007-03-20T00:36:26Z")

</div>

> What is the most efficient way in Ruby to create a conditional loop in  
> Ruby that looks like this in C(++):
> 
> for(int i=0; i\*j/2\<maxNum; i++)  
> {  
> &nbsp;&nbsp;//cool method stuff  
> }
> 
> I was looking at the .upto() method but it doesn't like the comparison  
> operator :-/

Wouldn't it be better to figure out what you're trying to do than look  
for a general construct?

&nbsp;&nbsp;\>\> max = 200  
&nbsp;&nbsp;=\> 200  
&nbsp;&nbsp;\>\> j = 35  
&nbsp;&nbsp;=\> 35  
&nbsp;&nbsp;\>\> i = 0  
&nbsp;&nbsp;=\> 0  
&nbsp;&nbsp;\>\> 0.upto(max) { |i| break if (i \* j / 2) \>= max }  
&nbsp;&nbsp;=\> nil  
&nbsp;&nbsp;\>\> i  
&nbsp;&nbsp;=\> 12

That will do the job.

-austin

> **···**
>
> On 3/19/07, studlee2 \<studlee2@gmail.com\> wrote:  
> --  
> Austin Ziegler \* halostatue@gmail.com \* [http://www.halostatue.ca/](http://www.halostatue.ca/)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\* austin@halostatue.ca \* [You are in a maze of twisty little passages, all alike. // halo • statue](http://www.halostatue.ca/feed/)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\* austin@zieglers.ca

---

<div class="post-metadata">

**Author:** ![Daniel\_Sheppard](https://avatars.discourse-cdn.com/v4/letter/d/db5fbb/32.png) [@Daniel\_Sheppard](https://rubytalk.org/u/Daniel_Sheppard)\
**Post date:** [20 March 2007 01:19 UTC](https://rubytalk.org/t/c-for-loop-equivalents/36277/3 "2007-03-20T01:19:40Z")

</div>

> What is the most efficient way in Ruby to create a conditional loop in  
> Ruby that looks like this in C(++):
> 
> for(int i=0; i\*j/2\<maxNum; i++)  
> {  
> &nbsp;&nbsp;//cool method stuff  
> }

In the general case, you can 'while' anywhere where you'd normally 'for'

i=0  
while i\*j/2\<maxNum  
&nbsp;&nbsp;//cool method stuff  
&nbsp;&nbsp;i++  
end

But 'most efficient way' to do a conditional loop depends on your  
context (and also what you're trying to make efficient - runtime?  
writetime? readtime?).

Dan.

---

<div class="post-metadata">

**Author:** ![Raj\_Sahae](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/raj_sahae/32/1840_2.png) [@Raj\_Sahae](https://rubytalk.org/u/Raj_Sahae)\
**Post date:** [20 March 2007 05:04 UTC](https://rubytalk.org/t/c-for-loop-equivalents/36277/4 "2007-03-20T05:04:37Z")

</div>

studlee2 wrote:

> What is the most efficient way in Ruby to create a conditional loop in  
> Ruby that looks like this in C(++):
> 
> for(int i=0; i\*j/2\<maxNum; i++)  
> {  
> &nbsp;&nbsp;//cool method stuff  
> }
> 
> I was looking at the .upto() method but it doesn't like the comparison  
> operator :-/
> 
> Thanks

Why not just do:  
((maxNum\*2)/(i\*j)).times do |n|  
&nbsp;&nbsp;&nbsp;&nbsp;#cool method stuff  
end

---

<div class="post-metadata">

**Author:** ![Daniel\_Finnie1](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@Daniel\_Finnie1](https://rubytalk.org/u/Daniel_Finnie1)\
**Post date:** [20 March 2007 01:27 UTC](https://rubytalk.org/t/c-for-loop-equivalents/36277/5 "2007-03-20T01:27:53Z")

</div>

Use the step method:

----------------------------------------------------------- Numeric#step  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;num.step(limit, step ) {|i| block } =\> num

> **···**
>
> ------------------------------------------------------------------------  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Invokes \_block\_ with the sequence of numbers starting at \_num\_,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;incremented by \_step\_ on each call. The loop finishes when the  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;value to be passed to the block is greater than \_limit\_ (if \_step\_  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;is positive) or less than \_limit\_ (if \_step\_ is negative). If all  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;the arguments are integers, the loop operates using an integer  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;counter. If any of the arguments are floating point numbers, all  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;are converted to floats, and the loop is executed \_floor(n +  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;n\*epsilon)+ 1\_ times, where \_n = (limit - num)/step\_. Otherwise,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;the loop starts at \_num\_, uses either the +\<+ or +\>+ operator to  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;compare the counter against \_limit\_, and increments itself using  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;the +++ operator.
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1.step(10, 2) { |i| print i, " " }  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Math::E.step(Math::PI, 0.2) { |f| print f, " " }
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\_produces:\_
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1 3 5 7 9  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2.71828182845905 2.91828182845905 3.11828182845905
> 
> So your example would become:  
> 0.step(i\*j/2, 1) # (I'm not too sure about the details of C++ and if defining i in the beginning would make i\*j/2 be 0 no matter what)  
> Or, you could do  
> 0.upto(i\*j/2) # assumes step is one.
> 
> Finally, you could do  
> for i in 0..(i\*j/2) do |i|  
> &nbsp;&nbsp;# blah...  
> end
> 
> Daniel Sheppard wrote:
> 
> > > What is the most efficient way in Ruby to create a conditional loop in  
> > > Ruby that looks like this in C(++):
> > > 
> > > for(int i=0; i\*j/2\<maxNum; i++)  
> > > {  
> > > &nbsp;&nbsp;//cool method stuff  
> > > }
> > 
> > In the general case, you can 'while' anywhere where you'd normally 'for'
> > 
> > i=0  
> > while i\*j/2\<maxNum //cool method stuff  
> > &nbsp;&nbsp;i++  
> > end
> > 
> > But 'most efficient way' to do a conditional loop depends on your  
> > context (and also what you're trying to make efficient - runtime?  
> > writetime? readtime?).
> > 
> > Dan.

---

<div class="post-metadata">

**Author:** ![Michael\_W\_Ryder1](https://avatars.discourse-cdn.com/v4/letter/m/8e7dd6/32.png) [@Michael\_W\_Ryder1](https://rubytalk.org/u/Michael_W_Ryder1)\
**Post date:** [20 March 2007 05:30 UTC](https://rubytalk.org/t/c-for-loop-equivalents/36277/6 "2007-03-20T05:30:08Z")

</div>

Raj Sahae wrote:

> studlee2 wrote:
> 
> > What is the most efficient way in Ruby to create a conditional loop in  
> > Ruby that looks like this in C(++):
> > 
> > for(int i=0; i\*j/2\<maxNum; i++)  
> > {  
> > &nbsp;&nbsp;//cool method stuff  
> > }
> > 
> > I was looking at the .upto() method but it doesn't like the comparison  
> > operator :-/
> > 
> > Thanks
> 
> Why not just do:  
> ((maxNum\*2)/(i\*j)).times do |n|  
> &nbsp;&nbsp;&nbsp;#cool method stuff  
> end

He would need to initialize i before your code as it could be any value, not just 0. By the same token the increment could be any number, not just 1.  
A further problem with your method is that the loop is executed until i\*j/2 is greater than maxNum. As you don't know if the loop somehow modifies the value of j you can not optimize it out. A simple example would be where the program reads some numbers from a file and adds them to j. i is incremented by one each time a number is read. You want the program to exit when i\*j/2 is larger than maxNum.

---

<div class="post-metadata">

**Author:** ![\_Pena\_Botp1](https://avatars.discourse-cdn.com/v4/letter/_/94ad74/32.png) [@\_Pena\_Botp1](https://rubytalk.org/u/_Pena_Botp1)\
**Post date:** [20 March 2007 10:24 UTC](https://rubytalk.org/t/c-for-loop-equivalents/36277/7 "2007-03-20T10:24:48Z")

</div>

# He would need to initialize i before your code as it could be  
# any value,  
# not just 0. By the same token the increment could be any number, not  
# just 1.

fwiw, this is a simple example (intended for nubies only).

C:\family\ruby\>cat -n test.rb  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1 #---------------------------  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2 #emulate c++ for (the better)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3 #for(int i=0; i\*j/2\<maxNum; i++)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;4 # {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;5 # //cool method stuff  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;6 # }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;7 #---------------------------  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;8  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;9 # using normal while loop  
&nbsp;&nbsp;&nbsp;&nbsp;10 puts "using while..."  
&nbsp;&nbsp;&nbsp;&nbsp;11 j=20  
&nbsp;&nbsp;&nbsp;&nbsp;12 i=0  
&nbsp;&nbsp;&nbsp;&nbsp;13 maxnum=100  
&nbsp;&nbsp;&nbsp;&nbsp;14 while i\*j/2 \< maxnum do  
&nbsp;&nbsp;&nbsp;&nbsp;15 puts i  
&nbsp;&nbsp;&nbsp;&nbsp;16 i+=1  
&nbsp;&nbsp;&nbsp;&nbsp;17 end  
&nbsp;&nbsp;&nbsp;&nbsp;18  
&nbsp;&nbsp;&nbsp;&nbsp;19 # let's make our own for() and call it for\_simple 🙂  
&nbsp;&nbsp;&nbsp;&nbsp;20 # note, there's no condition here  
&nbsp;&nbsp;&nbsp;&nbsp;21 # we're leaving it up to yield/blocks since that  
&nbsp;&nbsp;&nbsp;&nbsp;22 # is the ideal way, imho  
&nbsp;&nbsp;&nbsp;&nbsp;23 def for\_simple (init=0,incr=1)  
&nbsp;&nbsp;&nbsp;&nbsp;24 i = init  
&nbsp;&nbsp;&nbsp;&nbsp;25 loop do  
&nbsp;&nbsp;&nbsp;&nbsp;26 yield i  
&nbsp;&nbsp;&nbsp;&nbsp;27 i += incr  
&nbsp;&nbsp;&nbsp;&nbsp;28 end  
&nbsp;&nbsp;&nbsp;&nbsp;29 end  
&nbsp;&nbsp;&nbsp;&nbsp;30  
&nbsp;&nbsp;&nbsp;&nbsp;31 # now let's test it  
&nbsp;&nbsp;&nbsp;&nbsp;32 puts \<\<TEXT  
&nbsp;&nbsp;&nbsp;&nbsp;33 Test I.  
&nbsp;&nbsp;&nbsp;&nbsp;34 using our own simple for()... note advantage,  
&nbsp;&nbsp;&nbsp;&nbsp;35 we don't need to track the counter...  
&nbsp;&nbsp;&nbsp;&nbsp;36 TEXT  
&nbsp;&nbsp;&nbsp;&nbsp;37  
&nbsp;&nbsp;&nbsp;&nbsp;38 for\_simple do |i|  
&nbsp;&nbsp;&nbsp;&nbsp;39 break unless i\*j/2 \< maxnum  
&nbsp;&nbsp;&nbsp;&nbsp;40 puts i  
&nbsp;&nbsp;&nbsp;&nbsp;41 end  
&nbsp;&nbsp;&nbsp;&nbsp;42  
&nbsp;&nbsp;&nbsp;&nbsp;43 puts \<\<TEXT  
&nbsp;&nbsp;&nbsp;&nbsp;44 Test II.  
&nbsp;&nbsp;&nbsp;&nbsp;45 here we initialize to 2 and increment by 2  
&nbsp;&nbsp;&nbsp;&nbsp;46 again, we don't need to track the counter...  
&nbsp;&nbsp;&nbsp;&nbsp;47 TEXT  
&nbsp;&nbsp;&nbsp;&nbsp;48  
&nbsp;&nbsp;&nbsp;&nbsp;49 for\_simple(2,2) do |i|  
&nbsp;&nbsp;&nbsp;&nbsp;50 break unless i\*j/2 \< maxnum  
&nbsp;&nbsp;&nbsp;&nbsp;51 puts i  
&nbsp;&nbsp;&nbsp;&nbsp;52 end  
&nbsp;&nbsp;&nbsp;&nbsp;53  
&nbsp;&nbsp;&nbsp;&nbsp;54 puts \<\<TEXT  
&nbsp;&nbsp;&nbsp;&nbsp;55 Test III.  
&nbsp;&nbsp;&nbsp;&nbsp;56 again, here we initialize to 200 and decrement by 20  
&nbsp;&nbsp;&nbsp;&nbsp;57 again, we don't need to track the counter...  
&nbsp;&nbsp;&nbsp;&nbsp;58 and note, we change keyword unless to if  
&nbsp;&nbsp;&nbsp;&nbsp;59 ruby just simply rocks..  
&nbsp;&nbsp;&nbsp;&nbsp;60 TEXT  
&nbsp;&nbsp;&nbsp;&nbsp;61  
&nbsp;&nbsp;&nbsp;&nbsp;62 for\_simple(200,-20) do |i|  
&nbsp;&nbsp;&nbsp;&nbsp;63 break if i\*j/2 \< maxnum  
&nbsp;&nbsp;&nbsp;&nbsp;64 puts i  
&nbsp;&nbsp;&nbsp;&nbsp;65 end

C:\family\ruby\>ruby test.rb  
using while...  
0  
1  
2  
3  
4  
5  
6  
7  
8  
9  
Test I.  
using our own simple for()... note advantage,  
we don't need to track the counter...  
0  
1  
2  
3  
4  
5  
6  
7  
8  
9  
Test II.  
here we initialize to 2 and increment by 2  
again, we don't need to track the counter...  
2  
4  
6  
8  
Test III.  
again, here we initialize to 200 and decrement by 20  
again, we don't need to track the counter...  
and note, we change keyword unless to if  
ruby just simply rocks..  
200  
180  
160  
140  
120  
100  
80  
60  
40  
20

C:\family\ruby\>

Hth.  
kind regards -botp

> **···**
>
> From: Michael W. Ryder [[mailto:\_mwryder@worldnet.att.net](mailto:_mwryder@worldnet.att.net)] :

---

<div class="post-metadata">

**Author:** ![Michael\_W\_Ryder1](https://avatars.discourse-cdn.com/v4/letter/m/8e7dd6/32.png) [@Michael\_W\_Ryder1](https://rubytalk.org/u/Michael_W_Ryder1)\
**Post date:** [20 March 2007 19:00 UTC](https://rubytalk.org/t/c-for-loop-equivalents/36277/8 "2007-03-20T19:00:11Z")

</div>

Peña wrote:

> From: Michael W. Ryder [mailto:\_mwryder@worldnet.att.net] :
> 
> # He would need to initialize i before your code as it could be # any value, # not just 0. By the same token the increment could be any number, not # just 1.
> 
> fwiw, this is a simple example (intended for nubies only).
> 
> C:\family\ruby\>cat -n test.rb  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1 #---------------------------  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2 #emulate c++ for (the better)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3 #for(int i=0; i\*j/2\<maxNum; i++)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;4 # {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;5 # //cool method stuff  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;6 # }  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;7 #---------------------------  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;8  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;9 # using normal while loop  
> &nbsp;&nbsp;&nbsp;&nbsp;10 puts "using while..."  
> &nbsp;&nbsp;&nbsp;&nbsp;11 j=20  
> &nbsp;&nbsp;&nbsp;&nbsp;12 i=0  
> &nbsp;&nbsp;&nbsp;&nbsp;13 maxnum=100  
> &nbsp;&nbsp;&nbsp;&nbsp;14 while i\*j/2 \< maxnum do  
> &nbsp;&nbsp;&nbsp;&nbsp;15 puts i  
> &nbsp;&nbsp;&nbsp;&nbsp;16 i+=1  
> &nbsp;&nbsp;&nbsp;&nbsp;17 end  
> &nbsp;&nbsp;&nbsp;&nbsp;18  
> &nbsp;&nbsp;&nbsp;&nbsp;19 # let's make our own for() and call it for\_simple 🙂  
> &nbsp;&nbsp;&nbsp;&nbsp;20 # note, there's no condition here  
> &nbsp;&nbsp;&nbsp;&nbsp;21 # we're leaving it up to yield/blocks since that  
> &nbsp;&nbsp;&nbsp;&nbsp;22 # is the ideal way, imho  
> &nbsp;&nbsp;&nbsp;&nbsp;23 def for\_simple (init=0,incr=1)  
> &nbsp;&nbsp;&nbsp;&nbsp;24 i = init  
> &nbsp;&nbsp;&nbsp;&nbsp;25 loop do  
> &nbsp;&nbsp;&nbsp;&nbsp;26 yield i  
> &nbsp;&nbsp;&nbsp;&nbsp;27 i += incr  
> &nbsp;&nbsp;&nbsp;&nbsp;28 end  
> &nbsp;&nbsp;&nbsp;&nbsp;29 end  
> &nbsp;&nbsp;&nbsp;&nbsp;30  
> &nbsp;&nbsp;&nbsp;&nbsp;31 # now let's test it  
> &nbsp;&nbsp;&nbsp;&nbsp;32 puts \<\<TEXT  
> &nbsp;&nbsp;&nbsp;&nbsp;33 Test I.  
> &nbsp;&nbsp;&nbsp;&nbsp;34 using our own simple for()... note advantage,  
> &nbsp;&nbsp;&nbsp;&nbsp;35 we don't need to track the counter...  
> &nbsp;&nbsp;&nbsp;&nbsp;36 TEXT  
> &nbsp;&nbsp;&nbsp;&nbsp;37  
> &nbsp;&nbsp;&nbsp;&nbsp;38 for\_simple do |i|  
> &nbsp;&nbsp;&nbsp;&nbsp;39 break unless i\*j/2 \< maxnum  
> &nbsp;&nbsp;&nbsp;&nbsp;40 puts i  
> &nbsp;&nbsp;&nbsp;&nbsp;41 end  
> &nbsp;&nbsp;&nbsp;&nbsp;42  
> &nbsp;&nbsp;&nbsp;&nbsp;43 puts \<\<TEXT  
> &nbsp;&nbsp;&nbsp;&nbsp;44 Test II.  
> &nbsp;&nbsp;&nbsp;&nbsp;45 here we initialize to 2 and increment by 2  
> &nbsp;&nbsp;&nbsp;&nbsp;46 again, we don't need to track the counter...  
> &nbsp;&nbsp;&nbsp;&nbsp;47 TEXT  
> &nbsp;&nbsp;&nbsp;&nbsp;48  
> &nbsp;&nbsp;&nbsp;&nbsp;49 for\_simple(2,2) do |i|  
> &nbsp;&nbsp;&nbsp;&nbsp;50 break unless i\*j/2 \< maxnum  
> &nbsp;&nbsp;&nbsp;&nbsp;51 puts i  
> &nbsp;&nbsp;&nbsp;&nbsp;52 end  
> &nbsp;&nbsp;&nbsp;&nbsp;53  
> &nbsp;&nbsp;&nbsp;&nbsp;54 puts \<\<TEXT  
> &nbsp;&nbsp;&nbsp;&nbsp;55 Test III.  
> &nbsp;&nbsp;&nbsp;&nbsp;56 again, here we initialize to 200 and decrement by 20  
> &nbsp;&nbsp;&nbsp;&nbsp;57 again, we don't need to track the counter...  
> &nbsp;&nbsp;&nbsp;&nbsp;58 and note, we change keyword unless to if  
> &nbsp;&nbsp;&nbsp;&nbsp;59 ruby just simply rocks..  
> &nbsp;&nbsp;&nbsp;&nbsp;60 TEXT  
> &nbsp;&nbsp;&nbsp;&nbsp;61  
> &nbsp;&nbsp;&nbsp;&nbsp;62 for\_simple(200,-20) do |i|  
> &nbsp;&nbsp;&nbsp;&nbsp;63 break if i\*j/2 \< maxnum  
> &nbsp;&nbsp;&nbsp;&nbsp;64 puts i  
> &nbsp;&nbsp;&nbsp;&nbsp;65 end
> 
> C:\family\ruby\>ruby test.rb  
> using while...  
> 0  
> 1  
> 2  
> 3  
> 4  
> 5  
> 6  
> 7  
> 8  
> 9  
> Test I.  
> using our own simple for()... note advantage,  
> we don't need to track the counter...  
> 0  
> 1  
> 2  
> 3  
> 4  
> 5  
> 6  
> 7  
> 8  
> 9  
> Test II.  
> here we initialize to 2 and increment by 2  
> again, we don't need to track the counter...  
> 2  
> 4  
> 6  
> 8  
> Test III.  
> again, here we initialize to 200 and decrement by 20  
> again, we don't need to track the counter...  
> and note, we change keyword unless to if  
> ruby just simply rocks..  
> 200  
> 180  
> 160  
> 140  
> 120  
> 100  
> 80  
> 60  
> 40  
> 20
> 
> C:\family\ruby\>
> 
> Hth.  
> kind regards -botp

Now is it possible, like in Scheme, to assign a variable to a string and have Ruby evaluate the string? For example, f = "i\*j/2". This would allow one to make a more generic version of your methods. Ideally you would have for\_opt (i=0, f, inc=1) and have the method evaluate each of the variables if necessary. This would allow one to copy working C code without having to remember to do a lot of extraneous coding.

---

<div class="post-metadata">

**Author:** ![Albert\_Ng](https://avatars.discourse-cdn.com/v4/letter/a/839c29/32.png) [@Albert\_Ng](https://rubytalk.org/u/Albert_Ng)\
**Post date:** [20 March 2007 20:45 UTC](https://rubytalk.org/t/c-for-loop-equivalents/36277/9 "2007-03-20T20:45:25Z")

</div>

Is there any reason you can't use the syntactical sugar "for"? Or maybe  
construct a range:

(i...((maxNum\*2)/j) ).each\_with\_index do |stuff| end

> **···**
>
> On 3/20/07, Michael W. Ryder \<\_mwryder@worldnet.att.net\> wrote:
> 
> > Peña wrote:  
> > \> From: Michael W. Ryder [mailto:\_mwryder@worldnet.att.net] :  
> > \>  
> > \> # He would need to initialize i before your code as it could be  
> > \> # any value,  
> > \> # not just 0. By the same token the increment could be any number, not  
> > \> # just 1.  
> > \>  
> > \> fwiw, this is a simple example (intended for nubies only).  
> > \>  
> > \> C:\family\ruby\>cat -n test.rb  
> > \> 1 #---------------------------  
> > \> 2 #emulate c++ for (the better)  
> > \> 3 #for(int i=0; i\*j/2\<maxNum; i++)  
> > \> 4 # {  
> > \> 5 # //cool method stuff  
> > \> 6 # }  
> > \> 7 #---------------------------  
> > \> 8  
> > \> 9 # using normal while loop  
> > \> 10 puts "using while..."  
> > \> 11 j=20  
> > \> 12 i=0  
> > \> 13 maxnum=100  
> > \> 14 while i\*j/2 \< maxnum do  
> > \> 15 puts i  
> > \> 16 i+=1  
> > \> 17 end  
> > \> 18  
> > \> 19 # let's make our own for() and call it for\_simple 🙂  
> > \> 20 # note, there's no condition here  
> > \> 21 # we're leaving it up to yield/blocks since that  
> > \> 22 # is the ideal way, imho  
> > \> 23 def for\_simple (init=0,incr=1)  
> > \> 24 i = init  
> > \> 25 loop do  
> > \> 26 yield i  
> > \> 27 i += incr  
> > \> 28 end  
> > \> 29 end  
> > \> 30  
> > \> 31 # now let's test it  
> > \> 32 puts \<\<TEXT  
> > \> 33 Test I.  
> > \> 34 using our own simple for()... note advantage,  
> > \> 35 we don't need to track the counter...  
> > \> 36 TEXT  
> > \> 37  
> > \> 38 for\_simple do |i|  
> > \> 39 break unless i\*j/2 \< maxnum  
> > \> 40 puts i  
> > \> 41 end  
> > \> 42  
> > \> 43 puts \<\<TEXT  
> > \> 44 Test II.  
> > \> 45 here we initialize to 2 and increment by 2  
> > \> 46 again, we don't need to track the counter...  
> > \> 47 TEXT  
> > \> 48  
> > \> 49 for\_simple(2,2) do |i|  
> > \> 50 break unless i\*j/2 \< maxnum  
> > \> 51 puts i  
> > \> 52 end  
> > \> 53  
> > \> 54 puts \<\<TEXT  
> > \> 55 Test III.  
> > \> 56 again, here we initialize to 200 and decrement by 20  
> > \> 57 again, we don't need to track the counter...  
> > \> 58 and note, we change keyword unless to if  
> > \> 59 ruby just simply rocks..  
> > \> 60 TEXT  
> > \> 61  
> > \> 62 for\_simple(200,-20) do |i|  
> > \> 63 break if i\*j/2 \< maxnum  
> > \> 64 puts i  
> > \> 65 end  
> > \>  
> > \> C:\family\ruby\>ruby test.rb  
> > \> using while...  
> > \> 0  
> > \> 1  
> > \> 2  
> > \> 3  
> > \> 4  
> > \> 5  
> > \> 6  
> > \> 7  
> > \> 8  
> > \> 9  
> > \> Test I.  
> > \> using our own simple for()... note advantage,  
> > \> we don't need to track the counter...  
> > \> 0  
> > \> 1  
> > \> 2  
> > \> 3  
> > \> 4  
> > \> 5  
> > \> 6  
> > \> 7  
> > \> 8  
> > \> 9  
> > \> Test II.  
> > \> here we initialize to 2 and increment by 2  
> > \> again, we don't need to track the counter...  
> > \> 2  
> > \> 4  
> > \> 6  
> > \> 8  
> > \> Test III.  
> > \> again, here we initialize to 200 and decrement by 20  
> > \> again, we don't need to track the counter...  
> > \> and note, we change keyword unless to if  
> > \> ruby just simply rocks..  
> > \> 200  
> > \> 180  
> > \> 160  
> > \> 140  
> > \> 120  
> > \> 100  
> > \> 80  
> > \> 60  
> > \> 40  
> > \> 20  
> > \>  
> > \> C:\family\ruby\>  
> > \>  
> > \> Hth.  
> > \> kind regards -botp  
> > \>
> > 
> > Now is it possible, like in Scheme, to assign a variable to a string and  
> > have Ruby evaluate the string? For example, f = "i\*j/2". This would  
> > allow one to make a more generic version of your methods. Ideally you  
> > would have for\_opt (i=0, f, inc=1) and have the method evaluate each of  
> > the variables if necessary. This would allow one to copy working C code  
> > without having to remember to do a lot of extraneous coding.
