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

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

Dec 17, 2015

Download

Documents

Andrew Simon
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1 More Conditionals Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in.

1

More Conditionals

Instructor: Mainak [email protected]

Page 2: 1 More Conditionals Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in.

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.”); } } } }

Page 3: 1 More Conditionals Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in.

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.”); } } } }

Page 4: 1 More Conditionals Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in.

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.”); }}

Page 5: 1 More Conditionals Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in.

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.”); } }}

Page 6: 1 More Conditionals Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in.

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}

Page 7: 1 More Conditionals Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in.

7

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

Page 8: 1 More Conditionals Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in.

8

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

Page 9: 1 More Conditionals Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in.

9

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

Page 10: 1 More Conditionals Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in.

10

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

Page 11: 1 More Conditionals Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in.

11

switch-case: more flavorsswitch (expression) {

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

Page 12: 1 More Conditionals Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in.

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}

Page 13: 1 More Conditionals Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in.

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

Page 14: 1 More Conditionals Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in.

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

Page 15: 1 More Conditionals Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in.

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

Page 16: 1 More Conditionals Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in.

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

Page 17: 1 More Conditionals Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in.

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

Page 18: 1 More Conditionals Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in.

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;

Page 19: 1 More Conditionals Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in.

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

Page 20: 1 More Conditionals Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in.

20

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

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

Page 21: 1 More Conditionals Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in.

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 ?)

Page 22: 1 More Conditionals Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in.

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?

Page 23: 1 More Conditionals Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in.

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.”); } } }}

Page 24: 1 More Conditionals Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in.

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)

Page 25: 1 More Conditionals Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in.

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

Page 26: 1 More Conditionals Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in.

26

GCD else { while (b!=0) { r = a%b; a = b; b = r; } gcd = a; } System.out.println(“GCD: ” + gcd); }}