1

this is what i did but i'am not sure if this is right

int e = 1;
int m = 500;
for ( e = 1; m = 500; e < 4; m >= 300; e++; m-100;)
1
  • If you already have e and m declared you don't need to re-declare them in your loop. Also does the value of m actually change with your loop? You should have to use -= since m - 100 doesn't actually change the value of m unless it's part of m = m - 100; Commented Apr 15, 2015 at 0:59

2 Answers 2

4

What you are trying to do is possible, but you have a few pieces wrong.

You can execute multiple statements in each "area" of the for-loop structure, but you need to separate them differently. A valid loop would look like this:

for ( e = 1, m = 500; e < 4 && m >= 300; e++, m -= 100)
{
}

Notice that in the first and third blocks, you use commas to separate the initialization and increment/decrement statements. The second block has to be a single conditional, so I used && to AND them together.

I also fixed your "m" decrement statement so that it actually modified m. The subtraction operator is non-destructive, so wouldn't actually modify anything n your original code.

Sign up to request clarification or add additional context in comments.

Comments

0

The first part of the for is the initialization section. (The part before the first ;. So to initialize a, b, c, you can do this:

char a;
int b;
string c;

for (a = 'z', b = 28, c = "aardvark"; etc.)

Currently you are trying to initialize m after the first ;, which won't work.

1 Comment

@BradleyDotNet, you are right. I just focused on the initialization part of the question. You're answer also helps with the other parts of the for loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.