/*
 * while(condition){
 * 	body
 * }
 * evaluates condition, if true, then does body, then
 * evaluates condition, if true, then does body, then
 * evaluates condition, if true, then does body, then
 * ...
 *
 * Note: if condition is false, execution continues with
 * the first instruction after the loop.
 * Note: condition is also know as the loop guard
 * 
 * int i=0;
 * while(false){
 * 	i=i+1; // Never executes
 * }
 * while(true){
 * 	i=i+1; // executes forever
 * }
 */

class WhileLoops {
	public static void main(String [] args){

		int k;
		int n=10;
		// Process the n natural numbers 0,...,n-1
		// Every time we visit the loop guard, the following is
		// true: 0,...,k-1 HAVE BEEN processed
		k=0;
		while(k<n){ // Can replace this with (k!=n but...
			// Process k
			k=k+1;
		}
		
		// Process the n+1 natural numbers 0,...,n
		// Every time we visit the loop guard, the following is
		// true: 0,...,k HAVE BEEN processed
		k=-1;
		while(k<n){ // Can replace this with (k!=n) but...
		 	k=k+1;
		 	// Process k
		}

		// Every time we visit the loop guard, the following is
		// true: 0,...,k-1 HAVE BEEN printed
		k=0;
		while(k<n){
			// Process k means print k
			System.out.print(k);
			k=k+1;
		}
		System.out.println();

		// We compute 0+1+2+3+...+n
		int 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+1){
			// Process k means add k to the current sum
			sum=sum+k;
			k=k+1;
		}
		System.out.println("0+1+2+3+...+"+n+"="+sum);

		// Exercise: Fix the code below
		sum=0;
		k=0;
		// Every time we visit the loop guard, the following is
		// true: ?
		while(k!=n+1){
			k=k+1; // don't move this
			sum=sum+k; // don't move this
		}
		System.out.println("0+1+2+3+...+"+n+"="+sum);
	}
}
