This question already has answers here:
Why choosing for (;;){} over while(1)? [duplicate]
(4 answers)
Closed 9 years ago.
why c language needs this for(; ;) loop ? It is actually an infinite loop and while(1) also infinite loop. My question is that which one we should use ? I want to know which one is more efficient according to compiler point of view?
Both are same. I believe infinite loops are needed when you want to break on a certain condition which the executing code knows only once it enters the loop.
For me this looks more intuitive and easier:
while(1) {}
while(true) {}
Remember even this is an infinite loop :
// just don't provide the condition
for(int i=0; ;i++) // don't mind the syntax as I don't belong to C
Whichever fits your needs. Both do the same thing.
For an infinite loop, both do the same thing, but for and while are used for different things, and are not generally interchangeable.
Related
This question already has answers here:
What is the full "for" loop syntax in C?
(6 answers)
Closed 5 years ago.
My question is simple,thus I will not go in deep
can we use for() loop without condition like this
for(;;space+=1)
{
printf(" ");
break;
}
Of course you can. An empty condition is taken to evaluate to 1.
for (;;){/*ToDo - your code here*/} is idiomatic C.
Yes it is perfectly correct to do so.
But since you have provided a break immediately after printf, it will only execute once. I'm not sure whether this is what you wanted. But if so, then this works fine.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
If I can do a cycle with for and while both, which should I choose?
[for]
int num = 10;
int i;
for (i=0; i<num; i++)
{
/* ... */
}
[while]
int num = 10;
int i = num;
while ( i-- )
{
/* ... */
}
The choice between for and while is just matter of clarity:
K&R . Chapter 1. The For statment:
The choice between while and for is arbitrary, based on which seems
clearer. The for is usually appropriate for loops in which the initialization and
increment are single statements and logically related, since it is more compact
than while and it keeps the loop control statements together in one place.
Performance, of course, depends on the implementation in the language used. However, in most cases and with most compilers, the generated code from both loops will pretty much be the same.
A rule of thumb would be to use while when you don't know exactly how many times you want to iterate. In your example, it doesn't make much difference. Use what's clearer to you.
And yes, declarations in the for loop are only allowed in newer versions of C (C99).
Usually, you'd use a for loop for something like that because you know before you start the loop how many times you need the loop to run. while loops are more for when you don't know how many times you're going to have to repeat, and something that the user inputs or some random int will change it
In the example above, you should use a for loop since it clearly expresses the intent of what you intend. "Do this loop num times". Also, it is much simpler for the compiler to potentially optimize for loops into vector assembly operations.
You should use a while loop when you have a clearly expressible condition like
"Read lines until EOF" or some other condition that doesn't have an easily enumerable solution.
Also, I consider it poor practice to depend on the fact that 0 is false. You should clearly write the condition that exits the loop.
Every program solve in while loop that can be solved in for loop,but for loop suitable for:-
1--> If i know how many time the loop Will execute and while loop is suitable for if i don't know how many time the loop will be executed.
This question already has answers here:
C code won't finish running
(4 answers)
Closed 9 years ago.
The purpose of this code is to construct a char Hadamard matrix of the size of my choosing.
This question is related to a previous question I asked. The answer given there was an integer not char matrix, but the code here is pretty much the same format.
The code compiles but when executed it doesn't finish and I don't know why. When executed infinite 2's are printed.
I get the same result when swap the dynamic Hadamard matrix section for one of a fixed size.
Note: I've no idea what your program does, but obviously this is wrong. You failed to actually change the control variable in your for-loop (which can be done in the final expression or the loop body itself).
Change this:
for (ind=1;ind<=sizeH;ind*2)
to this:
for (ind=1;ind<=sizeH;ind*=2) // << note *=
This question already has answers here:
When have you come upon the halting problem in the field? [closed]
(13 answers)
Closed 9 years ago.
I'm building a compiler for a custom language. Is it possible for the compiler to detect any infinite looping condition without running the program?
If so how can I implement it?
You may be able to detect some infinite loops, but in general you can't detect all possible infinite loops (unless your custom language is specifically designed to eliminate general looping constructs). See http://en.wikipedia.org/wiki/Halting_problem.
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Is “for(;;)” faster than “while (TRUE)”? If not, why do people use it?
Why use the ugly for(;;) syntax instead of the slightly better looking while(true) loop?
There is no advantage to for(;;) over while(1).
Use while(1), because it's easier to comprehend (imho). I've never seen a for(;;) used before, and it may be confusing to others who view your code, and may wonder the same thing you just asked.
EDIT: Here's a link: while (1) Vs. for (;;) Is there a speed difference?
It basically says that they both generate the same code. In assembly, it's jmp ....
Less typing? It's shorter. And on a QWERTY keyboard, typing for alternates between the left and right hand, and while has three consecutive letters that are typed with the right hand, making typing it potentially even slower.