#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

    For-Switch program ->output? (easy) (solved)


    Im starting with java at the university but im having several problems, one of them is this example. We have to answer which would be the output the program will give us if we execute it. It is supposed to be -4 but i cant understand why.
    Thank you very much, here it is:
    (Sorry, i dont know why when i send the message it doesnt keep the tab, excuse me for giving a short of unreadable code)


    PROBLEM SOLVED! now i know how to handle with those missing breaks, thank you :3

    Code:
    1 Code:
    class Ejercicio { 
                public static void main (String [ ] args){ 
          int j=1; 
          for (int i=0; i<5; i++) { 
        switch (i) { 
          case 0: 
          case 2: 
        j=j+i; 
        break; 
          case 1:  
        j=j*i; 
          case 3:  
             j=j-i; 
             break; 
          default: 
        j=i/j; 
         } 
      } 
      System.out.println (j); 
       } 
     }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    57
    Rep Power
    12
    Hi,

    note that when a case of a switch statement is executed, all following cases to the next "break" are also executed (which is a typical error source, by the way).

    So, for example, in the second iteration of the loop (i == 1), both j = j*i and j = j - i are executed, because there's no "break" directly after "case 0".

    The rest you should be able to figure out yourself. Just go through each iteration and write down the result.
  4. #3
  5. Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    May 2004
    Location
    Superior, CO, USA
    Posts
    2,418
    Rep Power
    1713
    Just walk through it (and post with [ code ] or [ highlight ] tags next time):

    java Code:
    class Ejercicio {
        public static void main (String [ ] args){
            int j=1;
            for (int i=0; i<5; i++) {
                switch (i) {
                    case 0:
                    case 2:
                        j=j+i;
                       break;
                    case 1:
                        j=j*i;
                    case 3:
                        j=j-i;
                        break;
                    default:
                       j=i/j;
                }
            }
        System.out.println (j);
        }
    }


    There is a trick in the code - after "case 1:" there is no break statement. Therefore the code falls through and executes the code under "case 3" and the result would be the combination of "j=j*i" (essentially worthless) and "j=j-i". Does that help to explain the answer?
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0
    Thank you very much!, im having an exam this monday so im trying some examples the university gave us. All of them are kind of tricky haha, i think im having a break because ive got such a headache u.u

IMN logo majestic logo threadwatch logo seochat tools logo