340 likes | 373 Views
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.
E N D
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 if (condition) { statements } • Nested if if (condition1) { statements1 if (condition2) { statements2 } statements3 }
Nested if • Sometimes possible to simplify nested if if (condition1) { if (condition2) { statements } } • Same as if ((condition1) && (condition2)) { statements }
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; } } }
if-else if (condition) { statements1 } else { statements2 } • if within else if (condition1) { statements1 } else { if (condition2) { statements2 } statements3 }
if-else if-else if-…-else if (condition1) { statements1 } else if (condition2) { statements2 } else if (condition3) { statements3 } … else { statementsn }
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!”); } } }
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;
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 + “.”); } }
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 + “.”); } }
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
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
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
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
Example class justAnExample { public static void main(String arg[]) { int x = 5; int y = 0; while (x < 10) { y--; x++; } System.out.println(y); } }
Example class justAnExample { public static void main(String arg[]) { int x = 15; int y = 0; while (x < 10) { y--; x++; } System.out.println(y); } }
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); } }
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) } }
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)
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
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); } }
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); } } }
do-while do { statements } while (condition); • “statements” execute at least once irrespective of condition
for loops for (expression1; condition; expression2) { statements } • Same as expression1 while (condition) { statements expression2 }
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) } }
Comma operator in for loop for (expression1a, expression2a, …; condition; expression1b, expression2b,…) { statements } • Same as expression1a expression2a … while (condition) { statements expression1b expression2b … }
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) } }
Empty for loop body for (expression1; condition; expression2) { } • Same as for (expression1; condition; expression2);
Infinite loops • Loops that never terminate while (true) { statements } do { statements } while (true);
Infinite loops for (expression1; ;expression2) { statements } for (i=0; i > -10; i++) { statements } for (i=0; i<=100; i--) { statements }
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); } } } } }
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; } }
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); }