1 / 34

Conditionals

Conditionals. Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in. Announcements. B1 batch will go for tutorial today In CS102 (Computer Science department ground floor) After you enter, take right Ask people, if you are lost 0900-1000 (right after this class). if statement.

roseannew
Télécharger la présentation

Conditionals

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Announcements • B1 batch will go for tutorial today • In CS102 (Computer Science department ground floor) • After you enter, take right • Ask people, if you are lost • 0900-1000 (right after this class)

  3. if statement if (condition) { statements } • Nested if if (condition1) { statements1 if (condition2) { statements2 } statements3 }

  4. Nested if • Sometimes possible to simplify nested if if (condition1) { if (condition2) { statements } } • Same as if ((condition1) && (condition2)) { statements }

  5. Example class exampleIf { public static void main(String arg[]) { int x=10, y, z; if ((x%2)==0) { System.out.println(x + “ is even.”); if ((x%3)==0) { System.out.println(x + “ is a multiple of 6.”); y = x/6; } z = x%6; } } }

  6. if-else if (condition) { statements1 } else { statements2 } • if within else if (condition1) { statements1 } else { if (condition2) { statements2 } statements3 }

  7. if-else if-else if-…-else if (condition1) { statements1 } else if (condition2) { statements2 } else if (condition3) { statements3 } … else { statementsn }

  8. Example class greetings { public static void main(String arg[]) { int hour = 3; if ((hour >= 0) && (hour < 12)) { System.out.println(“Good Morning!”); } else if ((hour >= 12) && (hour < 18)) { System.out.println(“Good Afternoon!”); } else if ((hour >=18) && (hour < 24)) { System.out.println(“Good Evening!”); } else { System.out.println(“Bad time!”); } } }

  9. Conditional assignment if ((x%2)==0) { y = x/2; } else { y = (x+1)/2; } • Same as y = ((x%2)==0) ? x/2 : (x+1)/2;

  10. Integer part and absolute value class integerPart { public static void main(String arg[]) { double x = -3.7; int ipart; double aval; ipart = ((x >= 0) || ((int)x==x)) ? (int)x : (int)(x-1); aval = (x >= 0) ? x : -x; System.out.println(“Integer part of ” + x + “ is ” + ipart + “. Absolute value is ” + aval + “.”); } }

  11. Integer part and absolute value class integerPartAlternate { public static void main(String arg[]) { double x = -3.7; int ipart; double aval; ipart = ((x < 0) && ((int)x!=x)) ? (int)(x-1) : (int)x; aval = (x < 0) ? -x : x; System.out.println(“Integer part of ” + x + “ is ” + ipart + “. Absolute value is ” + aval + “.”); } }

  12. Sorting three numbers class sortThree { public static void main(String arg[]) { int x = 2, y = 5, z = 1; int max, mid, min; if ((x > y) && (x > z)) { max = x; if (y > z) { mid = y; min = z; } else { mid = z; min = y; } } // next slide

  13. Sorting three numbers else { if (y > z) { max = y; if (x > z) { mid = x; min = z; } else { mid = z; min = x; } } else { // the remaining two permutations} } // end else } // end main } // end class

  14. Loops • Needed in problems that require solving the same subproblem over and over • Computing the sum of the first 100 natural numbers and putting the result in y • Algorithm: y = 1; y = y + 2; y = y + 3; … y = y + 100; • Cannot write 99 such additions: use a loop

  15. while while (condition) { statements } • Can put anything in “statements” • The entire construct is called a while loop • statements are executed until condition is true • Even before executing it the first time condition is evaluated • A while loop may not execute even once

  16. Example class justAnExample { public static void main(String arg[]) { int x = 5; int y = 0; while (x < 10) { y--; x++; } System.out.println(y); } }

  17. Example class justAnExample { public static void main(String arg[]) { int x = 15; int y = 0; while (x < 10) { y--; x++; } System.out.println(y); } }

  18. Sum of natural numbers class naturalSum { public static void main(String arg[]) { int n = 2; int y = 1; while (n <= 100) { y += n; n++; } System.out.println(“Sum of the first ” + (n-1) + “ natural numbers is ” + y); } }

  19. Sum of natural numbers class naturalSumAnotherWay { public static void main(String arg[]) { int n = 99; int m = n+1; int y = 100; while (n > 0) { y += n; n--; } System.out.println(“Sum of the first ” + m + “ natural numbers is ” + y) } }

  20. Announcements • There is a class this Saturday 0800-0900, in L7 • Batches B9 and B10 will have labs on Saturday 1000-1300, in computer center • Midterm-I is on 5th September, 0930 to 1030, L1, L2, L3, L4, L5, L8, L9 • Odd row odd seats (OROS)

  21. Announcements • Seating arrangement (OROS) • L1: 98292, Y2, Y3, Y4, Y5, up to Y6096 • L2: Y6098 to Y6217 • L3: Y6218 to Y6285 • L4: Y6286 to Y6365 • L5: Y6368 to Y6448 • L8: Y6452 to Y6508 • L9: Y6510 to Y6552 • After arriving at your examination hall, if you fail to find an empty seat, please proceed to L9

  22. Integer index class integerIndex { public static void main(String arg[]) { int n = 3; double x = 3.14, y = 1.0; int m = n; if (n < 0) { x = 1/x; m = -n; } while (m > 0) { y *= x; m--; } System.out.println(x + “ to the power ” + n + “ is ” + y); } }

  23. Positive Decimal to Binary class positiveDecimalToBinary { public static void main(String arg[]) { int n = 34, y=0, polyTerm = 1; if (n < 0) { System.out.println(“Sorry, cannot handle negative integers today!”); } else { while (n > 0) { y += (polyTerm*(n%2)); n /= 2; polyTerm *= 10; } System.out.println(“Required binary: ” + y); } } }

  24. do-while do { statements } while (condition); • “statements” execute at least once irrespective of condition

  25. for loops for (expression1; condition; expression2) { statements } • Same as expression1 while (condition) { statements expression2 }

  26. Sum of natural numbers class naturalSum { public static void main(String arg[]) { int n; int y = 1; for (n=2; n <=100; n++) { y += n; } System.out.println(“Sum of the first ” + (n-1) + “ natural numbers is ” + y) } }

  27. Comma operator in for loop for (expression1a, expression2a, …; condition; expression1b, expression2b,…) { statements } • Same as expression1a expression2a … while (condition) { statements expression1b expression2b … }

  28. Sum of natural numbers class naturalSum { public static void main(String arg[]) { int n; int y; for (n=2, y=1; n <=100; y += n, n++) { } System.out.println(“Sum of the first ” + (n-1) + “ natural numbers is ” + y) } }

  29. Empty for loop body for (expression1; condition; expression2) { } • Same as for (expression1; condition; expression2);

  30. Infinite loops • Loops that never terminate while (true) { statements } do { statements } while (true);

  31. Infinite loops for (expression1; ;expression2) { statements } for (i=0; i > -10; i++) { statements } for (i=0; i<=100; i--) { statements }

  32. Perfect squares class identifySquareButLessClever { public static void main (String arg[]) { int n = 48; int i; if (n < 0) { System.out.println (n + “ is not a perfect square.”); } else if ((n==0) || (n==1)) { System.out.println (n + “ is a perfect square.”); } else { for (i=2; i<=n/2; i++) { if ((i*i) == n) { System.out.println(n + “ is square of ” + i); } } } } }

  33. break • In the last example you may want to come out of the for loop as soon as you discover that n is a square • The computation done after this is useless • Use break • Breaks out of the loop (while, do-while, or for) currently you are in for (i=2; i<=n/2; i++) { if ((i*i)==n) { System.out.println(n + “ is square of ” + i); break; } }

  34. break • Another way to achieve the same effect without using break for (i=2; (i<=n/2) && ((i*i) != n); i++) ; if ((i*i)==n) { System.out.println(n + “ is square of ” + i); }

More Related