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
Related
This question already has answers here:
Main calling main [duplicate]
(2 answers)
Why Output is 0000 and How? [duplicate]
(2 answers)
How does this program execute? [duplicate]
(5 answers)
understanding static int execution [duplicate]
(2 answers)
unexpected output in C (recursion)
(3 answers)
Closed 3 years ago.
why is the output contains 4 time 0's.The main call again and again until if condition become false and then it should be exit from if block.
#include <stdio.h>
int main()
{
static int i=5;
if(--i)
{
main();
printf("%d ",i);
}
}
Note the following.
the int i is static.
you are calling main reursively.
In the if condition you have a pre decrement of i
Every time you call main, the value of i will be the same as the previous call. So i will decrement each time. Since this is --i it will be 4 the first time and go to 0.
After the innermost main function returns(i==0), the printf of the main before that will be executed.
But i is static and has a value of 0. So you get 4 zeros printed for each of the main functions.
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.
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.
This question already has an answer here:
What does the following for loop syntax mean in C?
(1 answer)
Closed 8 years ago.
For example,
for (;;)
{
//do something
}
How is this different from
{
//do something
}
It's an infinite loop. Pretty much the same as writing
while (true)
{
// do something
}
This is used as an infinite loop. It is equivalent to while(1) { ... }.
It is equivalent to while(true).
A for-loop has three elements:
initializer
condition (or termination expression)
increment expression
Since this doesn't set any of them, it continues to run.
In C,
for (;;){
//do something
}
is the equivalent of
while(1){
//do something
}
(Similarly, in other languages:)
while(true){
//do something
}
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
...