Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

execution order of loops in C

Tags:

c++

c

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.

like image 629
Saad Avatar asked Jan 19 '23 16:01

Saad


2 Answers

The code inside a function is executed sequentially, by a single thread. Even if you send an other thread to your function it will execute it sequentually as well.

This is true to 99% of programming languages now days.

like image 53
vbence Avatar answered Jan 28 '23 22:01

vbence


UPDATE

basically what I'm trying to figure out is what the different between a loop like while (callFunction(someVariable)); is vs while (callFunction(someVariable)){}

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...

callFunction(int i){
     while (i< 100000000){
         i++;
     }
     return 1;
}

...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...

while (callFunction(someVariable)); 

...is equivalent to...

while (1)
    ;

...which simply loops forever.

Consequently, the rest of the program - after this while loop - 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:

  • insert output statements into your program so you can see how the value of variables is changing as the program executes, and whether it's exiting loops
  • use a debugger to do the same
like image 24
Tony Delroy Avatar answered Jan 28 '23 23:01

Tony Delroy