Question 2

  1. Rewrite the following if else if construct into switch case

              if( var==1)

                      System.out.println(“good”);

              else if(var==2)

                      System.out.println(“better”);

              else if(var==3)

                      System.out.println(“best”);

              else

                      System.out.println(“invalid”);

2. Rewrite the following loop using for loop:

              while (true)

              System.out.print(“*”);

3.  Rewrite the following using ternary operator:

               if (bill >10000)

                 discount = bill * 10.0/100;

              else

                 discount = bill * 5.0/100;

a)
        var=3;
        switch (var)
        {
            case 1:
            System.out.println("good");
            break;
            case 2:
            System.out.println("better");
            break;
            case 3:
            System.out.println("best");
            break;
            default:
            System.out.println("invalid");
        }

b)
for(;true;)   // for( ; ; )
       System.out.print("*"); 
c)

discount = (bill>10000) ? bill * 10.0/100 : bill * 5.0/100;

 discount = bill* ((bill>10000) ? 10.0 :5.0)/100;
This entry was posted in Term 1. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *