class ForLoop {
/* 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 { }.
*/
	public static void main(String args[]) throws Exception{
		System.out.println("sumUp3(10)="+sumUp3(10));
		System.out.println("sumUp4(10)="+sumUp4(10));
	}

	public static int sumUp3(int n){
                // We return 0+1+2+3+...+n-1
                int sum=0;
                int 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;
                }
                return(sum);
        }

	public static int sumUp4(int n){
		int sum=0;
                // Every time we visit the for loop, the following is
                // true: 0,...,k-1 HAVE BEEN added to sum

		for(int k=0;k<n;k=k+1){ sum=sum+k; }
		/* Same as 
		int k;
		for(k=0;k<n;k=k+1){ sum=sum+k; }
		*/
		return(sum);
	}
}
