Is the answer 35, 35?

int a = 5; int b = 7; int num = 35; while (a < b) { System.out.println(?My age is ? + num); a = a + 1; }

View Answers

December 23, 2010 at 1:08 PM

Hello Friend,

class Check{
    public static void main(String[] args) 
    {
        int a = 5;
        int b = 7;
        int num = 35;
        while (a < b) { 
        System.out.println("?My age is ? "+ num);
        a = a + 1;
        }
    }
}

The above program outputs: My age is ? " 35 My age is ? " 35

As you have created a condition that a should be less than b.And you have specified a=5 and b=7.So firstly it takes the value of a 5 and check the condition and returns 35. Then it checks the incremented value i.e a+1, it is also less than 7 so it returns 35. Then again it checks but this time value will be 8 which is larger than b i.e. 7 so the loop ends here.

Thanks

Thanks









Related Tutorials/Questions & Answers:
Advertisements