How does break statement work? [duplicate] - c

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
...

Related

What is the use of do-while [duplicate]

This question already has answers here:
Is there ever a need for a "do {...} while ( )" loop?
(19 answers)
Closed 1 year ago.
For and while loop can be used anywhere in replace of do-while then why the C language has do-while loop, what is it's real use .
#include <stdio.h>
int main()
{
do{
statements;
}
while(condition);
}
return 0;
The main use of using a do while loop
is that even if the condition that you pass is false it will execute the loop at least once.
While Loop: First condition then execute the code
Do While Loop: First execute the code then condition

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.

What for (;;) and while(); mean in C [duplicate]

This question already has answers here:
What does "for(;;)" mean?
(5 answers)
Closed 4 years ago.
I am looking at some example codes and I saw someone did this
for (;;) {
// ...
}
Is that equivalent to while(1) { } ?
And what does while(condition); do? I don't get the reason behind putting ';' instead of {}
yes,
for(;;){}
is an infinite loop
And what does while(condition); do? I don't get the reason behind putting ';' instead of {}
Well, your question is what happens if you put or you do not put a semicolon after that while condition? The computer identifies the semicolon as an empty statement.
Try this:
#include<stdio.h>
int main(void){
int a = 5, b = 10;
if (a < b){
printf("True");
}
while (a < b); /* infinite loop */
printf("This print will never execute\n");
return 0;
}
for(;;) and while(1) are both infinite loops, and compile to the same opcodes:
L2:
jmp L2
Which means there is no speed difference, as the disassembly is exactly the same.
while just loops though a single statement until the condition is false. It doesn't have to be a compound statement (this thing: {}), it can be any statement. ; is a statement that does nothing.
while(getchar() != '\n');
will loop until you hit enter, for example. Though, this is bad practice since it will hog the thread; adding a call to a sleep method in the loop is better.

Why is the print statement encountered five times in the following program? [duplicate]

This question already has answers here:
Values obtained in case of a recursive function
(3 answers)
Closed 7 years ago.
According to the logic of recursion, the printf() at the end must be encountered only once. The main() after if() should send the control back to the beginning. Only after var becomes zero should we reach the printf(). Why do we encounter the printf() five times? I know that this has been asked before but this logic is not explained in there.
void main()
{
static int var=5;
if(--var){
main();
}
printf("%d\n", var);
}
The printf statement is not within the if. Each (recursive) call to main will ultimately call printf before terminating.
"the printf() at the end must be encountered only once" that's incorrect.
you enter main() five times, so you will exit it 5 times. Each invocation of main() enters a new copy of it, opening a new stack frame for it.
When the next invocation has printed its value and exited, the control returns to the previous invocation, at the point after the recursive call.
You should read the logic of recursion again i guess..
When the function is called from inside that function only, that presently executing function and all its parameters are pushed into the stack, and the newly called function starts executing. As this function calls gets over, the execution returns to the previous function (by popping it from the stack), and it will start executing from the point where it left..
Now, you can trace your program and check with his logic..

Resources