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
Related
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 7 months ago.
Improve this question
C newbie here. Was wondering why doesn't this code throw and error. It has missing parentheses and comparison between characters.
#include <stdio.h>
int main(){
while ('a'<'b')
printf("-");
return 0;
}
'a' and 'b' are character integer constants (6.4.4.4) and comparable like any other pair of integers. That comparison is probably evaluated at compile-time leaving you with an infinite loop:
while(1) print("-");
The answer is simple, if you are not using the curly brackets to create a block of code you if's while's etc... will execute the first line of code after the condition\loop etc...
while( condition )
func1();
But this is bad practice, you can fool yourself thinking that the example below will execute in a loop the 3 lines of code, in reality it will execute inside the while just the first one:
while( condition )
func1(); //<- just this line will be executed inside the while loop.
func2();
func3();
Correct way:
while( condition )
{
func1();
func2();
func3();
}
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.
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 7 years ago.
Improve this question
int main(void) {
long fall, n, k, p, i, j, r;
long long x, y, a[110][110];
for(a[0][0]=scanf("%ld",&fall);fall--;printf("%lld\n",y)) {
for(i=!!scanf("%ld%ld%ld",&n,&k,&p);i<=k+1;i++)
for(j=0;++j<=i;a[i][j]=(a[i-1][j-1]+j*a[i-1][j])%p)
;
for(y=!(j=1);j<=k+1;y=(y+a[k+1][j++]*x)%p)
for(x=!((r=n%j)*!(i=-1));++i<j;x=x*(n-i)/((i==r)?j:1)%p)
;
}
return 0;
}
How does for loop work here? It doesn't follow the syntax as I see.
for loops have the following pattern:
for(initial expression; conditional expr; afterthought)
I'll break down the first loop for you, you should be able to do the rest on your own.
for(a[0][0]=scanf("%ld",&fall);fall--;printf("%lld\n",y))
The initialization part of this loop is a[0][0]=scanf("%ld",&fall).
scanf is used for reading input and returns the number of input values. In this case, it will be 1 and it gets assigned to a[0][0].
fall-- is the conditional expression. In C, positive numbers are evaluated as true. So this loop will run until fall == 0.
printf("%lld\n",y) is the afterthought. It gets run after each loop iteration. In this case, it will simply print the value.
Unraveling obfuscated code can be a good learning exercise though you must obviously never use it in practice.
This code abuses the fact that the first and third conditions of the for loop does not necessarily need to have anything to do with the loop itself. At its core, the for loop simply executes an initial expression, performs the conditional check and executes the afterthought after every iteration.
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.
This question already has answers here:
Does `break` work only for `for`, `while`, `do-while`, `switch' and for `if` statements?
(6 answers)
Closed 9 years ago.
If i write break statement in for loop then will variable be updated and then for loop exits,or after just excecution of break statement for loop exits?
for e.g
for(i=0;i<100;i++){
//do something something
if(i==50){
break;
}
what will be the value of i after for loop exit?
When the break statement is encountered inside a loop, the loop is immediately terminated and program control resumes at the next statement following the loop.It can be used to terminate a case in the switch statement
The value will be 50.
The for loop can be described in general terms like this:
for(INIT; CONDITION; UPDATE)
BODY
and it can be replaced with the equivalent while loop, like this:
INIT
while(CONDITION)
{
BODY
UPDATE
}
So, since your break is in the BODY, the UPDATE is not run, and the value 50 remains.
If loop ready to exit by the break statement , that mean the i value should be equal to the condition .
if(i==50);
i Will be 50
...