What's wrong with this code ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • devilinthebox
    New Member
    • Feb 2008
    • 6

    What's wrong with this code ?

    It has to do this

    "10...9...8...7 ...6...5...4... 3...2...1...0 BLAST OFF!"

    Code:
    c.println ("Count Down");
    int num = 10;
    while (num<10)
    {
    c.print (num + " . . .");
    num = num - 1;
    }
    c.println ("BLAST OFF!");
    It just says
    " Count Down
    BLAST OFF!"
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by devilinthebox
    It has to do this

    "10...9...8...7 ...6...5...4... 3...2...1...0 BLAST OFF!"

    Code:
    c.println ("Count Down");
    int num = 10;
    while (num<10)
    {
    c.print (num + " . . .");
    num = num - 1;
    }
    c.println ("BLAST OFF!");
    It just says
    " Count Down
    BLAST OFF!"
    Here's a small question to you: if 'num' equals 10 is it smaller than 10?

    kind regards,

    Jos

    Comment

    • devilinthebox
      New Member
      • Feb 2008
      • 6

      #3
      No it's not.... I still don't understand what's wrong with the code though.

      Comment

      • Laharl
        Recognized Expert Contributor
        • Sep 2007
        • 849

        #4
        Because 10 is not less than 10, you never enter the loop. Also, note that if you change it to <=10, you're going to get an infinite loop.

        Comment

        • jimhawkss
          New Member
          • Feb 2008
          • 10

          #5
          Just in case:
          What is being said is change your statment to

          while (num >= 0)

          That way it will Count down to 0

          The way you have it now basically your statement says;

          While (10 < 10)
          {
          do the stuff in here.
          }

          Trust me I've done this once or twice myself.... but never a third time tankfully.

          Comment

          • Nepomuk
            Recognized Expert Specialist
            • Aug 2007
            • 3111

            #6
            By the way, there's not only one kind of loop - try something similar to this in future:
            [CODE=java]
            for(int i=3; i>=0; i--)
            {
            // do something
            }
            [/CODE](Yes, for-loops can go BOTH ways... ^^)

            Greetings,
            Nepomuk

            Comment

            Working...