# What is equivalent for loop?

• 01-14-2012, 08:00 PM
diamonddragon
What is equivalent for loop?
int i = 0;
int sum = 0;

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

Explain output?
• 01-14-2012, 08:24 PM
Fubarable
Re: What is equivalent for loop?
You first. :)
• 01-14-2012, 09:25 PM
diamonddragon
Re: What is equivalent for loop?
That was my first, don't You see? :)
• 01-14-2012, 10:22 PM
Fubarable
Re: What is equivalent for loop?
Quote:

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.
• 01-14-2012, 10:46 PM
diamonddragon
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;
}
• 01-15-2012, 10:16 AM
JosAH
Re: What is equivalent for loop?
A while loop:

Code:

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

Code:

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

kind regards,

Jos