Could anyone tell me what this (int i=total-1; i>=0; i-- ) means please ?

 

Im new to programming and I have found this and I cant work out what it does

thanks

 


didnt think of looking in wikipedia,

thankyou

 

Define a variable integer (in this example call it i) to have a start value equal to the value referenced by the constant, variable or expression (expression in this example total-1) now so long as the variable i is greater than or equal to zero (i>=0) then for each iteration of the for loop subtract one from the current value in variable i as denoted by the expression i--.

C reference libray can help with language structure and syntax

https://www-s.acm.illinois.edu/webmonkeys/book/c_guide//

 
Ickyrus:

Define a variable integer (in this example call it i) to have a start value equal to the value referenced by the constant, variable or expression (expression in this example total-1) now so long as the variable i is greater than or equal to zero (i>=0) then for each iteration of the for loop subtract one from the current value in variable i as denoted by the expression i--.

C reference libray can help with language structure and syntax

https://www-s.acm.illinois.edu/webmonkeys/book/c_guide//


(int i=total-1; i>=0; i-- )

why total - 1, the the - 1 part ?


 
MickGlancy:

(int i=total-1; i>=0; i-- )

why total - 1, the the - 1 part ?



Its ok I get it

thank you very much

 
MickGlancy:


Its ok I get it

thank you very much

from wikipedia


The three control expressions, separated by semicolons here, are from left to right the initializer expression, the loop test expression, and the counting expression. The initializer is evaluated exactly once right at the start of the loop. The loop test expression is evaluated at the beginning of each iteration through the loop, and determines when the loop should exit. Finally, the counting expression is evaluated at the end of each loop iteration - even if continue is called - and is usually responsible for altering the loop variable.

 
    for(pos = OrdersTotal()-1; pos >= 0 ; pos--) if (
        OrderSelect(pos, SELECT_BY_POS)                 // Only my orders w/
    &&  OrderMagicNumber()  == magic.number             // my magic number
    &&  OrderSymbol()       == Symbol() ){              // and my pair.
Usually used in a order select loop, selecting by position 0...total-1. A variable name like pos or position would be a better choice.
 

(int i=total-1; i>=0; i-- )

For example let say total is 10 so that code means...

( this whole calculation below are integer based...

"total-1;"

i is "total - 1" so...

i is 9


"i>=0"

then if i more than or equal than 0 it will do some function {} then it will do next line "i--"...


"i--"

"i--" which means reduce i by 1


since now i is still 9 so i will be reduced by 1 using "i--"


it will be looping until the second line is meet, so it mean it will go over and over to test "i>=0" then if its true it will goes to "i--" which is reduce i by 1)


so in looping it will be like this:

declaration, total is 10

total=10;

1st loop => ( i is 9 because i=[total=10]-1 ; 9 is more than 0 ; i will be reduced 1 so i now is 8) {do some function}

2nd loop => (i is now 8 because i-- at 1st loop; 8 is more than 0; so i will be reduced 1 so i now is 7) {do some function}

3rd loop => (i is now 7 because i-- at 2nd loop; 7 is more than 0; so i will be reduced 1 so i now is 6) {do some function}

4th loop => (i is now 6 because i-- at 3rd loop; 6 is more than 0; so i will be reduced 1 so i now is 5) {do some function}

and so on until (i is now -1 because i--; -1 is less than and not equal than 0; so it will not be reduced 1 anymore) {the function will stop} and go to next code row

 

that is not quite correct. As you said, in the first iteration of the loop i is 9 but in any operations during that first iteration of the loop, i will still be 9

 


Thankyou

Reason: