Download presentation
Presentation is loading. Please wait.
1
1 Conditionals Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in
2
2 if statement if (condition) { statements } Nested if if (condition1) { statements1 if (condition2) { statements2 } statements3 }
3
3 Nested if Sometimes possible to simplify nested if if (condition1) { if (condition2) { statements } Same as if ((condition1) && (condition2)) { statements }
4
4 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; }
5
5 if-else if (condition) { statements1 } else { statements2 } if within else if (condition1) { statements1 } else { if (condition2) { statements2 } statements3 }
6
6 if-else if-else if-…-else if (condition1) { statements1 } else if (condition2) { statements2 } else if (condition3) { statements3 } … else { statementsn }
7
7 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!”); }
8
8 Announcements Wednesday is a holy day –No class –No labs
9
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
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
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
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
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
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
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
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
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
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
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
20 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); }
21
21 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); }
22
22 do-while do { statements } while (condition); “statements” execute at least once irrespective of condition
23
23 for loops for (expression1; condition; expression2) { statements } Same as expression1 while (condition) { statements expression2 }
24
24 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); }
25
25 Comma operator in for loop for (expression1a, expression2a, …; condition; expression1b, expression2b,…) { statements } Same as expression1a expression2a … while (condition) { statements expression1b expression2b … }
26
26 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); }
27
27 Empty for loop body for (expression1; condition; expression2) { } Same as for (expression1; condition; expression2);
28
28 Infinite loops Loops that never terminate while (true) { statements } do { statements } while (true);
29
29 Infinite loops for (expression1; ;expression2) { statements } for (i=0; i > -10; i++) { statements } for (i=0; i<=100; i--) { statements }
30
30 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); }
31
31 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; }
32
32 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); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.