1

Assumed a given code:

int n;

while (n<100){ n--;}

n isn't initialized. Therefore, what kind of error is it when you use an un-initialized variable?

As well, there is a chance that the loop would be infinite? In that case, is it a logical error or a run-time error?

(Generally, if accidentally there is an infinite loop in a certain code, is It a logical error or a rum-time error?).

Thanks a lot!

Lori
  • 1,392
  • 1
  • 21
  • 29
Sapsap
  • 21
  • 1
  • 4
  • `n` has whatever happens to be in that memory location. Maybe the compiler optimizes away your loop. If it doesn't, the loop isn't infinite: if `n` happens to be positive, it will get under 100 and the loop stops. If `n` happens to be negative, it will reach the lowest possible value for int on your system, then overflow and become positive, and eventually get below 100. – Robert Feb 04 '19 at 00:58
  • [Undefined behaviour](https://stackoverflow.com/a/4105123/1505939) – M.M Feb 04 '19 at 02:51

2 Answers2

1

and generally, if there is an infinite loop, is it a run-time error or a logical error? as well, if i use an un-initialized variable. is it a run time error or a compilation error?

Sapsap
  • 21
  • 1
  • 4
-1

you do not get an error, you app simply decrements the counter for a while, and will move on when it overflows

zambari
  • 4,797
  • 1
  • 12
  • 22