-6

In a program to print hello:

for (1; 2; 3) 
printf ("Hello"); 

Why does the output show its an infinite loop? Isn't for ( ; ; ) alone an infinite loop?

Spikatrix
  • 20,225
  • 7
  • 37
  • 83
  • 12
    Because any number that is different than zero is considered `true` for boolean operations. So, the second operand in the for loop is then not the number `2` but an always `true` boolean statement. – Ivaylo Slavov Aug 26 '15 at 11:07
  • 2
    @user5268195 > No you won't or you won't stay long here. Maintaining SO's quality is every community member's business. – Laurent S. Aug 26 '15 at 11:15
  • 6
    While this might not be a good or interesting question, it is a valid question as far as SO policies are concerned. Being rude is however not ok. – Lundin Aug 26 '15 at 11:17
  • 4
    This is a bit unacceptable. I'm not sure what the argument is about here - this seems like a perfectly reasonable question to me? The answer might be obvious to those who are more experience with C / C++ programming, but that's no reason for the hostility towards the OP. – FreelanceConsultant Aug 26 '15 at 11:18
  • 7
    Also, why close vote for "too broad"? This question is about as narrow as they get. – Lundin Aug 26 '15 at 11:20
  • 2
    @Lundin Hi thanks, I'm new to this app and I'm still figuring out how it works. Bear with me – user5268195 Aug 26 '15 at 11:21
  • 2
    @user3728501 Thank you. I'm a beginner learning C programming and i dont have much guidance except this app – user5268195 Aug 26 '15 at 11:23
  • @user5268195 FYI, this is a Q/A site, not an app. – Spikatrix Aug 26 '15 at 11:36
  • @user5268195 In that case I'd suggest getting a book. [This list](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) has a good number of recommendations. – nemetroid Aug 26 '15 at 11:45
  • @nemetroid Sure, thanks a lot! – user5268195 Aug 26 '15 at 11:50

3 Answers3

4

Hmm you correct. In C for structure is

for ( initialization; condition; increment/decrement)

So, In your code, for (1; 2; 3) here in condition part (ie. middle) is a non-zero (ie. 2) and it,s always true. That's why your code always get true condition and infinite loop occur.

According to infinite Loop Definition in For Loop:

When the conditional expression is absent, it is assumed to be true. You may have an initialization and increment expression, but C programmers more commonly use the for( ; ; ) construct to signify an infinite loop.

For better Understanding, you can follow For Looop

Sakib Ahammed
  • 2,452
  • 2
  • 25
  • 29
3

In C, all non-zero integers are considered as true. So here,

for (1; 2; 3) 

2 is a non-zero integer and so, it is an infinite loop. And yes,

for ( ; ; )

is also an infinite loop.

Spikatrix
  • 20,225
  • 7
  • 37
  • 83
1

for will halt once the second statement evaluates to 0. (An empty statement counts as non-zero in this context).

Yours never does. for(1; 2; 3), for(; 2;), and for(;;) will not halt.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483