# Increment outerloop value in innerloop in each do Syntax

**URL:** <https://www.ruby-forum.com/t/increment-outerloop-value-in-innerloop-in-each-do-syntax/179752>\
**Category:** Ruby\
**Created:** [December 3, 2009, 1:58am UTC](https://www.ruby-forum.com/t/increment-outerloop-value-in-innerloop-in-each-do-syntax/179752 "2009-12-03T01:58:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![nagaraju](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f07891/32.png) [@nagaraju](https://www.ruby-forum.com/u/nagaraju)\
**Post date:** [December 3, 2009, 1:58am UTC](https://www.ruby-forum.com/t/increment-outerloop-value-in-innerloop-in-each-do-syntax/179752/1 "2009-12-03T01:58:44Z")

</div>

Hi All,

I am new to RUBY. Please help me in solving the below problem.  
I have a c code and I am trying to do the same in RUBY.

for(int i = 0; i \< 10;i++)  
{  
for(int j = 0; j\< 5; j++)  
{  
if(i == j)  
{  
printf("%d - its J value\n",j);  
i++;  
}  
}  
printf("%d - its I value\n",i);  
}

Above is my C code. The output of above code is:

0 - its J value  
1 - its J value  
2 - its J value  
3 - its J value  
4 - its J value  
5 - its I value  
6 - its I value  
7 - its I value  
8 - its I value  
9 - its I value

I want to do the same in RUBY. I have written following script

(0…9).each do |i|  
(0…5).each do |j|  
if( i == j)  
puts “#{j} J Value”  
end  
end  
puts “#{i} I Value”  
end

But its not giving the desired output. Please suggest me how to achieve  
the above output using RUBY script.

Thanks in advance.

Regards,  
Nag.

---

<div class="post-metadata">

**Author:** ![nagaraju](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f07891/32.png) [@nagaraju](https://www.ruby-forum.com/u/nagaraju)\
**Post date:** [December 3, 2009, 2:20am UTC](https://www.ruby-forum.com/t/increment-outerloop-value-in-innerloop-in-each-do-syntax/179752/2 "2009-12-03T02:20:39Z")

</div>

Nag R. wrote:

> ```
> {
> 
> ```
> 
> 1 - its J value
> 
> the above output using RUBY script.

I don’t think you can use each on the i value. I would use a while loop  
to check for the value of i.

---

<div class="post-metadata">

**Author:** ![nagaraju](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f07891/32.png) [@nagaraju](https://www.ruby-forum.com/u/nagaraju)\
**Post date:** [December 3, 2009, 2:29am UTC](https://www.ruby-forum.com/t/increment-outerloop-value-in-innerloop-in-each-do-syntax/179752/3 "2009-12-03T02:29:16Z")

</div>

Michael W. Ryder wrote:

> Nag R. wrote:
> 
> > ```
> > {
> > 
> > ```
> > 
> > 1 - its J value
> > 
> > the above output using RUBY script.
> 
> I don’t think you can use each on the i value. I would use a while loop  
> to check for the value of i.

Hi Michael,

I have to get it done with each do syntax. Because I have a structure  
type tag in xml file and I need to move onto next element if my  
comparision goes fine.

Please help me in this. Thanks in advance.

Regards,  
Nag.

---

<div class="post-metadata">

**Author:** ![nagaraju](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f07891/32.png) [@nagaraju](https://www.ruby-forum.com/u/nagaraju)\
**Post date:** [December 3, 2009, 2:55am UTC](https://www.ruby-forum.com/t/increment-outerloop-value-in-innerloop-in-each-do-syntax/179752/4 "2009-12-03T02:55:46Z")

</div>

Nag R. wrote:

> Hi Michael,
> 
> I have to get it done with each do syntax. Because I have a structure  
> type tag in xml file and I need to move onto next element if my  
> comparision goes fine.

The following outputs the same results as your C++ code:

i = 0  
while (i \< 10)  
(0…5).each do |j|  
if (i == j)  
puts “#{j} its J Value”  
i += 1  
end  
end  
puts “#{i} its I Value”  
i += 1  
end

0 its J Value  
1 its J Value  
2 its J Value  
3 its J Value  
4 its J Value  
5 its J Value  
6 its I Value  
7 its I Value  
8 its I Value  
9 its I Value

If this isn’t what you are looking for I don’t understand what you are  
trying to do.

---

<div class="post-metadata">

**Author:** ![nagaraju](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f07891/32.png) [@nagaraju](https://www.ruby-forum.com/u/nagaraju)\
**Post date:** [December 3, 2009, 3:17am UTC](https://www.ruby-forum.com/t/increment-outerloop-value-in-innerloop-in-each-do-syntax/179752/5 "2009-12-03T03:17:26Z")

</div>

Good Day Nag,

On Wed, Dec 2, 2009 at 5:29 PM, Nag R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I have to get it done with each do syntax. Because I have a structure  
> type tag in xml file and I need to move onto next element if my  
> comparision goes fine.

If you would like assistance please don’t try to outsmart us. If you  
have a  
specific issue (and apparantly this double loop is not your issue)  
please  
just discuss the issue and not try to make it pretty or such.

Ruby isn’t like C in many respects to looping and you probably are not  
approaching your actual problem in a very “Rubyesque” method by doing it  
a C  
style loop.

Specifcally unless you need the element index for some reason - you will  
find many Ruby idioms never use an index based loop but rather an  
“object”  
based loop instead iterating over the actual objects you are intersted  
in.

Again, absent what you are specifically trying to accomplish - it’s  
difficult to assist.

John

---

<div class="post-metadata">

**Author:** ![nagaraju](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f07891/32.png) [@nagaraju](https://www.ruby-forum.com/u/nagaraju)\
**Post date:** [December 3, 2009, 3:22am UTC](https://www.ruby-forum.com/t/increment-outerloop-value-in-innerloop-in-each-do-syntax/179752/6 "2009-12-03T03:22:08Z")

</div>

On Dec 2, 2009, at 9:28 PM, Nag R. wrote:

> ```
> {
> printf("%d - its J value\n",j);
> i++;
> }
> }
> printf("%d - its I value\n",i);
> 
> ```
> 
> }

Wow, is there any use case for this pattern?

> end  
> Be aware that i or j are in no way the same as i and j in  
> your C program. Here, you iterate over the set of natural  
> numbers from 0…9 and 0…5, binding i and j to those values  
> for every step of the iteration. So i and j are not controlling  
> your iteration - each does.  
> So any change to i and j will be lost at the end of the do-block.

But, with a bit of trickery, you can achieve this:

(0…9).each do |i|  
if j = (0…5).find{ |j| i==j }  
puts “#{j} J Value”  
else  
puts “#{i} I Value”  
end  
end

#find finds the first element of the range that satisfies  
the condition given in the block. If there is none, it returns  
nil, which is not true in a boolean sense. As the statement in  
the condition can be any ruby expression (because the always return  
a value, there is no void), we can immediately assign that to j.

As j has a true value (everything but nil and false) if the find  
condition matches, the first branch of the if is used. Otherwise,  
j is nil and i is printed.

Again: you iterate over a set, this is no counting loop. This might  
take a bit to get used to, but in the end, you can use such nice  
things as #find, #map, #inject on it. Learn them, they are a rubyists  
mightiest tools.

Have a lot of fun.

Regards,  
Florian G.

---

<div class="post-metadata">

**Author:** ![nagaraju](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f07891/32.png) [@nagaraju](https://www.ruby-forum.com/u/nagaraju)\
**Post date:** [December 3, 2009, 4:47pm UTC](https://www.ruby-forum.com/t/increment-outerloop-value-in-innerloop-in-each-do-syntax/179752/7 "2009-12-03T16:47:27Z")

</div>

Florian G. wrote:

> On Dec 2, 2009, at 9:28 PM, Nag R. wrote:
> 
> > ```
> > {
> > printf("%d - its J value\n",j);
> > i++;
> > }
> > }
> > printf("%d - its I value\n",i);
> > 
> > ```
> > 
> > }
> 
> Wow, is there any use case for this pattern?
> 
> > end  
> > Be aware that i or j are in no way the same as i and j in  
> > your C program. Here, you iterate over the set of natural  
> > numbers from 0…9 and 0…5, binding i and j to those values  
> > for every step of the iteration. So i and j are not controlling  
> > your iteration - each does.  
> > So any change to i and j will be lost at the end of the do-block.
> 
> But, with a bit of trickery, you can achieve this:
> 
> (0…9).each do |i|  
> if j = (0…5).find{ |j| i==j }  
> puts “#{j} J Value”  
> else  
> puts “#{i} I Value”  
> end  
> end
> 
> #find finds the first element of the range that satisfies  
> the condition given in the block. If there is none, it returns  
> nil, which is not true in a boolean sense. As the statement in  
> the condition can be any ruby expression (because the always return  
> a value, there is no void), we can immediately assign that to j.
> 
> As j has a true value (everything but nil and false) if the find  
> condition matches, the first branch of the if is used. Otherwise,  
> j is nil and i is printed.
> 
> Again: you iterate over a set, this is no counting loop. This might  
> take a bit to get used to, but in the end, you can use such nice  
> things as #find, #map, #inject on it. Learn them, they are a rubyists  
> mightiest tools.
> 
> Have a lot of fun.
> 
> Regards,  
> Florian G.

Hi All,

Thank you very much Michael and Florian for your help. I am sorry John  
if I made something wrong. I tried with continue, break and next.  
Nothing worked out for me. Unluckily I have to insert some code  
according to this pattern. Because remaining file maintained this format  
so I have to. At last after some manipulation, I got the below code  
which works similar to the above codes given by Michael and Florian.  
Once again thank you very much for the help.

myval = true  
(0…9).each do |i|  
(0…5).each do |j|  
if(i==j)  
puts “#{j} its J value”  
myval = false  
end  
end  
if(myval)  
puts “#{i} its I value”  
end  
myval = true  
end

Regards,  
Nag
