two different loops, are they doing the same thing ?
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.
There is quite a bit of difference between the two, but it is impossible to tell if the differences are intentional or not. Maybe you could explain a little more about why you are asking your question and what happened when you ran the code.
I think they are missing { and } ,is right?
They are have some differece:
code1:
print array[i],array[i-1] and array[i+1] first,then i++
code2:
i++ first and then print array,so the results of printting is not same. And the value of positon are different too.The first one is less than the second one,I think.
Yep, I think you're right bamboo, it makes a little more sense if he's missed out a whole { } block on that while, but there are still logic difference in there.
increasing i at the start means that &array[1] is never checked against block, so that'd need fixing for starters.
Reading the code the indentation implies that the while is missing the braces, but the break in the for loop could imply that the looping should stop when the if condition is met, meaning that the while condition should have consisted of two conditions!
Yes the incrementing of the loop counter in the while loop is wrong, if the braces are missing but again are the braces really meant to be there?
As I said without knowing the intent of the code it is not possible to be any more precise.
for (int i =1 ; array[i]!=0; i++)
if (&array[i] == block)
{
printf("array[i] = %d\n ", array[i]);
printf("array[i+1]= %d\n", array[i+1]);
printf("array[i-1]= %d\n", array[i-1]);
position = i;
blockSize = *block;
break;
}
Loop 2:
Code:
int i =1;
while(array[i]!=0)
i++;
if (&array[i] == block)
{
printf("array[i] = %d\n ", array[i]);
printf("array[i+1]= %d\n", array[i+1]);
printf("array[i-1]= %d\n", array[i-1]);
position = i;
blockSize = *block;
}
are this achieve the same thing ? if not what are the difference in the output?
If you did indeed want Loop 2 to do the if and printf's inside the while loop then no the two loops do not do the same thing.
Loop 2 will start looking at &array[2] and check if that is == to block. If you are going to use a while loop to iterate through an array in place of a for loop then you should put the i++ after your last usage of it. So an updated Loop 2 would be:
Code:
int i =1;
while(array[i]!=0)
{
if (&array[i] == block) /* First time through i is now 1 not 2 */
{
printf("array[i] = %d\n ", array[i]);
printf("array[i+1]= %d\n", array[i+1]);
printf("array[i-1]= %d\n", array[i-1]);
position = i;
blockSize = *block;
}
i++;
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.