-8
# include <stdio.h>

int main()
{
  int a=5;
  begin:
  if(a)
       printf("%d\n",a);
   a--;
   goto begin;
   return 0;
 }

When a becomes 0 then if condition will not execute then why the output is going to be infinty in this code means

output -

 5
 4
 3
 2
 1
 0
-1
-2
and so on endless
Taryn
  • 242,637
  • 56
  • 362
  • 405
Aman Singh
  • 743
  • 1
  • 10
  • 20

3 Answers3

3

If the program really does print 0 for you then there might be a serious problem with your compiler (or even your machine...). My suspicion is that it doesn't print 0, but your question is really why the program loops infinitely.

This is because the if-body only contains the print statement. So when a reaches 0 it isn't printed but the lines

a--;
goto begin;

are still executed. The machine will obey, go back to begin and the loop continues. The quickest fix is to put braces around all the statements you want executed when a != 0:

if(a){
    printf("%d\n",a);
    a--;
    goto begin;
}

return 0;

This will make the program only loop until a is 0, after which it returns.

But the real problem is: don't use goto (for this)! This is a perfect situation to use a while loop:

while(a--){
    printf("%d\n", a);
}

return 0;

(The braces around the while-body are not even strictly necessary here, but just good practice)

Kninnug
  • 7,992
  • 1
  • 30
  • 42
1

Its because after the if the goto statement is again executed and then the value of a has already become other than 0 . Now, again you get the goto statement and therefore if goes on executing and printing negative values.

Look at it this way :-

The statement

printf("%d\n",a);

is executed only when condition in if is true. TRUE here refers to anything not equal to 0 so, the printf is not executed when a is 0, while it executes for any other value. Now, a-- and goto are executed outside if so they are executed again and again making the condition in if true always and negative values are printed infinitely.

Nevertheless,

My question is , why are you using goto?

0decimal0
  • 3,884
  • 2
  • 24
  • 39
  • just checking its working, that's it, actually i am learning C and thank you all for the help – Aman Singh Jul 13 '13 at 19:25
  • 3
    @AmanSingh Keep in mind that unless you have a very good reason for that, you should not use goto. (And you don't probably have a very good reason for that if you just started learning C.) –  Jul 13 '13 at 19:26
  • @AmanSingh its very old style almost obsolete. Anyway:) – 0decimal0 Jul 13 '13 at 19:27
  • Got it guys, and H2CO3 if we have to send the execution back to any part of the program what should i use ? – Aman Singh Jul 13 '13 at 19:29
  • @AmanSingh Loops. While, for, do-while... –  Jul 13 '13 at 19:30
  • 2
    Oh come on!! I haven't said it prints 0 why the downvote on my answer. Please explain yourselves. – 0decimal0 Jul 13 '13 at 19:32
  • 1
    Seriously, the downvote was lightning fast without even looking at my answer properly. Can't find out where and when I wrote it prints 0. Just escaped a stroke LOL :D . And despite of writing `0` in output OP clearly states that `if` will not execute . – 0decimal0 Jul 13 '13 at 19:59
0
if a==1 -> Evaluates to TRUE
if a==0 -> Evaluates to FALSE
if a==-1 -> Evaluates to TRUE

etc.

Therefore it will display numbers in descending order, except 0 which will not display.

Winks
  • 580
  • 1
  • 7
  • 17