what is the logic

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ramya kandasamy
    New Member
    • Apr 2010
    • 9

    what is the logic

    Code:
    public static void main(String[ ] args) {
          int x = 5;
          while (x > 1) { 
              x = x + 1;
              if (x < 3) {
                   System.out.println("small x");
              }
          }
     }

    the output of the program is smallx but how?
    Last edited by Dormilich; Apr 28 '10, 02:05 PM. Reason: Please use [code] tags when posting code
  • Dheeraj Joshi
    Recognized Expert Top Contributor
    • Jul 2009
    • 1129

    #2
    I will suggest you to put the print statement for x inside the if loop

    Code:
    public static void main(String[ ] args) {
    int x = 5;
    while (x > 1) { 
    x = x + 1;
    if (x < 3) {
    System.out.println(x);
    System.out.println("small x");
    }
    }
    }
    This will print
    Code:
    -2147483648
    small x
    While loop will be running continuously and if condition will be failing till x becomes -2147483648. when x reaches -2147483648, it prints small x and comes out of the loop.

    Regards
    Dheeraj Joshi

    Comment

    • ramya kandasamy
      New Member
      • Apr 2010
      • 9

      #3
      thanks a lot

      Comment

      Working...