Presentation is loading. Please wait.

Presentation is loading. Please wait.

Conditionals with Strings The comparison operators we’ve seen so far (==, !=, >=, > etc.) all apply to numbers ( ints floats doubles etc.) and return either.

Similar presentations


Presentation on theme: "Conditionals with Strings The comparison operators we’ve seen so far (==, !=, >=, > etc.) all apply to numbers ( ints floats doubles etc.) and return either."— Presentation transcript:

1 Conditionals with Strings The comparison operators we’ve seen so far (==, !=, >=, > etc.) all apply to numbers ( ints floats doubles etc.) and return either true or false String myName = “fintan”; String userName; userName = JOptionPane.showInputDialog(null,” enter name :”); if (userName.equals(myName) ) { JOptionPane.showMessageDialog(null,” Hello, O great creator !”); } else { JOptionPane.showMessageDialog(null,” Hello, “+userName); } ints floats doubles are all primitive types: this is why we compare them with these operators. To compare reference types like Strings we use methods. The method.equals() belongs to a String ( userName, above) and takes another String as its argument. It returns true if both Strings contain the same chars in the same order, and false otherwise.

2 The boolean data type A variable of type int can have 2 32 different values A variable of type boolean can have 2 different values: boolean b = true;....... b = false; We can put true or false values directly into boolean variables, or we can put the results of comparisons (which turn into true or false ) into boolean variables: int x = 10; int y = 20; boolean match = (x == y);

3 More conditionals with Strings We can use myName.equals(userName) in an if boolean expression Or we could use userName.equals(myName) if we liked Each uses the.equals method in a String to compare it to another string, and return true or false. We can use this.equals() method with any other reference type (we’ll see others later on). We use == only for primitive types like numbers. We can use other methods inside a boolean expression too, if we want: if (userName.length() > 50) { JOptionPane.showMessageDialog(null,” Your name is too long !”); } else { JOptionPane.showMessageDialog(null,” Hello, “+userName); }

4 Assume the following: // two boolean variables with // initial values boolean honest = true; boolean happy = false;

5 Nested if-statements if (honest){ if (happy){ System.out.print("Poor man"); } else { System.out.print("Beggarman"); } } else { if (happy) { System.out.print("Rich man"); } else { System.out.print("Thief"); }

6 One-liners: no braces necessary This is permissible: if (input > 0) System.out.println("input is ok"); else System.out.println("input is not ok"); But what if we add a line to either the 'true' set or the 'false' set? Recommend: always use braces, even for one-liners. (A single exception exists: multiple nested alternatives)

7 Multiple alternatives if (score >= 85.0) { grade = 'A'; } else { if (score >= 70.0) { grade = 'B'; } else { if (score >= 55.0) { grade = 'C'; } else { if (score >= 40.0) { grade = 'D'; } else {......................etc

8 Multiple alternatives: better if (score >= 85.0) grade = 'A'; else if (score >= 70.0) grade = 'B'; else if (score >= 55.0) grade = 'C'; else if (score >= 40.0) grade = 'D'; else grade = 'F';

9 Computing leap year Write a program to determine whether a given year is leap. A year is leap if: it is divisible by 4.......................................2004 is leap but if divisible by 100, it is not leap...........2100 is not leap unless it is also divisible by 400.................2000 was leap

10 Switch statement I // we can do this..... if (numOfYears == 7) { annualInterestRate = 7.25; } else if (numOfYears == 15) { annualInterestRate = 8.50 } else if (numOfYears == 30) { annualInterestRate = 9.0; } else { System.out.println(“....”); }

11 Switch statement II // or we can do this.... switch (numOfYears) { case 7: annualInterestRate = 7.25; break; case 15: annualInterestRate = 8.50; break; case 30: annualInterestRate = 9.0; break; default: System.out.println(“...”); }

12 Switch statement III Rules for switch statement: 1. choice among alternatives depends on the value in parentheses—the switch-expression. 2. The switch-expression must evaluate to an integer type (char, int, byte, short) 3. The cases are also indexed by an integer type 4. If the break statement is absent, execution continues with the next case statement 5. The default case is optional, and is executed if no case matches the switch-expression. 6. Use of this construct is entirely optional

13 Homework You should be reading through the textbook a group of pages every week, following what I’m doing the the lectures. You shouldn’t need me to tell you this! You should also be doing exercises from the chapter! Pick exercises where you have to write a program. Then you can check if you got the exercise right by seeing if the program operates as required.


Download ppt "Conditionals with Strings The comparison operators we’ve seen so far (==, !=, >=, > etc.) all apply to numbers ( ints floats doubles etc.) and return either."

Similar presentations


Ads by Google