Quick Quiz
/* Please do not write a class specification or any method specification. * Instead, please write only a code segment. The code segment should * use a for-loop to print the even numbers between 1 and 10, inclusive */ for (int number=1; number <=10; number++) if ((number % 2) == 0) // Alternate: if ( (number/2) == ((number+1)/2) ) System.out.println (number); }
The Modulus (%-Operator)
Notice the solution given above. What's the funny %-sign? It is the modulus operator. Think about integer division. It "loses" the remainder, right? Well, where does it go? Don't know. But the %-operator can find it.Whereas the /-divide operator, when applied to an int, returns the integer quotient, the %-operator, only defined over ints, returns the remainder.
So, for example, "Five divided by two is two -- with one left over", so 5 / 2 = 2, 5 % 2 = 1.
A Simple Tangle
Let's write some quick code. Given a user's choice, represented as a char, let's print my 'F'irst, 'M'iddle, or 'L'ast name, or 'Q'uit.
boolean quit = false; while (!quit) { // Display the menu System.out.println (""); System.out.println (""); System.out.println ("--- Menu ---"); System.out.println ("F)irst name"); System.out.println ("M)iddle name"); System.out.println ("L)ast name"); System.out.println ("Q)uit"); System.out.println (""); System.out.println ("Enter your choice: "); // Get choice String choiceString = br.readLine(); char choice = choiceString.toUpperCase().charAt(0); // Decide what to do -- and do it if (choice == 'F') System.out.println ("Gregory"); else if (choice == 'M') System.out.println ("Michael"); else if (choice == 'L') System.out.println ("Kesden"); else if (choice == 'Q') quit = true; // or break else System.out.println ("Please select F, M, L, or Q."); }Notice how nested that code became. Does it have to be that complicated? Well, we can rewrite it and make it a bit better. To do that, we'll bail as soon as we get a match, so we can eliminate the nesting:
boolean quit = false; while (!quit) { // Display the menu System.out.println (""); System.out.println (""); System.out.println ("--- Menu ---"); System.out.println ("F)irst name"); System.out.println ("M)iddle name"); System.out.println ("L)ast name"); System.out.println ("Q)uit"); System.out.println (""); System.out.println ("Enter your choice: "); // Get choice String choiceString = br.readLine(); char choice = choiceString.toUpperCase().charAt(0); // Decide what to do -- and do it if (choice == 'F') { System.out.println ("Gregory"); continue; } if (choice == 'M') { System.out.println ("Michael"); continue; } if (choice == 'L') { System.out.println ("Kesden"); continue; } if (choice == 'Q') { quite = true; // Or simply, "break;" and skip the next line continue; } System.out.println ("Please select F, M, L, or Q."); }The above construction is better. But, it highlights the problem. We're trying to make a one-of-n decision, given a construct, the if-else, that is designed to make a one-of-two decision. So, we basically build up the the right number of choices in powers of two.
The switch-statment
When we come back next class, we'll take a look at another Java construct, the switch statement, designed for exactly these 1-of many (or even several of many) types of decisions.