Computer Science 15-100, Fall 2008
Class Notes: More Conditionals and Loops
More Conditionals and Loops
import java.util.*; class MyCode { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter your choice (0-9): "); int choice = scanner.nextInt(); switch (choice) { case 0: System.out.println("Case for choice 0."); break; case 1: System.out.println("Case for choice 1."); break; // multiple cases: case 5: case 8: System.out.println("Case for choice 5 or 8."); // no break, fall-through case 9: System.out.println("Case for choice 9."); break; default: System.out.println("Case for default choice."); } } }
class SwitchDemo2 { public static void main(String[] args) { int month = 2; int year = 2000; int numDays = 0; switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: numDays = 31; break; case 4: case 6: case 9: case 11: numDays = 30; break; case 2: if ( ((year % 4 == 0) && !(year % 100 == 0)) || (year % 400 == 0) ) numDays = 29; else numDays = 28; break; default: System.out.println("Invalid month."); break; } System.out.println("Number of Days = " + numDays); } }
import java.util.*;
class MyCode {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number to test for primality: ");
int n = scanner.nextInt();
boolean isPrime = (n > 1);
for (int k=2; k<n; k++)
if (n % k == 0) {
System.out.println(n + " is divisble by " + k);
isPrime = false;
break;
}
System.out.println("isPrime = " + isPrime);
}
}
Another Example:
import java.util.*;
class MyCode {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int count = 0;
int sum = 0;
while (count < 3) {
System.out.print("Enter # " + (count+1) + " of 3 (or 'q' to quit): ");
String s = scanner.next();
if (s.equals("q"))
break;
sum += Integer.parseInt(s);
count++;
}
System.out.println("sum = " + sum);
}
}
import java.util.*;
class MyCode {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int count = 0;
int sum = 0;
while (count < 3) {
System.out.print("Enter # " + (count+1) + " of 3 (or 'q' to quit): ");
if (!scanner.hasNextInt()) {
String s = scanner.next();
if (s.equals("q"))
break;
else {
System.out.println(" Not an int: " + s + ". Please try again.");
continue;
}
}
sum += scanner.nextInt();
count++;
}
System.out.println("sum = " + sum);
}
}
import java.util.*; class MyCode { public static void main(String[] args) { System.out.println("This code demonstrates a labeled break. Note that"); System.out.println("some people believe you should never use these!"); System.out.println(); String[] strings = { "abc", "de", "fghij", "klm", "n" }; int i, j=0; char key = 'm'; boolean foundKey = false; searchForKey: for (i=0; i<strings.length; i++) for (j=0; j<strings[i].length(); j++) if (strings[i].charAt(j) == key) { foundKey = true; break searchForKey; } if (foundKey == true) System.out.println("Found " + key + " at char " + j + " of string " + i); else System.out.println("Did not find " + key); } }
import java.util.*; class MyCode { public static void main(String[] args) { System.out.println("This code demonstrates a labeled continue. Note that"); System.out.println("some people believe you should never use these!"); System.out.println(); String string = "We all live on a yellow submarine!"; String sub = "sub"; int start; boolean foundIt = false; searchForSubstring: for (start=0; start<=string.length() - sub.length(); start++) { for (int i=0; i<sub.length(); i++) { if (sub.charAt(i) != string.charAt(start+i)) continue searchForSubstring; } foundIt = true; break; } if (foundIt == true) System.out.println("Found substring starting at index " + start); else System.out.println("Did not find the substring"); } }
import java.util.*; class MyCode { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int sum = 0; int count = 0; while (true) { System.out.print("Enter # " + (count+1) + " (or 'q' to quit): "); String s = scanner.next(); if (s.equals("q")) break; sum += Integer.parseInt(s); count++; } System.out.println("sum = " + sum); } }
Style Alert: Do not use "for ( ; ; )" -- use "while (true)" import java.util.*; class MyCode { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int sum = 0; int count = 0; for (;;) { System.out.print("Enter # " + (count+1) + " (or 'q' to quit): "); String s = scanner.next(); if (s.equals("q")) break; sum += Integer.parseInt(s); count++; } System.out.println("sum = " + sum); } }
import java.util.*; class MyCode { public static void main(String[] args) { System.out.println("Enter some #'s until their sum exceeds 10."); Scanner scanner = new Scanner(System.in); int sum = 0; do { System.out.print("sum = " + sum + ". Next number: "); sum += scanner.nextInt(); } while (sum <= 10); System.out.println("sum = " + sum); } }
Example:
int x;
for (x=0; x<2; x++)
System.out.println(x);
Roughly the same as:
for (int
x=0; x<2; x++)
System.out.println(x);
But: variable's scope is limited to the for loop:
int x;
for (x=0; x<2; x++)
System.out.println(x);
System.out.println(x);
Cannot be done this way:
for (int x=0; x<2;
x++)
System.out.println(x);
System.out.println(x);
// will not compile, x
is not in scope here!
Also: cannot declare a local variable within the scope of another
local variable of the same name
int x;
for (int x=0; x<2; x++)
// will not compile --
x is already defined!
System.out.println(x);
But you can reuse variables in subsequent for loops:
for (int x=0; x<2; x++)
System.out.println(x);
for (int x=0; x<2;
x++)
System.out.println(x);
carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem