Example 1: Sum the numbers from 1 to 10 Suppose we want to calculate the sum of the numbers from 1 to 10. We could do the following: sum = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10; or we could recognize that this is really the set of steps: 1. sum = 1 2. sum = sum + 2 3. sum = sum + 3 4. sum = sum + 4 5. sum = sum + 5 6. sum = sum + 6 7. sum = sum + 7 8. sum = sum + 8 9. sum = sum + 9 10. sum = sum + 10 So to write our program we will need: 1. A variable that will be used to accumulate our sum, called “sum”. 2. A variable that will represent the set of numbers that will be added to sum.. (1, 2, 3, 4, 5…) which we will call “x”. 3. Both should be integers. 4. Our loop will exit when x is greater than 10. We will loop while x < = 10 public class Sumto10 { public static void main (String args[]) { int sum, x; sum = 0; x = 1; This prints: while ( x <= 10) { sum = sum + x; x = x + 1; } The sum of the numbers from 1 to 10 is: 55 System.out.println(“The sum of the numbers from 1 to 10 is: “ + sum); } } Example 2: What if we needed to do a more generalized type of calculation? Instead of just the numbers from 1 to 10, but sum the numbers from 1 to N where N is a number input by the user… The steps are the same: sum = sum + 1, sum = sum + 2….. sum = sum + n We would do the following alterations to our program: 1. We would create a new variable “n”, and prompt the user to input an integer value for n. 2. We would loop as long as x < = n --------------------------------------------------------------------------------------------------public class Sumton { public static void main (String args[]) { int sum, x, n; System.out.println(“Please enter an integer number, N, and we will calculate the “ + “ sum from 1 to N “); n = (int) ReadItem.getLong(); sum = 0; x = 1; while ( x <= n) { sum = sum + x; x = x + 1; } System.out.println(“The sum of the numbers from 1 to “ + n + “ is: “ + sum); } } /home/hayhurst/java- java Sumton Please enter an integer number, N, and we will calculate the sum from 1 to N 20 PRE-TEST LOOPS: The sum of the numbers from 1 to 20 is: 210 Example 3: As another example, lets take the calculator program we created in a previous class using a switch statement, and modify it so that it can allow the user to enter multiple calculations. Originally it was implemented as: import java.util.*; public class switchEx2 { public static void main(String args[]) { Scanner in = new Scanner(System.in); int a,b; char ch; String input; System.out.println ("Do you want to Add, Subtract, Multiply or " + "Divide?"); System.out.print ("Please enter the first letter of what you would" + " like to do?"); ch= (in.next().toUpperCase()).charAt(0); System.out.println ("Enter first number: "); a = in.nextInt(); System.out.println ("Enter second number: "); b = in.nextInt(); switch (ch) { case 'A': System.out.println ("Answer is "+ (a+b)); break; case 'S': System.out.println ("Answer is "+ (a-b)); break; case 'M': System.out.println ("Answer is "+ (a*b)); break; case 'D': if (b != 0) System.out.println ("Answer is "+ (a/b)); else System.out.println("Division by zero"); break; default : System.out.println("Invalid Operation"); } //end of switch System.out.println("Good bye"); } } As originally implemented it only allowed to user to enter a single calculation, to change to multiple our program will need to be able to : Input the desired operation multiple times Input 2 integers for each operation Determine the operation entered and perform the calculation for each operation and print the result. We will need to allow the user to enter something special to indicate when they are finished performing calculations…. Perhaps “Q” The steps of our program will look like: 1. Prompt for and get the first operation from the user. 2. If the operation entered is “q”, exit the program and do nothing. 3. If the operation is not q, then enter the loop, a. Prompt for the 2 integers to be used in the calculation b. Identify the operation entered, and perform the calculation c. Print the results d. Prompt for and get the NEXT operation. e. Return to step 2 --------------------------------------------------------------------------------------------------------- import java.util.*; public class switchEx2 { public static void main(String args[]) { Scanner in = new Scanner(System.in); int a,b; char ch; String input; System.out.println ("Do you want to Add, Subtract, Multiply or Divide?"); System.out.print ("Please enter the first letter of what you would like to do or Q to quit"); 1 ch= (in.next().toUpperCase()).charAt(0); // read the input while ( ch != ‘Q’) { System.out.println ("Enter first number: "); a = in.nextInt(); System.out.println ("Enter second number: "); b = in.nextInt(); 2 3 switch (ch) { case 'A': System.out.println ("Answer is "+ (a+b)); break; case 'S': System.out.println ("Answer is "+ (a-b)); break; case 'M': System.out.println ("Answer is "+ (a*b)); break; case 'D': if (b != 0) System.out.println ("Answer is "+ (a/b)); else System.out.println("Division by zero"); break; default : System.out.println("Invalid Operation"); } //end of switch 5 System.out.println ("Do you want to Add, Subtract, Multiply or Divide?"); System.out.print ("Please enter the first letter of what you would like to do or Q to quit"); ch= (in.next().toUpperCase()).charAt(0); // read the input } // end of the loop body System.out.println("Good bye"); } } 1. 2. 3. 4. 5. 4 Get the initial operation to perform Loop while the operation is not quit Get the numbers used in the calculation Perform the calculation Get the next operation and return to the top of the loop Example 4: Sum the numbers from 1 to 10. public class Sumto10 { public static void main (String args[]) { int sum, x; sum = 0; x = 1; do { sum = sum + x; x = x + 1; } while (x <= 10); System.out.println("The sum of the numbers from 1 to 10 is: " + sum); } } /home/hayhurst/java- java Sumto10 The sum of the numbers from 1 to 10 is: 55 Example 5: public class Sumto10 { public static void main (String args[]) { int sum, x; sum = 0; x = 20; do { sum = sum + x; x = x + 1; } while (x <= 10); System.out.println("The sum of the numbers from 1 to 10 is: " + sum); } } /home/hayhurst/java- java Sumto10 The sum of the numbers from 1 to 10 is: 20 Example #6: For loops Sum the numbers from 1 to n. public class Sumto10for { public static void main (String args[]) { int sum, x; sum = 0; for ( x = 1 ; x <= 10 ; x ++) sum = sum + x; System.out.println("The sum of the numbers from 1 to 10 is: " + sum); } } /home/hayhurst/java- java Sumto10for The sum of the numbers from 1 to 10 is: 55 Example #7 public class Sumto10for { public static void main (String args[]) { int sum, x=1; sum = 0; for (; x <= 10 ; x ++) sum = sum + x; System.out.println("The sum of the numbers from 1 to 10 is: " + sum); } } Example #8 public class Sumto10for { public static void main (String args[]) { int sum, x=1; sum = 0; for (sum = 0, x = 1; x <= 10 ; x ++) sum = sum + x; System.out.println("The sum of the numbers from 1 to 10 is: " + sum); } } Example 8b: public class Sumto10fora { public static void main (String args[]) { int sum, x=1; sum = 0; for ( ; ; ) { sum = sum + x; x++; if (x > 10) break; } System.out.println("The sum of the numbers from 1 to 10 is: " + sum); } } Example #9: public class Sumto10for { public static void main (String args[]) { int sum; sum = 0; for (int x = 1; x <= 10 ; x ++) sum = sum + x; System.out.println("The sum of the numbers from 1 to 10 is: " + sum); } } NOTE: IF YOU DECLARE THE LOOP CONTROL VARIABLE IN THE LOOP INITIALIZATION SECTION, YOU CAN ONLY HAVE ONE STATEMENT IN THAT SECTION!!! Example 10 public class IsPrime { public static void main (String args[]) { int inputValue, i; System.out.println(“Please enter a positive Integer. “ ); inputValue = (int) ReadItem.getLong(); if ((inputValue == 2) || ( (inputValue % 2) == 0 ) System.out.println( inputValue + “is a Prime Number”); else { for (i = 3; i < inputValue; i += 2) { if ((inputValue % i) == 0) { System.out.println( inputValue + “is NOT a Prime Number”); break; } // end of if } // end of for loop if ( i == inputValue) System.out.println( inputValue + “is a Prime Number”); } // end else } }