Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in More Conditionals Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in
Primality testing class primalityTestSlow { public static void main (String arg[]) { int n = 42, d; if (n <= 1) { System.out.println(n + “ is not a prime.”); } else { for (d=2; d<=n/2; d++) { if ((n%d)==0) { break; if (d > n/2) { System.out.println(n + “ is a prime.”); } } }
Primality testing class primalityTestLittleBetter { public static void main (String arg[]) { int n = 42, d; if (n <= 1) { System.out.println(n + “ is not a prime.”); } else { for (d=2; d*d<=n; d++) { if ((n%d)==0) { break; if (d*d > n) { System.out.println(n + “ is a prime.”); } } }
continue Allows you to skip parts of a for or while or do-while loop statements Example (want to print two-digit numbers with both digits odd) for (i=10; i<100; i++) { if ((i%2)==0) { continue; } if (((i/10)%2)==1) { System.out.println(i + “ has odd digits.”);
Perfect numbers class perfectNumber { public static void main (String arg[]) { int n = 24; int d, sigma_n = 1+n; for (d=2; d<=n/2; d++) { if ((n%d) != 0) { continue; } sigma_n += d; if (sigma_n == 2*n) { System.out.println (n + “ is perfect.”);
switch-case An alternative of if-else if-…-else switch (expression) { case constant1: // integer or character statements1 case constant2: statements2 … case constantN: statementsN default: statementsD }
switch-case Same as if (expression==constant1) { statements1 … statementsN statementsD } else if (expression==constant2) { statements3 // continued on next slide
switch-case else if (expression==constant3) { statements3 statements4 … statementsN statementsD } else if (expression==constantN) { else {
switch-case with break switch (expression) { case constant1: statements1 break; case constant2: statements2 … case constantN: statementsN default: statementsD }
switch-case with break Same as if (expression==constant1) { statements1 } else if (expression==constant2) { statements2 … else if (expression==constantN) { statementsN else { statementsD
switch-case: more flavors switch (expression) { case constant1: case constant2: statements1 break; case constant3: statements3 … case constantN: statementsN default: statementsD }
switch-case: more flavors Same as if ((expression==constant1) || (expression==constant2)) { statements1 } else if (expression==constant3) { statements3 … else if (expression==constantN) { statementsN else { statementsD
Classification of numbers class classifyNumbers { public static void main (String arg[]) { int n = 8; switch (n) { case 0 : System.out.println(“Zero!”); break; case 1 : System.out.println(“Smallest positive!”); case 2 : System.out.println(“Smallest prime!”); // continued in next slide
Classification of numbers case 3 : System.out.println(“Smallest odd prime!”); break; case 4 : System.out.println(“Smallest prime squared!”); case 5 : System.out.println(“Number of fingers!”); case 6 : System.out.println(“Smallest perfect!”); // continued in next slide
Classification of numbers case 7 : System.out.println(“North seven stars!”); break; case 8 : System.out.println(“Smallest prime cubed!”); case 9 : System.out.println(“Smallest odd prime squared!”); default : System.out.println(“Not a digit!”); } // end of switch } // end of main } // end of class
More classification class differentClassification { public static void main (String arg[]) { int n = 8; switch (n) { case 2: case 3: case 5: case 7: System.out.println(“Prime!”); break; case 1: case 4: case 9: System.out.println(“Square!”); // continued in next slide
More classification case 6: System.out.println(“Perfect!”); break; System.out.println(“Cube!”); case 0: System.out.println(“Zero the Great!”); default: System.out.println(“Not a digit!”); } // end switch } // end main } // end class
More example of switch class rainbow { public static void main (String arg[]) { char c = ‘V’; switch (c) { case ‘V’ : case ‘v’ : System.out.println (“Violet”); break; case ‘I’ : case ‘i’ : System.out.println (“Indigo”); case ‘B’ : case ‘b’ : System.out.println (“Blue”);
More example of switch case ‘G’ : case ‘g’ : System.out.println (“Green”); break; case ‘Y’: case ‘y’ : System.out.println (“Yellow”); case ‘O’ : case ‘o’ : System.out.println (“Orange”); case ‘R’ : case ‘r’ : System.out.println (“Red”); // continued in next slide
More example of switch default : System.out.println (“You are not in rainbow!”); break; }
Nested loops Loop within loop for (i=0; i<=100; i++) { for (j=0; j<=100; j++) { System.out.println (i+j); } Number of loops is called the depth of the nest The innermost loop executes most frequently The outermost loop executes least You can nest while loops within for loops and vice-versa Loop variables should be different for different loops e.g., i and j in this case (what happens if both are i ?)
Nested loops for (i1=p1; i1<q1; i1++) { // outermost statements1 // can be empty for (i2=p2; i2<q2; i2++) { statements2 // can be empty for (i3=p3; i3<q3; i3++) { statements3 // can be empty … for (iN=pN; iN<qN; iN++) { // innermost statementsN } } // How many times does statementsK execute?
All perfect numbers class allPerfectNumbersUptoOneLakh { public static void main (String arg[]) { int n, d, sigma_n; for (n=2; n<=100000; n++) { sigma_n = 1+n; for (d=2; d<=n/2; d++) { if ((n%d)==0) { sigma_n += d; } if (sigma_n == 2*n) { System.out.println (n + “is perfect.”);
Euclid’s division algorithm A fast way to compute the greatest common divisor of two numbers a and b Algorithm (this is not a program) Divide b by a, assign a to b and the remainder to a; loop until a is zero. The value of b at this point is the gcd. Assumes a is less than b Main observation: gcd (a, b) = gcd (a, b-a)
GCD class gcd { public static void main (String arg[]) { int a = 40, b = 24, r, gcd; if ((a==0) || (b==0)) { gcd = (a > b) ? a : b; } else if (a < b) { while (a!=0) { r = b%a; b = a; a = r; gcd = b; // next slide
GCD else { while (b!=0) { r = a%b; a = b; b = r; } gcd = a; System.out.println(“GCD: ” + gcd);
Announcements Mid-term exam I on 31st August 0930 to 1030 Seating arrangement: Y4, Y5, Y7001 to Y7085: L1 OROS Y7092 to Y7233: L2 ERES Y7234 to Y7388: L16 OROS Y7391 to Y7518: L17 ERES Please take seats 15 minutes before the scheduled time Exam syllabus: up to and including conditionals