function not compiling

It's hard to find a discussion post with super basic errors like this.
Can someone tell me what I'm missing?

I wrote another function above this function that also uses a loop (coded the same exact way) and it compiled fine. Error shown is below.

1
2
3
4
5
6
7
8
9
10
  int SumNumTerms(int numTerms) {
  // Output is sum of the specified number of terms
  int sum = 0;
  int i = 0;
  for (i <= numTerms) {
    sum = sum + 1;
    i = i + 1;
  }
  return sum;
}


Error Message:
expected ';' before ')' token

Got it...
I was writing the code like a while loop...

Edited:

int SumNumTerms(int numTerms) {
// Output is sum of the specified number of terms

int sum = 0;
for (i = 0; i <= numTerms; i = i + 1) {
sum = sum + 1;
}
return sum;
}
Hello Homedogslice,

Actually all you need is or (i = 0; i <= numTerms; i++). This does the same as i = i + 1.

In your first bit of code for (i <= numTerms) it should have been written as for (; i <= numTerms;). Adding the ";"s would have made it a proper for loop.

Andy
Topic archived. No new replies allowed.