Presentation is loading. Please wait.

Presentation is loading. Please wait.

Loops and Logic. Making Decisions Conditional operator Switch Statement Variable scope Loops Assertions.

Similar presentations


Presentation on theme: "Loops and Logic. Making Decisions Conditional operator Switch Statement Variable scope Loops Assertions."— Presentation transcript:

1 Loops and Logic

2 Making Decisions Conditional operator Switch Statement Variable scope Loops Assertions

3 Making Decisions Conditions to be evaluated or tested by the program

4 If Statement If(condition) { statement 1; statement 2;... statement n; } --------------------------------------------------------------------------------------------- int a, b;.. if(a < b) a = 0; If(b<10) b = 0;

5 if...else statement

6 int a, b; //... if(a < b) a = 0; else b = 0; ----------------------------------------------------- boolean dataAvailable; //... if (dataAvailable) ProcessData(); else waitForMoreData();

7 if...else Program import java.lang.Math; public class NumberCheck { public static void main(String[] args) { int number = 0; number = 1+(int)(100*Math.random()); if(number%2 == 0) { // Test if it is even System.out.println(“You have got an even number, “ + number } else { System.out.println(“You have got an odd number, “ + number); }

8 Nested if statements  Nested if is an if statement that is the target of another if or else. if(number%2 == 0) { if(number < 50) { System.out.println(“You have got an even number < 50, “ + number); } else { System.out.println(“You have got an odd number, “ + number); }

9 Nested if statements if(i == 10) { if(j < 20) a = b; if(k > 100) c = d; else a = c; } else a = d;

10 if-else-if Ladder  Sequence of nested ifs is the if-elseif ladder if(condition) statement; else if(condition) statement; else if(condition) statement;. else statement;

11 if-else-if Ladder Program class IfElse { public static void main(String args[]) { int month = 4; // April String season; if(month == 12 || month == 1 || month == 2) season = "Winter"; else if(month == 3 || month == 4 || month == 5) season = "Spring"; else if(month == 6 || month == 7 || month == 8) season = "Summer"; else if(month == 9 || month == 10 || month == 11) season = "Autumn"; else season = "Bogus Month"; System.out.println("April is in the " + season + "."); }

12 if-else-if Ladder Program class IfElseDemo { public static void main(String[] args) { int testscore = 76; char grade; if (testscore >= 90) { grade = 'A'; } else if (testscore >= 80) { grade = 'B'; } else if (testscore >= 70) { grade = 'C'; } else if (testscore >= 60) { grade = 'D'; } else { grade = 'F'; } System.out.println("Grade = " + grade); } Output: Grade = C

13 Programs The aIsBigger() method should return true if the int parameter a is larger than b by 2 or more. Given two int values, a and b, return true if either one is 6. Or if their sum or difference is 6 Input 3 int a, b and c, write a function that calculate the largest value. You are driving a car. Write code to compute the result. If speed is 60 or less, the result is 0. If speed is between 61 and 80 inclusive, the result is 1. If speed is 81 or more, the result is 2. Calculate Odd or even number Given three ints, a b c, return true if it is possible to add two of the ints to get the third. You have a green lottery ticket, with ints a, b, and c on it. If the numbers are all different from each other, the result is 0. If all of the numbers are the same, the result is 20. If two of the numbers are the same, the result is 10.

14 switch Multiway branch statement Provides a better alternative than a large series of if-else-if statements switch (expression) { case value1: // statement sequence break; case value2: // statement sequence break;. case valueN : // statement sequence break; default: // default statement sequence }

15 switch class SampleSwitch { public static void main(String args[]) { Int i=2; switch(i) { case 0: System.out.println("i is zero."); break; case 1: System.out.println("i is one."); break; case 2: System.out.println("i is two."); break; default: System.out.println("i is greater than 2."); }

16 Nested switch switch(count) { case 1: switch(target) { // nested switch case 0: System.out.println("target is zero"); break; case 1: // no conflicts with outer switch System.out.println("target is one"); break; } break; case 2: //...  Switch within the switch statement

17 Loops/ Iteration Iteration statements are for, while, and do-while

18 for Two type : (1) traditional for (11) for-each for for(initialization; condition; iteration) { // body }

19 How Execute for 1 st initialization portion, sets value to control variable. executed only once. 2 nd, condition is evaluated. It is boolean expression. If this expression is true, then the body of the loop is executed. If it is false, the loop terminates. Next, the iteration portion of the loop is executed. It is increments or decrements the loop control variable

20 for loop programs class ForTick { public static void main(String args[]) { for(int n=0; n<10; n++) System.out.println("tick " + n); }

21 for loop programs class FindPrime { public static void main(String args[]) { int num; boolean isPrime; num = 14; if(num < 2) isPrime = false; else isPrime = true; for(int i=2; i <= num/i; i++) { if((num % i) == 0) { isPrime = false; break; } if(isPrime) System.out.println("Prime"); else System.out.println("Not Prime"); }

22 Comma in for loop class Sample { public static void main(String args[]) { int a, b; b = 4; for(a=1; a<b; a++) { System.out.println("a = " + a); System.out.println("b = " + b); b--; } class Comma { public static void main(String args[]) { int a, b; for(a=1, b=4; a<b; a++, b--) { System.out.println("a = " + a); System.out.println("b = " + b); }

23 Infinite for loop infinite loop (a loop that never terminates) for( ; ; ) { //... } OR for( ; ; );

24 For-Each Version for Loop for(type itr-var : collection) statement-block int nums[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; int sum = 0; for(int i=0; i < 10; i++) sum += nums[i]; int nums[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; int sum = 0; for(int x: nums) sum += x;

25 Example Program1 // Use a for-each style for loop. class ForEach { public static void main(String args[]) { int nums[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; int sum = 0; // use for-each style for to display and sum the values for(int x : nums) { System.out.println("Value is: " + x); sum += x; } System.out.println("Summation: " + sum); }

26 Example Program2 // Use break with a for-each style for. class ForEach2 { public static void main(String args[]) { int sum = 0; int nums[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; // use for to display and sum the values for(int x : nums) { System.out.println("Value is: " + x); sum += x; if(x == 5) break; // stop the loop when 5 is obtained } System.out.println("Summation of first 5 elements: " + sum); }

27 Example Program3 // The for-each loop is essentially read-only. class NoChange { public static void main(String args[]) { int nums[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; for(int x: nums) { System.out.print(x + " "); x = x * 10; // no effect on nums } System.out.println(); for(int x : nums) System.out.print(x + " "); System.out.println(); } Output: 1 2 3 4 5 6 7 8 9 10

28 for loop programs

29 while while(condition) { // body of loop } The body of the loop will be executed as long as the conditional expression is true

30 while Program // Demonstrate the while loop. class While { public static void main(String args[]) { int n = 10; while(n > 0) { System.out.println("tick " + n); n--; }

31 do-while Execute the body of a loop at least once, even if the conditional expression is false to begin with. Loop first executes the body of the loop and then evaluates the conditional expression do { // body of loop } while (condition); do { System.out.println("tick " + n); n--; } while(n > 0);

32 Nested Loops one loop may be inside another. // Loops may be nested. class Nested { public static void main(String args[]) { int i, j; for(i=0; i<10; i++) { for(j=i; j<10; j++) System.out.print("."); System.out.println(); } Output:.......................................................

33 Using break using break, you can force immediate termination of a loop, class BreakLoop { public static void main(String args[]) { for(int i=0; i<100; i++) { if(i == 10) break; // terminate loop if i is 10 System.out.println("i: " + i); } System.out.println("Loop complete."); }

34 Using break // Using break to exit a while loop. class BreakLoop2 { public static void main(String args[]) { int i = 0; while(i < 100) { if(i == 10) break; // terminate loop if i is 10 System.out.println("i: " + i); i++; } System.out.println("Loop complete."); }

35 labeled break The general form of the labeled break statement is shown here: break label; label is the name of a label that identifies a block of code.

36 labeled break // Using break as a civilized form of goto. class Break { public static void main(String args[]) { boolean t = true; first: { second: { third: { System.out.println("Before the break."); if(t) break second; // break out of second block System.out.println("This won't execute"); } System.out.println("This won't execute"); } System.out.println("This is after second block."); } Output: Before the break. This is after second block.

37 Using continue Continue running the loop but stop processing the remainder of the code in its body for this particular iteration. // Demonstrate continue. class Continue { public static void main(String args[]) { for(int i=0; i<10; i++) { System.out.print(i + " "); if (i%2 == 0) continue; System.out.println(""); } Output: 0 1 2 3 4 5 6 7 8 9

38 Java Programs


Download ppt "Loops and Logic. Making Decisions Conditional operator Switch Statement Variable scope Loops Assertions."

Similar presentations


Ads by Google