Differences in increment operators used in a while loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • will85
    New Member
    • Jul 2010
    • 1

    Differences in increment operators used in a while loop

    I was wondering if someone could explain what is actually happening with i between these while loops:

    Code:
    i = 1;
    		while (i++ < max) {
    			arr[i] = arr[i-1] + i*i;
    			System.out.println(arr[i] + "   " + i);
    		}
    Code:
    i = 1;
    		while (++i < max) {
    			arr[i] = arr[i-1] + i*i;
    			System.out.println(arr[i] + "   " + i);
    		}
    Code:
    i = 1;
    		while (i < max) {
    			arr[i] = arr[i-1] + i*i;
    			System.out.println(arr[i] + "   " + i);
    			i++;
    		}
    Thanks for the help!
  • ahmee
    New Member
    • Mar 2010
    • 39

    #2
    in first code the i is increment e.g int i=1 so in while statement the value of i that is one is increment to 2 and then checked with max(max can be any value which you specfic to compare with.

    sEcond code is the same as the first one as in first you write i++ in whileloop and in second you write as ++i it doesnt matter both are same

    In third i is compare with max(which can be any value you specfy)

    Comment

    Working...