using goto keyword in C [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
# 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

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)

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?

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.

Related

about the break statement [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I'm kind of confused about the break statement
If I have for example a for loop inside a while loop and there is a break inside the for loop:
the break statement will break out of the while loop right?
No, the break statement will break out of the for loop. The break statement is used to exit the loop it is written in
The break statement in C programming has the following two usages:
When a break statement is encountered inside a loop, the loop is immediately terminated and the program control resumes at the next statement following the loop.
It can be used to terminate a case in the switch statement
If you are using nested loops, the break statement will stop the execution of the innermost loop and start executing the next line of code after the block.
Have a look here
The keyword break will exit the loop it's written in for example:
int main(void) {
int i=0;
int for_called_count=0;
while(i<10){
for_called_count++;
for(int j=1; j<=10; j++){
i++;
if(j==5){
break;
}
}
}
printf("i: %i", i);
printf("\nfor_called_count: %i", for_called_count);
}
Output:
i: 10
for_called_count: 2
Here you see, for is called twice because once the for loop exits with break, the while loop is not done as i is still smaller than 10 so it calls for again. That shows that break only exits the loop it's written in.
The break will only exit the for loop, and will proceed again in while loop if the condition in while loop is still not met

How does the putchar and whole of the code gets executed? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I want the dry run of the code for the first 3 iterations to understand.
THe output of the code is : abcdbcdbcdbcdbc........(infinte times)
I know how for loop works and put char also.I did not dry run as i was not understand will the third argument in the for loop will increment or not.
#include <stdio.h>
int main()
{
for (putchar('a');putchar('b');putchar('d'))
putchar('c');
return 0;
}
For your example:
Initial statement : putchar('a')
Condition expression: putchar('b')
Repeat step: putchar('d')
Loop statement : putchar('c')
Now map you code with flow chart above.
Since putchar returns the character it has printed which is b also satisfies the true condition, thus your for loops run infinite time.
Attribution :
http://www.equestionanswers.com/c/for-while-do-while-loop-syntax.php
putchar always returns the char that you put. So for instance, putchar('a') returns 'a'. With that in mind, let's have a look at how the for loop works:
for ( init_clause ; cond_expression ; iteration_expression )
loop_statement
The init_clause is putchar('a'). This prints a once, because the init_clause is evaluated once at the beginning of the for loop.
The cond_expression is putchar('b'). This is checked every run through the loop, which is why it always prints b. And as it returns 'b' every time, the loop never stops. The loop would only stop if the cond_expression returned 0 or the loop is exited otherwise, for instance through break.
The iteration_expression is putchar('d'), hence d is printed every time. The loop_statement is putchar('c'), printing c.
The result is printing a once, followed by an infinite amount of bcd. The reason why you get them in this order is because in every run throught the loop, it first checks the cond_expression, executes the loop_statement and then the iteration_expression.

In C, what happens when the condition for a for loop aren't met at the beginning? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
For example, what happens if I say:
for(i = 2; i < 2; i++)
Obviously, this is a useless for loop, but maybe i = a, and a is set by something else. So what happens in this case?
Neither iteration of the loop will be executed.
In fact this loop (provided that the condition has no side effects)
for(i = 2; i < 2; i++) { /* ... */ }
is equivalent to this statement
i = 2;
The condition of a for loop is checked before every iteration, including the first one; so your loop's body will never be executed.
The way for loop works is it checks for condition (in your case i < 2) and executes whatever is between { } or whatever code on following lines
As you are initializing i to 2 the condition fails immediately and nothing executes.
Essentially whatever code that is inside of for loop never executes.
In a for loop, the condition is evaluated before the first iteration. This means that in your example, the content of the loop would not be executed because i is already greater than or equal to 2.
Example code path:
Set i = 2.
Check if i < 2.
Exit loop because step 2 evaluated to false.
i would still be modified, however, as the variable initialization (i.e. i = 2) still occurs before the condition is checked.

Understanding Simple For Loop Code in C [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I'm beginner in C programming (just started) and i need help from you to understand the output of this very simple code:
int main()
{
int x=1;
for (;x<=10;x++);
printf("%d\n",x);
return 0;
}
output is:
11
the same output if x value is <=11
and if x value is 12 or more, it prints the exact value of x (ex: if int x=12; the output is 12).
how did the computer understand this code?
So,
int main()
{
int x=1; // line 1
for (;x<=10;x++); // line 2
printf("%d\n",x); // line 3
return 0; // line 4
}
Line 1 initializes x to 1.
Line 2 keeps increasing x by 1 until it reaches 11. The first semicolon indicates "don't do anything before starting the loop", x<=10 indicates keep going until x > 10 (so when x = 11) and x++ means increase x by 1 each time. If x >= 11, this line gets basically skipped because x is already greater than 10.
Line 3 prints out x to the command line (in this case, x = 11 if x started out less than 11 or just x if x started at >= 11 due to the previous line)
Line 4 means the program was successful, exit the program.
for is this:
for(*init-expr*; *test-expr*; *update-expr*)
*body-statement*
Or rather, commonly, it can be decribed like this:
*init-expr*;
while(*test-expr*){
*body-statement*
*update-expr*;
}
and, your for statement is followed by a semicolon, where body-statement is.So, it is a "null statement", just loop and update x, when finishes the loop, just print the x after loop, so, the output is 11.

Describing the following code [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I've got a question regarding an assignment i've been given.
-There are two integeras, a and b, which has the value of -1 and 1 respectively.
What's the value of a and b after running the following code and WHY.
if(!++a)
b+=a++;
When i run this code i'm getting the values 1 and 1. I can not really figure out WHY though... Im getting quite confused over the if statement, could anyone explain this for a beginner trying to learn C?
Your code is equivalent to this one:
int a = -1;
int b = 1;
a += 1;
if (a == 0) {
b += a;
a += 1;
}
You should see why both variables end up as 1 here. Now try to figure out why these codes are equivalent.
if (!++a)
First of all, if (a) is identical to if (a != 0). Weird, but true. Zero means false, any other number means true.
Saying if (!a) inverts that meaning.
++a increments a and returns the new value. (Unlike a++, which increments a but returns the old value, not the new one.)
Putting all that together, this says "increment a and test whether the answer is zero". If a = -1, then this is indeed true.
Usually people would write if (...) {do stuff}, but if the "do stuff" part is only one statement, you can leave out the brackets. We've already established that the condition is true in this case, so the "b+=a++" line is run.
If we put some spaces on that, we have
b += (a++);
So, increment a again, but before that, add it's (old) value to b.
++a gives you the value after increment, so you get 0 which means false. The ! operator makes it true.
Then you basically have
b = b + a;
a++;
So b remains 1 and a gets another increment and ends up at 1 too.
The main concept here is the difference between a++ and ++a. If you use a++ you will first get the value of a and then the value is incremented whereas for ++a the value is first incremented and then returned.
Initially a= -1 when your code will enter if (!++a) and it is pre increment first increment of a will happen so a will become 0 and !0 is 1 and it will enter in the if block.
now b+=a++;
here a++ is post increment so you can devide this statement in two parts.
first b+=a; b+=0 so b will remain 1.
second a++; a will become 1.

Resources