0

Good day! Why, if you add the boolean operator <= to the for loop, then the count will start from one, not from zero

2nd Jun 2021, 6:04 PM
‎أندريه‎
‎أندريه‎ - avatar
2 odpowiedzi
+ 1
<= is not boolean operator but comparison operator if you use it in the comparison check of your counter as 'counter <= max' instead of 'counter < max', that doesn't change the start value ^^ the start value is what you define 'counter' to be before starting the loop... if you want 'max' iterations, either initialize 'counter' at zero and use 'counter < max' OR initialize 'counter' at one and use 'counter <= max', because both [0,1,2,...,max-1] and [0,1,2,...,max] have 'max' elements ;P
2nd Jun 2021, 6:25 PM
visph
visph - avatar
0
Because index start from 0 and end with (length -1) so if you do <= then index should start from 1 but you may get exception. For example if you have an array of 5 elements so if you do like this: for (int i = 1; i <= 5; i++) { } in this case you will get exception if you access last element by doing arr[i] because last index is 4 but you are accessing 5. In this case you have to access data of an array by doing arr[i - 1].
2nd Jun 2021, 6:18 PM
A͢J
A͢J - avatar