Upload
kathryn-heath
View
226
Download
1
Embed Size (px)
Citation preview
2
if statementif (condition) { statements}• Nested ifif (condition1) { statements1 if (condition2) { statements2 } statements3}
3
Nested if• Sometimes possible to simplify nested ifif (condition1) { if (condition2) { statements }}• Same asif ((condition1) && (condition2)) { statements}
4
Exampleclass 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
if-elseif (condition) { statements1}else { statements2}• if within elseif (condition1) { statements1 }else { if (condition2) { statements2 } statements3}
6
if-else if-else if-…-elseif (condition1) { statements1}else if (condition2) { statements2}else if (condition3) { statements3}…else { statementsn}
7
Exampleclass 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 assignmentif ((x%2)==0) { y = x/2;}else { y = (x+1)/2;}• Same asy = ((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 numbersclass 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
whilewhile (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
Exampleclass justAnExample { public static void main(String arg[]) { int x = 5; int y = 0; while (x < 10) { y--; x++; } System.out.println(y); }}
17
Exampleclass 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 numbersclass 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 numbersclass 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
Integer indexclass 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
Positive Decimal to Binaryclass 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
do-whiledo { statements} while (condition);• “statements” execute at least once
irrespective of condition
23
for loopsfor (expression1; condition; expression2)
{ statements}• Same asexpression1while (condition) { statements expression2}
24
Sum of natural numbersclass 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
Comma operator in for loopfor (expression1a, expression2a, …;
condition; expression1b, expression2b,…) { statements }• Same asexpression1aexpression2a …while (condition) { statements expression1b expression2b …}
26
Sum of natural numbersclass 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
Empty for loop bodyfor (expression1; condition;
expression2) {}• Same asfor (expression1; condition;
expression2);
28
Infinite loops• Loops that never terminatewhile (true) { statements}
do { statements} while (true);
29
Infinite loopsfor (expression1; ;expression2) { statements}
for (i=0; i > -10; i++) { statements}
for (i=0; i<=100; i--) { statements}
30
Perfect squaresclass 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
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; }}