Computer Science 15-100, Fall 2008
Class Notes: Loops (An Introduction)
Loops
class MyCode { public static void main(String[] args) { int counter; for (counter=1; counter<=5; counter++) { System.out.println(counter); } } }
class MyCode { public static void main(String[] args) { int counter; int n = 10; for (counter=1; counter<=n; counter++) { System.out.println(counter); } } }
class MyCode { public static void main(String[] args) { int counter; int n = 10; int sum = 0; for (counter=1; counter<=n; counter++) { sum = sum + counter; } System.out.println(sum); } }
class MyCode {
public static void main(String[] args) {
int counter;
int n = 10;
int sum = 0;
for (counter=1; counter<=n; counter++) {
if (counter % 2 != 0)
sum = sum + counter;
}
System.out.println(sum);
}
}
class MyCode {
public static void main(String[] args) {
int counter;
int n = 10;
int sum = 0;
for (counter=1; counter<=n; counter+=2) {
sum = sum + counter;
}
System.out.println(sum);
}
}
class MyCode { public static void main(String[] args) { int i; // i for index, rather than counter String s = "Carpe diem"; for (i=0; i<s.length(); i++) { char c = s.charAt(i); System.out.println(c); } } }
class MyCode { public static void main(String[] args) { int i; String s = "Carpe diem"; for (i=s.length()-1; i>=0; i--) { char c = s.charAt(i); System.out.println(c); } } }
class MyCode { public static void main(String[] args) { int x = 35; int y = 5; int quotient = 0; while (x >= y) { x -= y; quotient++; } System.out.println(quotient); } }
class MyCode { public static void main(String[] args) { String s1 = "hi"; String s2 = "bonjour"; while (s1.length() < s2.length()) { s1 = s1 + "hi"; } System.out.println(s1); System.out.println(s2); } }
class IsPrimeAndNthPrimeExamples { ////////////////////////////////////////// /// isPrime ////////////////////////////////////////// // Returns true if n is a prime number, and false otherwise. // Do not worry about efficiency (yet). We'll make this faster soon... public static boolean isPrime(int n) { int counter; if (n < 2) return false; for (counter=2; counter<n; counter++) { if (n % counter == 0) return false; } return true; } public static void testIsPrime() { System.out.print("Testing isPrime()... "); assert(isPrime(2)); assert(isPrime(3)); assert(!isPrime(4)); assert(isPrime(5)); assert(!isPrime(6)); assert(isPrime(8179)); assert(!isPrime(8211)); assert(!isPrime(-3)); assert(!isPrime(0)); assert(!isPrime(1)); System.out.println("Passed all tests!"); } ////////////////////////////////////////// /// nthPrime ////////////////////////////////////////// // Returns the nth prime number, or -1 if n is non-positive. // Do not worry about efficiency (yet). We'll make this faster soon... public static int nthPrime(int n) { int counter = 1; int numberOfPrimes = 0; if (n < 1) return -1; while (numberOfPrimes < n) { counter++; if (isPrime(counter)) numberOfPrimes++; } return counter; } public static void testNthPrime() { System.out.print("Testing nthPrime()... "); assert(nthPrime(1) == 2); assert(nthPrime(2) == 3); assert(nthPrime(3) == 5); assert(nthPrime(4) == 7); assert(nthPrime(5) == 11); assert(nthPrime(6) == 13); assert(nthPrime(-5) == -1); assert(nthPrime(0) == -1); System.out.println("Passed all tests!"); } ////////////////////////////////////////// /// main ////////////////////////////////////////// public static void main(String[] args) { testIsPrime(); testNthPrime(); } }
carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem