/* For loops have the following form (syntax):
 * for(init; condition; increment){
 *      body
 * }
 * 
 * They have the following meaning (semantics):
 * 
 * init
 * while(condition){
 *     body
 *     increment
 * }
 * Notes: Java allows variable declaration inside init
 * If body is a single statement, then you dont need { }.
 */

class ForLoop {
	public static void main(String args[]) {

                // Compute 0+1+2+3+...+n-1
		int n=10, sum=0, k=0;
                // Every time we visit the loop guard, the following is
                // true: 0,...,k-1 HAVE BEEN added to sum
                while(k<=n){
                        // Process k means add k to the current sum
                        sum=sum+k;
                        k=k+1;
                }
		System.out.println("0+1+2+3+...+"+n+"="+sum);

		sum=0;
                // Every time we visit the for loop guard, the following is
                // true: 0,...,k-1 HAVE BEEN added to sum
		for(int j=0;j<=n;j=j+1){ 
			sum=sum+j; 
		}

		/* Same as 
		 * int j;
		 * for(j=0;j<=n;j=j+1){ sum=sum+j; }
		 */
		System.out.println("0+1+2+3+...+"+n+"="+sum);
	}
}
