this is probably a very noob question but I was what the result of this would be:
int someVariable = 1;
while (callFunction(someVariable));
if (someVariable = 1) {
printf("a1");
} else {
printf("a2");
}
callFunction (int i) {
while (i< 100000000) {
i++;
}
return 0;
}
so when you hit the while loop
while (callFunction(someVariable));
does a thread wait at that loop until it finishes and then to
if(someVariable == 1) {
printf("a1");
} else {
printf("a2");
}
or does it skip and move to the if condition, print “a2” and then after the loop has finished goes through the if condition again?
UPDATE: This isn’t ment to be valid c code just psuedo, maybe I didn’t word it right, basically what I’m trying to figure out is what the different between a loop like while (callFunction(someVariable)); is vs
while (callFunction(someVariable)){}
i also changed the bolded part in my code i.e ** int someVariable = 1; **, I was doing an endless loop which wasn’t my intention.
UPDATE
No practical difference.
;delimits an empty statement,{}is a scope without statements. Any compiler can be expected to produce identical code.Of course, if you want to do something in each iteration of the loop,
{}creates a “scope” in which you can create types, typedefs and variables as well as call functions: on reaching the ‘}’ or having an uncaught exception, the local content is cleaned up – with destructors called and any identifiers/symbols use forgotten as the compiler continues….ORIGINAL ANSWER
This…
…just wastes a lot of CPU time, if the compiler’s optimiser doesn’t remove the loop on the basis that it does no externally-visible work – i.e. that there are no side-effects of the loop on the state of anything other that “i” and that that’s irrelevant because the function returns without using i again. If always returns “1”, which means the calling code…
…is equivalent to…
…which simply loops forever.
Consequently, the rest of the program – after this
whileloop – is never executed.It’s very hard to guess what you were really trying to do.
To get better at programming yourself – understanding the behaviour of your code – you should probably do one or both of: