About if else statement [closed]

ok ok it’s very good question about performance.

Short answer: they will run in the same speed. Why

the compiler when it complies your code it will translate it to mechine language which is bunch of low level processor commands in RAM

In the if with one line if the condition is false the complier will ask the processor to jump 1 line this will increase the program counter with one to ignore the if block and execute the else block instead. look the overhead is just one command to increase the program counter.

program counter is register that saves which line you are executing

and in the if with 1000 line it will be simply the same just increase the program counter with 1000 to jump the if block to execute the else block. One Operation the overhead is the same.

thats it even with else is the same if condition true if block will executed then jump the else block.

Leave a Comment