Why for loop without any working code (in third part) is there?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
The "end action", for example ai++, can be omitted if it's done in the loop block.
Code:
for (fint ai = 0; ai < N; )
A[ai++] = N - getfi();
The advantage of a for loop is that you can see the "initial action", "leave condition", "end action" alltogether.
Here it is still readable because the loop body is only one line.
The normal loop would use a cpu cycle more:
Code:
for (fint ai = 0; ai < N; ai++ )
A[ai] = N - getfi();
Last edited by MadeInGermany; 12-13-2023 at 12:43 AM.
I would always use "the third part" because this what every programmer expects to see.
It's kinda like "Paris In The The Spring." Your wandering eye, "knowing" what to expect, might not have noticed the second "The" until I just mentioned it . . .
. . . and a programmer, while frantically trying to locate a bug, just might not either.
If you're lucky, they might just say "D-oh!" But they might instead say something unprintable. So, "when you write code, think about the next person who will read it – under pressure and in a hurry."
Last edited by sundialsvcs; 12-13-2023 at 08:28 AM.
Title: Why for loop without any working code (in third part) is there?
...in the main() there is twice given a for-loop, that has empty 3rd part, i.e. nothing gets executed.
Given the thread title, it seems to me that you are saying nothing gets executed, that is the loop is not executed due to the missing third expression, so why is the loop even there.
If so, that is not correct.
All three expressions are optional, the semicolons are not, so that even with for(;;); the loop will be executed (endlessly in this case).
Quote:
Originally Posted by ajiten
Kindly help by telling why such for-loop was used twice.
Only the person who wrote it can tell you why they did it in that way.
Last edited by astrogeek; 12-13-2023 at 05:37 PM.
Reason: typos
Only the person who wrote it can tell you why they did it in that way.
But plenty of programmers who have to follow that person, and debug his work, will be "mightily annoyed." Yes, there is a reason why you should write your code in a way that conforms to your successor's expectations. Your successor wants above all to understand your (erroneous ...) intentions "at a glance." Because, anything else is "a waste of time," and "time is money."
Frankly, there are quite a few times over the years when I looked at a block of code, said to myself "what the f&ck is this?" Ripped the whole thing out and re-wrote it in a way that was much more obvious. (Granted: Some of that code was so old that it had been written to cope with hardware restrictions that, quite thankfully, no longer exist.)
Last edited by sundialsvcs; 12-18-2023 at 08:37 AM.
There's a website called https://godbolt.org/ that you can use to check the assembler that C/C++ code would become.
The asm produced by the following two programs is not identical. I'll leave the interpretation as an exercise to someone who knows ASM better than I do.
Code:
#include <stdio.h>
int main()
{
for (int i = 0; i < 10;) {
printf("%d\n", i++);
}
return 0;
}
Code:
#include <stdio.h>
int main()
{
for (int i = 0; i < 10; i++) {
printf("%d\n", i);
}
return 0;
}
There's a website called https://godbolt.org/ that you can use to check the assembler that C/C++ code would become.
The asm produced by the following two programs is not identical. I'll leave the interpretation as an exercise to someone who knows ASM better than I do.
Code:
#include <stdio.h>
int main()
{
for (int i = 0; i < 10;) {
printf("%d\n", i++);
}
return 0;
}
Code:
#include <stdio.h>
int main()
{
for (int i = 0; i < 10; i++) {
printf("%d\n", i);
}
return 0;
}
Yes, you are right, they are not identical.
the question is if you switch on optimization what will be the result.
Yes, you are right, they are not identical.
the question is if you switch on optimization what will be the result.
I get the same instructions, but the order is slightly different. I guess it would be the same amount of cycles either way, but it's hard to be sure since modern CPUs are complicated.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.