Does Objective-C execute code line by line?

I'm really new to Objective-C programming and programming in general, so forgive me if this is a super obvious question:

I am wondering if Objective-C code works line by line. What I mean is that if it processes one line before moving on to another, or just runs the next line regardless of whether the previous line has ended or not.

For example,

int difference = number1 - number2;
if (difference < 0) {
    difference = difference + 10;
}
result = difference;

      

Say that number1 = 3

and number2 = 7

. When I run this code, it will go line by line and run the if block up to line 5 and give me result = 6

, or will it start run the if block and line 5 at the same time and give me result = -4

?

Thanks in advance!

EDIT: Modified to append due to Obj-C bug.

+3


source to share


2 answers


As far as you think, you can assume that it works line by line.



In fact, the compiler can put the code in a more efficient ordering (i.e., make sure the divisions are not too close together, as this can slow things down). However, even when the compiler reorders things, it ensures that the result is calculated before it is needed. When you build code in a fully optimized way (release mode), if you debug it, you can see that the code has indeed been reordered (the debugger jumps when you weren't expecting it). However, when you think about your code, it's safe to assume that it runs it in the order in which you write it.

+7


source


If you are new to programming, all the programming languages ​​that you come across at the beginning of your learning will be done sequentially. This means that the next one occurs only after the end of the previous one. However, languages ​​like Objective C are not line oriented, and it is not strings that are important, but expressions like assignment ending with a semicolon! or if.



0


source







All Articles