For Loop

I learnt something today. Something so trivial I don’t know why I did not know this before today.

The last part of the for loop is evaluated AFTER the body of the loop has executed. Which means whether you use post increment or pre increment the number of loops remain the same.

This, it now seems, makes pre increment slightly better because it requires no temporary variable.

[ATTACH=full]78504[/ATTACH]

Good evening.

2 Likes

What a @TerribleWaste waste of my data

1 Like

Is it Arab data?

Yea na sikushikanisha kitu isipokuwa nilifanya it

niaje blue sky

Sorry. I was just making a silly reference to a Busta song. Anyways, it’s all good. No one knows all, right?

Poa sana.

@TerribleWaste , wacha nikuchekelee kidogo and also ask you a stupid question.

Kwani you have never stepped through your code before, how do you debug.

I will give you a like and leave you with probably the most important piece of advice i can give you. “IN SOFTWARE ENGINEERING, THE MOST IMPORTANT THING TO UNDERSTAND IS WHY AND NOT HOW”

1 Like

I never really paid attention, I guess. I’ve always used the post increment and the VS debugger sort of jumps through the line quickly I never noticed when the index variable has changed. I guess it’s hard to see something if you’re not looking for it. Never too late to learn though.

PHP is a @TerribleWaste of a language.

People say that… yet it still powers 80% of the web. You comment was processed by PHP, how about that?

3 Likes

@TerribleWaste

A for loop is short for of below


unsigned int i =0;
unsigned int max_val=10;
for(;;){
    if(i<=max_val){
        //do something 
    }
    else{
       break;
    }
    i++; //increment
 }
1 Like

I think there is no difference in the number of loops since the compiler subtly changes any pre-increment to post-increment, contrary to your conclusion.
Therefore if you provided pre-increment, the compiler will go an extra mile to optimize it before executing the body. Post-increment on the other hand requires no further optimization and the compiler will therefore just jump straight into executing the body.
This makes sense because from the basics of php, statements get executed line by line (lines are separated by semi-colons) and I therefore don’t think that the increment gets executed after the body in the code you provided. If that were the case, it would contrast this very rule of php.

How is it then that zero gets printed. If the index was incremented before the body of the loop was executed on pre increment $i would be == 1 before the body is executed. Right?

You are right. I think because post-increment doesn’t really change the value of a variable until the next time it’s evaluated which will be when the loop comes back around after executing the body.

It all translates to something similar to this

mov  cx, 1

myloop:
   push cx
    ; Your loop content
    pop  cx

    loop myloop

You should all make a point of reading a book name “the art of assembly” by Randall hyde

2 Likes

@kukufry , it’s always a post increment/decrement

1 Like

Just to clarify: When I said pre increment I meant ++$i, and post increment => $i++
I was a little confused by what @kukufry said about compiler optimizations but I now see what you meant.

1 Like

I like to break to a new line for the braces and indent the code in them. Easier to follow.

Let me close this discussion by saying that HE RAO WNBP.

Waiter reta birr.

So do I. I was typing this on the console.

1 Like