1 More Conditionals Instructor: Mainak Chaudhuri [email protected].

26
1 More Conditionals Instructor: Mainak Chaudhuri [email protected]

Transcript of 1 More Conditionals Instructor: Mainak Chaudhuri [email protected].

1

More Conditionals

Instructor: Mainak [email protected]

2

Primality testingclass 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) { System.out.println(n + “ is not a

prime.”); break; } } if (d > n/2) { System.out.println(n + “ is a prime.”); } } } }

3

Primality testingclass 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) { System.out.println(n + “ is not a

prime.”); 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 numbersclass 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-…-elseswitch (expression) { case constant1: // integer or character statements1 case constant2: statements2 … case constantN: statementsN default: statementsD}

7

switch-case• Same asif (expression==constant1) { statements1 statements2 … statementsN statementsD}else if (expression==constant2) { statements2 statements3 … statementsN statementsD}// continued on next slide

8

switch-caseelse if (expression==constant3) { statements3 statements4 … statementsN statementsD}…else if (expression==constantN) { statementsN statementsD}else { statementsD}

9

switch-case with breakswitch (expression) { case constant1: statements1 break; case constant2: statements2 break; … case constantN: statementsN break; default: statementsD break;}

10

switch-case with break• Same asif (expression==constant1) { statements1}else if (expression==constant2) { statements2}…else if (expression==constantN) { statementsN}else { statementsD}

11

switch-case: more flavorsswitch (expression) {

case constant1: case constant2: statements1 break; case constant3: statements3 break; … case constantN: statementsN break; default: statementsD break;}

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 numbersclass 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!”); break; case 2 : System.out.println(“Smallest prime!”); break; // 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!”); break; case 5 : System.out.println(“Number of fingers!”); break; case 6 : System.out.println(“Smallest perfect!”); break; // 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!”); break; case 9 : System.out.println(“Smallest odd prime

squared!”); break; default : System.out.println(“Not a digit!”); break; } // end of switch } // end of main} // end of class

16

More classificationclass 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!”); break; // continued in next slide

17

More classification case 6: System.out.println(“Perfect!”); break; case 8: System.out.println(“Cube!”); break; case 0: System.out.println(“Zero the Great!”); break; default: System.out.println(“Not a digit!”); break; } // end switch } // end main} // end class

18

More example of switchclass 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”); break; case ‘B’ : case ‘b’ : System.out.println (“Blue”); break;

19

More example of switch case ‘G’ : case ‘g’ : System.out.println (“Green”); break; case ‘Y’: case ‘y’ : System.out.println (“Yellow”); break; case ‘O’ : case ‘o’ : System.out.println (“Orange”); break; case ‘R’ : case ‘r’ : System.out.println (“Red”); break; // continued in next slide

20

More example of switch default : System.out.println (“You are

not in rainbow!”); break; } }}

21

Nested loops• Loop within loopfor (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 loopsfor (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 numbersclass 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

GCDclass 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); }}