-5

I have been looking at this code for 20min and I can't understand why this loop is infinite, it should print 30 elements.

Some text, because post is mostly code.

user3529236
  • 107
  • 2
  • 9

3 Answers3

1

This is a common mistake when programming in C(++), early on, people mistake the equality operator == with the assignment operator =.

What you're doing here with the line if (i = 1){ is setting i to 1 on each iteration of the loop, so i never gets any larger.
To fix it, simply change that line to if (i == 1) {

porglezomp
  • 1,333
  • 12
  • 24
0

I'm guessing it's because if (i = 1) assigns 1toi. You meant to compareif (i == 1)

jpw
  • 44,361
  • 6
  • 66
  • 86
0

the if statement is wrong. it should be

if(i == 1)

currently on each loop you are assigning i back to 1. Hence the reason why the loop is infinite

JayDev
  • 1,340
  • 2
  • 13
  • 21