//common tasks using loops

import java.util.Scanner;

class Loops {
    //method1: multiplies m by n by repeated addition
    public static int multiply(int n, int m) {
	int result1=0, result2=0, result3=0;
	//use a while loop: 
	int counter = 1; //note this is scoped outside (unlike for)
	while(counter <= n) {
	    result1 += m;
	    ++counter; //<--forgetting this results in an INIFINITE loop (!)
	}

	//use a do-while loop:
	int counter2 = 1; //again, scoped outside
	do {
	    result2 += m;
	    ++counter2; //<--
	}
	while(counter2 <= n);

	//use a for loop
	for(int counter3 = 1; counter3 <= n; counter3++) {
	    result3 += m;
	}
	//can you S.O.P(counter3) ?

	//compare all results
	if(result1 == result2 && result2 == result3) {
	    System.out.println("Nice job!");
	}
	else
	    System.out.println("Error");
	
	return result1;
    }

    //method2: sentinel-controlled loop with a switch inside
    //useful for implementing a menu
    //exercise: what does this small program do?
    public static void doMenuUntilQuit() {
	System.out.println("Welcome to the menu");
	char selection = ' ';
	String input = "";
	Scanner s = new Scanner(System.in);
	while(selection != 'q') {
	    System.out.println("Enter <q> for quit, <+> for add, <*> for multiply");
	    input = s.next();
	    selection = input.charAt(0); //what does this assume?
	    switch(selection) { 
 	     case 'q': System.out.println("Goodbye!");
		break;
	     case '+': System.out.println("Enter the two numbers to add: ");
		int a = s.nextInt();
		int b = s.nextInt();
		System.out.println("Result is: " + (a + b));
		break;
	     case '*': System.out.println("Enter the two numbers to multiply: ");
		int c = s.nextInt();
		int d = s.nextInt();
		System.out.println("Result is: " + multiply(c, d)); //note i don't need to qualify the method
		break;
	     default: System.out.println("Wrong menu option, no action taken.");
	    }

	}//while	   
    }//menu
}
