473,495 Members | 2,058 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

foreach loop and multiple incrementers

74 New Member
with traditional for loops, it is possible to do something like this:

for (int i = 1, j = 100; i < 100; i = i+1, j = j-1) {
System.out.println(i + j);
}


is it possible to do the same using a foreach loop?

thanks
Apr 10 '09 #1
3 3363
dmjpro
2,476 Top Contributor
@rotaryfreak
You better to write i++ and j-- instead of assignment operations.
Apr 10 '09 #2
JosAH
11,448 Recognized Expert MVP
@rotaryfreak
Of course you can but you have to keep your counters out of the loop dedicate structure, e.g.

Expand|Select|Wrap|Line Numbers
  1. List<Monkey> monkeys= ...;
  2. int i= 0; j= 100;
  3. for (Monkey cheetah : monkeys) {
  4.    i++; // your counting stuff
  5.    j--;
  6.    // other stuff ...
  7. }
  8.  
kind regards,

Jos
Apr 10 '09 #3
rotaryfreak
74 New Member
Thank you Jos, your help is always greatly appreciated
Apr 12 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
1161
by: Robert Sentgerath | last post by:
Foreach is a relative handy construct to avoid having to create the classic "for(int loop = 0; loop < collection.Length; loop++) construct. Though when I use foreach instead I do not have access to...
104
7041
by: cody | last post by:
What about an enhancement of foreach loops which allows a syntax like that: foeach(int i in 1..10) { } // forward foeach(int i in 99..2) { } // backwards foeach(char c in 'a'..'z') { } // chars...
6
3202
by: Dave Spencer | last post by:
Hi all, New to this group and to C# in general (experienced in MFC). Anyway I have a ListView and a foreach loop that is doing something to each item in the list and updating the list on the...
13
14429
by: TrintCSD | last post by:
How can I reset the collections within a foreach to be read as a change from within the foreach loop then restart the foreach after collections has been changed? foreach(string invoice in...
0
1544
by: Benjamin | last post by:
I am attempting to create multiple itterators for a custom class that I have created. Basically I have a class that I am populating using XML de-serialization. I want to be able to loop through the...
7
6127
by: bonk | last post by:
Hello I am acessing a Dictionary<TKey,TValuefrom multiple threads and often in a foreach loop. While I am within one of the foreach loops the other threads must not modify the collection itself...
2
3343
by: Ryan Liu | last post by:
Hi, I heard foreach is slower then use loop directly, is that really true? When loop though a list in a multiple thread environment, is that a good practice to copy this list to an array the...
10
2899
by: fig000 | last post by:
HI, I'm new to generics. I've written a simple class to which I'm passing a generic list. I'm able to pass the list and even pass the type of the list so I can use it to traverse it. It's a...
15
2721
by: fjm | last post by:
Hello everyone and happy Sunday. :) I have a problem that I *think* I may know the solution to but have no idea how to write the code for it. I am working on a templating system wher I have...
0
7160
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7196
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7373
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5456
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4583
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3088
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1405
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
649
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.