Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Question on While Loops

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Question on While Loops

    Hi there, I'm an absolute beginner on Java. I wrote the following piece of code. I ran it and it gives the output as 1.
    Since 1 is not greater than 10, it should NOT execute the println at all. But why value i is printed. Can someone explain me why?


    public class While_Loop {

    public static void main(String[] args) {
    int i=1;

    while (i>10);
    {
    System.out.println(i);
    i=i+1;
    }

    }

    }


  2. #2
    Member
    Join Date
    Aug 2013
    Posts
    95
    Thanks
    3
    Thanked 14 Times in 14 Posts

    Default Re: Question on While Loops

    you have a semicolon after the while loop. You need to take it away.

  3. The Following User Says Thank You to camel-man For This Useful Post:

    GregBrannon (September 20th, 2014)

  4. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: Question on While Loops

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Please post your code correctly using code or highlight tags per the above link.

  5. #4
    Junior Member
    Join Date
    Sep 2014
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Question on While Loops

    remove semi colon ...

  6. #5
    Member
    Join Date
    May 2013
    Posts
    106
    My Mood
    Amused
    Thanks
    16
    Thanked 9 Times in 9 Posts

    Default Re: Question on While Loops

    Like they said, you gotta remove the semicolon. But even when you do that, your code will probably not have the result you intended. It should go from printing out the number "1" to having no output at all. Think about the value of i and the condition in your loop.

Similar Threads

  1. Question about do loops?
    By chakana101 in forum Java Theory & Questions
    Replies: 5
    Last Post: March 25th, 2014, 08:38 PM
  2. Question about do-while loops?
    By Scorks in forum Java Theory & Questions
    Replies: 2
    Last Post: April 11th, 2013, 07:08 AM
  3. Java Loops Question
    By ThatBeast in forum Java Theory & Questions
    Replies: 1
    Last Post: December 6th, 2012, 10:42 PM
  4. Question on do-while loops
    By Sean448 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 24th, 2012, 01:06 AM
  5. Beginner question about for loops...
    By ninjaBob in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 29th, 2012, 02:57 PM