Download presentation
Presentation is loading. Please wait.
1
Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in
More Conditionals Instructor: Mainak Chaudhuri
2
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.”); } } }
3
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.”); } } }
4
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.”);
5
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.”);
6
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 }
7
switch-case Same as if (expression==constant1) { statements1
… statementsN statementsD } else if (expression==constant2) { statements3 // continued on next slide
8
switch-case else if (expression==constant3) { statements3 statements4
… statementsN statementsD } else if (expression==constantN) { else {
9
switch-case with break
switch (expression) { case constant1: statements1 break; case constant2: statements2 … case constantN: statementsN default: statementsD }
10
switch-case with break
Same as if (expression==constant1) { statements1 } else if (expression==constant2) { statements2 … else if (expression==constantN) { statementsN else { statementsD
11
switch-case: more flavors
switch (expression) { case constant1: case constant2: statements1 break; case constant3: statements3 … case constantN: statementsN default: statementsD }
12
switch-case: more flavors
Same as if ((expression==constant1) || (expression==constant2)) { statements1 } else if (expression==constant3) { statements3 … else if (expression==constantN) { statementsN else { statementsD
13
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
14
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
15
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
16
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
17
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
18
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”);
19
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
20
More example of switch default :
System.out.println (“You are not in rainbow!”); break; }
21
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 ?)
22
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?
23
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.”);
24
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)
25
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
26
GCD else { while (b!=0) { r = a%b; a = b; b = r; } gcd = a;
System.out.println(“GCD: ” + gcd);
27
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.