why for(;;) instead of while(1)? [duplicate] - c

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.

Related

Why while(true) is bad practice? [closed]

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 7 years ago.
Improve this question
I use while true loop in many projects. It serves me well and it's not over-complicated. So, why it is a bad idea to use while true loop infinitely running background processes. Thanks for your answers.
My project leader doesn't allow this kind of loop in my code, he's arguing that it has to be replaced by while(!exit_flag)
That's very debatable. while (!exit_flag) suggests that the loop has a natural exit condition which it will reach at some specific point by itself, e.g. something counting from 0 to 100 and then leaving the loop. However, if you are writing this loop to create a permanent daemon or event loop, then the point of this loop is to keep the program running indefinitely. Nothing says indefinitely better than while (true).
There's no real technical difference between both; it's just a matter of readability and expression of intent. Of course, you'll have to square this with the people who will read that code in the end. If their policy is while (!exit_flag), so be it.
It is not a bad practice, it just means that you did not think your code through.
The condition is required to tell the loop when to finish looping. If you have a terminating point, then why not put it as a condition?
var i = 0;
while(true){
i++;
if(i == 100)
break;
}
This is fine code. But so is this:
var i = 0;
while(i != 100){
i++;
}
Both methods are correct, but if someone else is reading your code, it is much easier to see when the loop will stop iterating if it is right there in the condition.
Hm, ask the one who claimed it was "a bad idea".
What I could think of is, that any loop running indefinitely must come to an end at some point, unless, of course, you expect your program to run forever.
If while is not given any criteria to terminate, there would need to be some other kind of mechanism to terminate the loop hidden somewehere inside the looping code, like return, break or (yuck - Basic - but that's probably where the statement origins - a "Goto" statement jumping somewehere outside the loop) to terminate the loop.
Code like this may be hard to read and debug, and formerly was an indication that a programmer did not care much about writing clean code. Nevertheless nowadays with event driven applications and structured error handling (see try ... catch structures) there are clean and easy to read ways to exit any kind of loop any time, so while (true) can be used without hassles.
Greetz
ALina

Datatype which can store very large value in C [duplicate]

This question already has answers here:
Are there any solid large integer implementations in C? [closed]
(7 answers)
Closed 8 years ago.
Recently in programming contest in Here, the problem is pretty straight forward but catch is with worst case scenario which we have to handle data of size 10^10000 .
I tried the program in python which is straight forward as i don't have to specify the datatype(It is taken care by the compiler ) but when i tried with C I couldn't find the correct datatype .
(I tried uintmax_t which didn't work out too).
So how to approach very huge type of data's in C ?
There is no built-in datatype in C that can store that big values. You will either have to write your own implementation or use a library. As this is a competition, though the second is not an option. Every now and then similar problems appear and usually the best approach is to use another language e.g. java(as it is usually available on competitions).

Is it a good practice to write multiple concise statements in one line? [closed]

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
I am prone to writing code like this:
if (*t) while (*++t);
It reads: if string t does not start with /0, then move to the end.
Note the while loop has no body, so the semicolon terminates it.
I'd like to know if it is good practice to do this? Why and why not?
C is one of the oldest popular language in use today. I believe there's a good chance of finding one or more established style guide(s).
I know that Google has one for their C++ open source projects - http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml
Can anyone point me to resources on why or why not write code in certain manner?
Usually it is a good practice to write separate lines of code. Like in case of large pieces of code, debugging is clearer if we write code in separate lines.
It depends! Who is going to have to read and maintain this code? Coding standards exist for two major reasons:
To make code more readable and maintainable. When there are multiple developers, it makes code more consisent (which is more readable).
To discourage common errors. For example, a standard might require putting literals first in conditionals to discourage the assignment-as-comparison bug.
How do these goals apply to your specific code? Are you prone to making mistakes? If this is Linux kernel code, it's a lot more tolerable to have code like this than if it's a web app maintained by entry level programmers.
It reads: if string t does not start with /0, then move to the end.
Then consider putting a comment on it that says that.
Surprisingly - it is usually more expensive to maintain code over time than to write it in the first place. Maintenance costs are minimized if code is more readable.
There are three audiences for your code. You should think of how valuable their time is while you are formatting:
Fellow coders, including your co-workers and code-reviewers. You
want these people to have a high reputation of you. You should write code that is easily understandable for them.
Your future self. Convoluted code may be obvious while you are
writing it, but pick it up again in two weeks, and you will not
remember what it means. The 'concise' statement that you wrote in 10
minutes will someday take you 20 minutes to decipher.
The Optimizing Compiler, which will produce efficient code no matter
whether your line is concise or not. The compiler does not care - try to save time for the other two. (Cue angry remarks about this item. I am in favor of writing efficient code, but concise styles like the one we are describing here will not affect compiler efficiency.)
Bad practice, because not easy to parse. I'd do
while (*t) ++t;
and let the compiler do the tiny bit of optimization.
The textual translation of it reads even shorter than yours
advance t until it points to a 0
Although you can write some pretty clever code in one line in C, it's usually not good practice in terms of readability and ease of maintenance. What's straightforward for you to understand may look completely foreign to someone maintaining your code in future.
You need to strike a balance between conciseness and readability. To this end, it's usually better to separate the code out so each line does one thing.

While vs. for: what is the best? [closed]

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.

why c language needs this for(; ;) loop? [duplicate]

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.

Resources