# Thread: What is equivalent for loop?

1. Senior Member
Join Date
Jan 2012
Posts
210
Rep Power
6

## What is equivalent for loop?

int i = 0;
int sum = 0;

while (i < 4) {
if (i++ % 3 == 0)
continue;
sum += i;
}

Explain output?
Last edited by diamonddragon; 01-14-2012 at 08:09 PM.

2. Senior Member
Join Date
Jan 2012
Posts
210
Rep Power
6

## Re: What is equivalent for loop?

That was my first, don't You see? :)

3. ## Re: What is equivalent for loop?

Originally Posted by diamonddragon
That was my first, don't You see? :)
You won't learn if you don't put in effort, and I'm sure that you wouldn't have posted this if you didn't try to solve it first. So please show us the fruits of your efforts. It will also give us a better idea of what you do and don't understand. Also we frown on folks who just dump assignments in here without first showing their work, so help us all out by changing this.

4. Senior Member
Join Date
Jan 2012
Posts
210
Rep Power
6

## Re: What is equivalent for loop?

Here is one solution, and wonder if there is better one?

int i, sum;

for (i = 0, sum = 0; i < 4;) {
if (i++ % 3 != 0)
sum += i;
}

5. ## Re: What is equivalent for loop?

A while loop:

Java Code:
```wile (<condition>)
<statement>;```
can always be transformed to a for loop as follows:

Java Code:
```for(; <condition>; )
<statement>;```
But I don't think you had this in mind ...

kind regards,

Jos

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•