PowerPoint Presentation - Department of Computer Science and by wuyunyi

VIEWS: 3 PAGES: 39

									Computer Science and Elements of Programming


               Java Lecture 5

       Dr Giovanna Di Marzo Serugendo

           Department of Computer Science
              and Information Systems
        Birkbeck College, University of London


            Email: dimarzo@dcs.bbk.ac.uk
     Web Page: http://www.dcs.bbk.ac.uk/~dimarzo
          Java Lecture 4: Overview
•   Java Programs: the basics
•   Structure of a Java program
•   Constructors
•   Methods
     – Input parameters
     – Return values
     – Main method
• Objects
Java Lectures – Spring 2006 – Giovanna Di Marzo Serugendo   2
          Java Lecture 5: Overview
• Control statements
• Selection (choices) statements
     – if
     – if – else
     – switch
• Nested if



Java Lectures – Spring 2006 – Giovanna Di Marzo Serugendo   3
                  Control Statements
• Instruction flow
     – Sequential               public static void main(String[] args)
                                  {
                                     // creates an object of class HelloWorld
                                     HelloWorld h = new HelloWorld();
                                     // activates a method
                                     h.printHelloWorld();
                                  }




• Control statement
     – Disruption to the sequential flow
Java Lectures – Spring 2006 – Giovanna Di Marzo Serugendo                       4
                  Control Statements
• Selection statements (choices)
     – if, if-else, switch
• Iteration statements (loops)
     – while, do-while, for (Lecture 6)


• Transfer statements
     – break, continue, return,
       try-catch-finally

Java Lectures – Spring 2006 – Giovanna Di Marzo Serugendo   5
                Selection Statements
• Choice between different actions during execution
• Choice is based on a criteria specified during the
  selection statement

     – criteria : boolean condition (if, if-else)
          • Choice:
               – “if it rains I will go shopping” (if)
               – “if it rains I will go shopping, otherwise I will go to the park” (if-else)

     – criteria : a specific value among a set of values (switch)
          • Example:
               –   On “Monday”, I do my shopping
               –   On “Tuesday”, I work
               –   On “Wednesday”, I go to the library
               –   On “Thursday”, I go to the swimming pool
               –   Etc.

Java Lectures – Spring 2006 – Giovanna Di Marzo Serugendo                                      6
                Simple if Statement
     if ( <conditional expression> )
          <statement>


     if ( <conditional expression> ){
          <statement_1>
          <statement_2>
              …
          <statement_n>
     }


Java Lectures – Spring 2006 – Giovanna Di Marzo Serugendo   7
                 Simple if Statement
      if ( <conditional expression> )
           <statement>
                       Condition must evaluate to true
public void aSimpleCondition(double price, double money){

          if (money >= price)
                System.out.println("Payment accepted");

  }
                                                             End of if statement

 Java Lectures – Spring 2006 – Giovanna Di Marzo Serugendo                         8
                Simple if Statement




Java Lectures – Spring 2006 – Giovanna Di Marzo Serugendo   9
                Simple if Statement
     if ( <conditional expression> ){
          <statement_1>
                 <statement_2>
                     …
                 <statement_n>
     }




Java Lectures – Spring 2006 – Giovanna Di Marzo Serugendo   10
                 Simple if Statement

public void aSimpleCondition2(double price, double money){

       double change;

       if (money >= price) {
           System.out.println("Payment accepted");
           change = money - price;
           System.out.println("Your change is " + change);
       }
   }
        End of if statement
 Java Lectures – Spring 2006 – Giovanna Di Marzo Serugendo   11
                Simple if Statement




Java Lectures – Spring 2006 – Giovanna Di Marzo Serugendo   12
                 Simple if Statement
      On the importance of { } for if statement

public void aSimpleCondition3(double price, double money){

      double change;

      if (money >= price)
          System.out.println("Payment accepted");
          change = money - price;
          System.out.println("Your change is " + change);

  }
 Java Lectures – Spring 2006 – Giovanna Di Marzo Serugendo   13
                Simple if Statement




Java Lectures – Spring 2006 – Giovanna Di Marzo Serugendo   14
                 Simple if Statement
      On the importance of { } for if statement
public void aSimpleCondition3(double price, double money){

      double change;                                         End of if statement


      if (money >= price)
          System.out.println("Payment accepted");

      change = money - price;
      System.out.println("Your change is " + change);

  }
 Java Lectures – Spring 2006 – Giovanna Di Marzo Serugendo                         15
                 if-else Statement

          if ( <conditional expression> )
              <statement_1>
          else
                      <statement_2>




Java Lectures – Spring 2006 – Giovanna Di Marzo Serugendo   16
                 if-else Statement

             if ( <conditional expression> ){
               <statement_1>
                …
               <statement_n>
             } else {
               <statement_o>
                 …
               <statement_z>
             }

Java Lectures – Spring 2006 – Giovanna Di Marzo Serugendo   17
                  if-else Statement
                   Condition evaluates to true
public void aSimpleConditionWithElse(double price,
                                         double money){
     if (money >= price)
         System.out.println("Payment accepted");
     else
         System.out.println("Payment not accepted");

 }
     Condition evaluates to false




 Java Lectures – Spring 2006 – Giovanna Di Marzo Serugendo   18
                 if-else Statement




Java Lectures – Spring 2006 – Giovanna Di Marzo Serugendo   19
                  if-else Statement
public void aSimpleConditionwithElse2(double price, double money){
    if (money >= price) {
        double change;
        System.out.println("Payment accepted");
        change = money - price;
        System.out.println("Your change is " + change);
    }
    else {
        double missingMoney;
        missingMoney = price - money;
        System.out.println("Payment not accepted");
        System.out.println("Please complete with " + missingMoney);
     }
  }


 Java Lectures – Spring 2006 – Giovanna Di Marzo Serugendo            20
                 if-else Statement




Java Lectures – Spring 2006 – Giovanna Di Marzo Serugendo   21
 Nested if / if-else Statements
public void nesteIfA(int x, int y){
     if ( x > 0 ) {
             if (y > 0)
                System.out.println("Both x and y are positive");
          }
          else
             System.out.println("x is negative, y may be positive or negative");

  }
                                                             End of second if statement




 Java Lectures – Spring 2006 – Giovanna Di Marzo Serugendo                           22
Nested if / if-else Statements




Java Lectures – Spring 2006 – Giovanna Di Marzo Serugendo   23
Nested if / if-else Statements




Java Lectures – Spring 2006 – Giovanna Di Marzo Serugendo   24
  Nested if / if-else Statements

public void nesteIfC(int x, int y){
     if ( x > 0 )
             if (y > 0)
                System.out.println("Both x and y are positive");
     else
          System.out.println("x is negative, y may be positive or negative");

  }


      Be careful to the use of { }

      Changes the tested values
  Java Lectures – Spring 2006 – Giovanna Di Marzo Serugendo                 25
Nested if / if-else Statements




                                                            This is wrong
Java Lectures – Spring 2006 – Giovanna Di Marzo Serugendo               26
Nested if / if-else Statements




Java Lectures – Spring 2006 – Giovanna Di Marzo Serugendo   27
Nested if / if-else Statements

 public void nesteIfB(int x, int y){
      if ( x > 0 )
              if (y > 0)
                 System.out.println("Both x and y are positive");
              else
                  System.out.println("x is positive and y is negative");

     }


   Be careful to the use of { }

   Changes the tested values
Java Lectures – Spring 2006 – Giovanna Di Marzo Serugendo                  28
Nested if / if-else Statements




Java Lectures – Spring 2006 – Giovanna Di Marzo Serugendo   29
Nested if / if-else Statements




Java Lectures – Spring 2006 – Giovanna Di Marzo Serugendo   30
 Nested if / if-else Statements
public void nestedIf2(int testscore) {
     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);
  }
Java Lectures – Spring 2006 – Giovanna Di Marzo Serugendo   31
Nested if / if-else Statements




   Order of test is important:

   72 > 60 but grade is not D
   Stops as soon as condition is true
Java Lectures – Spring 2006 – Giovanna Di Marzo Serugendo   32
                   switch Statement
switch ( <integral expression> ) {
     case label_1:
           <statement_1>                         Evaluated First
     case label_2:
                                           Value of integral expr
             <statement_2>        compared with first case label
     …
                                            Value of integral expr
     case label_n:             compared with second case label
           <statement_n>
     defaults:
          <statement>
   }                       Continues until the end unless “break”

Java Lectures – Spring 2006 – Giovanna Di Marzo Serugendo        33
                   switch Statement
switch ( <integral expression> ) {
     case label_1:
           <statement_1>                                    char
     case label_2:                                          byte
                                                            short
             <statement_2>                                  int
     …                                                      (enumerated type)
     case label_n:
           <statement_n>
     defaults:
          <statement>
   }
Java Lectures – Spring 2006 – Giovanna Di Marzo Serugendo                   34
                   switch Statement
  public void switchExample(char digit) {

         switch (digit) {
           case '1': System.out.println("First "); break;
           case '2': System.out.println("Second ");break;
           case '3': System.out.println("Third ");break;
           case '4': System.out.println("Fourth ");break;
           case '5': System.out.println("Fifth ");break;
           default: System.out.println("Other ");
         }
     }




Java Lectures – Spring 2006 – Giovanna Di Marzo Serugendo   35
                   switch Statement




Java Lectures – Spring 2006 – Giovanna Di Marzo Serugendo   36
                   switch Statement




Java Lectures – Spring 2006 – Giovanna Di Marzo Serugendo   37
                   switch Statement
  public void switchExample2(int digit) {

         switch (digit) {
           case 1: System.out.println("First ");
           case 2: System.out.println("Second ");
           case 3: System.out.println("Third ");
           case 4: System.out.println("Fourth ");
           case 5: System.out.println("Fifth ");
           default: System.out.println("Other ");
         }
   }


    If no break, applies all statement after condition is satisfied

Java Lectures – Spring 2006 – Giovanna Di Marzo Serugendo             38
                   switch Statement




Java Lectures – Spring 2006 – Giovanna Di Marzo Serugendo   39

								
To top