class WhileLoops {
	public static void main(String [] args){
		/*
		 * 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
		 * }
		 */

		int k;
		// 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)
			// 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)
		 	k=k+1;
		 	// Process k
		}

		printNums(3);
		printNums(10);
		System.out.println("0+1+2+...+10="+sumUp1(10));
		System.out.println("0+1+2+...+10="+sumUp2(10));
		System.out.println(countAs(""));
		System.out.println(countAs("that"));
		System.out.println(countAs("A"));
		System.out.println(countAs("this is Another test Again"));
		System.out.println(countAs("this is Another test A"));
	}
		
	public static void printNums(int n){
		// Every time we visit the loop guard, the following is
		// true: 0,...,k-1 HAVE BEEN printed
		int k=0;
		while(k<n){
			// Process k means print k
			System.out.print(k);
			k=k+1;
		}
		System.out.println();
	}

	public static int sumUp1(int n){
		// We return 0+1+2+3+...+n
		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+1){
			// Process k means add k to the current sum
			sum=sum+k;
			k=k+1;
		}
		return(sum);
	}

	public static int sumUp2(int n){
		// We return 0+1+2+3+...+n
		int sum=0;
		int k=-1;
		// Every time we visit the loop guard, the following is
		// true: 0,...,k HAVE BEEN added to sum
		while(k!=n){
			k=k+1;
			// Process k means add k to the current sum
			sum=sum+k;
		}
		return(sum);
	}

	public static int countAs(String s){
		// Return the number of 'A' characters appearing in s
		int numA=0;
		int n=s.length();
		// We want to process characters at positions 0,...,s.length()-1

		int k=0;
		// Every time we visit the loop guard, the following is
		// true: characters 0,...,k-1 HAVE BEEN checked 
		// and numA modified (if appropriate). In otherwords,
		// numA== the number of As appearing in
		while(k<n){
			// Process k means check if character at position k is an A
			// if so, then add 1 to numA
			if(s.charAt(k)=='A')numA=numA+1;
			k=k+1;
		}
		return(numA);
	}
}
