0

First of all sorry for this, I really think it is a silly question but I've been stuck on this for a while. So maybe you can help me. The problem is I don't truly understand what's wrong in the code. So let's look up.

void enter()
{
    int init= 1, end= 2;
    float jump= 0.2;
    create(init, end, jump);
}

void create(int Init, int End, float Jump)
{
    float i;
    int total = 0;
    for(i = Init; i < End; i + Jump)
        total += 1;
}

It does not exit the loop and I don't understand why.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
blackout
  • 84
  • 9
  • Never, ever, ever, use floats as loop controls. Bad programmer! No biscuit! Also, your `i + jump` should be `i += jump`. – Lee Daniel Crocker Dec 16 '14 at 18:36
  • 1
    There's nothing per se wrong with using floating point variables as loop controls, for instance: `for (i=1.0; i=i/2.0; i>epsilon) { ... }`. What's wrong is using floats for things that integers are better used for, particularly comparison for equality with potentially large integers. – abligh Dec 16 '14 at 19:36

3 Answers3

4

The problem is here:

i + Jump

That does not change i, it simply evaluates. Change it to this:

i += Jump

and it should work fine.

Alexis King
  • 43,109
  • 15
  • 131
  • 205
2

The loop is infinit because variable i is not being changed within the loop. Change this statement

for(i = Init; i < End; i + Jump)

to

for(i = Init; i < End; i += Jump)

Also maybe there is a sense to define the function as having return type int is not there? For example

int create(int Init, int End, float Jump)
{
    //...
    return total;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

i + Jump this does not modify i and hence i < End is always evaluated as TRUE, as per the initial condition (Init being less than End).

You need to change to i += End to keep incrementing the value of i.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261