Why does my list include numbers bigger than the while loop?












2















I created a while loop with break statements inside it to print some numbers and avoid others and it ended up printing a number bigger the the limit of my loop. Here's the code



package examples;

public class LoopContolExamples {
public static void main(String args) {
int count = 0;
while (count <= 12) {
count++;

if (count == 9)
//break;
continue;

if (count % 2 == 0)
continue;
else
System.out.println(count);{
}
}
}
}


And here's the result.

3
5
7
11
13



Can anyone help explain what is going on?
I'm new to Java and this is one of the first pieces of code I've written in the language.










share|improve this question























  • Well, you still enter the loop while the count is 12 and then, afterwards, inside the loop increment it to 13...

    – arkascha
    Nov 21 '18 at 5:52













  • you must remove equals sign in the while loop if you want the values to be less than 12

    – Hades
    Nov 21 '18 at 6:31
















2















I created a while loop with break statements inside it to print some numbers and avoid others and it ended up printing a number bigger the the limit of my loop. Here's the code



package examples;

public class LoopContolExamples {
public static void main(String args) {
int count = 0;
while (count <= 12) {
count++;

if (count == 9)
//break;
continue;

if (count % 2 == 0)
continue;
else
System.out.println(count);{
}
}
}
}


And here's the result.

3
5
7
11
13



Can anyone help explain what is going on?
I'm new to Java and this is one of the first pieces of code I've written in the language.










share|improve this question























  • Well, you still enter the loop while the count is 12 and then, afterwards, inside the loop increment it to 13...

    – arkascha
    Nov 21 '18 at 5:52













  • you must remove equals sign in the while loop if you want the values to be less than 12

    – Hades
    Nov 21 '18 at 6:31














2












2








2








I created a while loop with break statements inside it to print some numbers and avoid others and it ended up printing a number bigger the the limit of my loop. Here's the code



package examples;

public class LoopContolExamples {
public static void main(String args) {
int count = 0;
while (count <= 12) {
count++;

if (count == 9)
//break;
continue;

if (count % 2 == 0)
continue;
else
System.out.println(count);{
}
}
}
}


And here's the result.

3
5
7
11
13



Can anyone help explain what is going on?
I'm new to Java and this is one of the first pieces of code I've written in the language.










share|improve this question














I created a while loop with break statements inside it to print some numbers and avoid others and it ended up printing a number bigger the the limit of my loop. Here's the code



package examples;

public class LoopContolExamples {
public static void main(String args) {
int count = 0;
while (count <= 12) {
count++;

if (count == 9)
//break;
continue;

if (count % 2 == 0)
continue;
else
System.out.println(count);{
}
}
}
}


And here's the result.

3
5
7
11
13



Can anyone help explain what is going on?
I'm new to Java and this is one of the first pieces of code I've written in the language.







java






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 5:49









frank dinfrank din

143




143













  • Well, you still enter the loop while the count is 12 and then, afterwards, inside the loop increment it to 13...

    – arkascha
    Nov 21 '18 at 5:52













  • you must remove equals sign in the while loop if you want the values to be less than 12

    – Hades
    Nov 21 '18 at 6:31



















  • Well, you still enter the loop while the count is 12 and then, afterwards, inside the loop increment it to 13...

    – arkascha
    Nov 21 '18 at 5:52













  • you must remove equals sign in the while loop if you want the values to be less than 12

    – Hades
    Nov 21 '18 at 6:31

















Well, you still enter the loop while the count is 12 and then, afterwards, inside the loop increment it to 13...

– arkascha
Nov 21 '18 at 5:52







Well, you still enter the loop while the count is 12 and then, afterwards, inside the loop increment it to 13...

– arkascha
Nov 21 '18 at 5:52















you must remove equals sign in the while loop if you want the values to be less than 12

– Hades
Nov 21 '18 at 6:31





you must remove equals sign in the while loop if you want the values to be less than 12

– Hades
Nov 21 '18 at 6:31












3 Answers
3






active

oldest

votes


















5














It entered the loop when count was equal to 12.



And then you immediately did count++;, making it 13, while it was still inside the loop. The next iteration would fail the condition.






share|improve this answer































    1














    You are printing the value of count after incrementing it. So when it entered the loop, it will be 12 and then the value is incremented to 13 which then gets printed. Now if you want to print numbers leass than or equal to 12 then you need to change your code like this:



    int count = 0;
    while (count < 12) {
    count++;

    if (count == 9)
    //break;
    continue;

    if (count % 2 == 0)
    continue;
    else
    System.out.println(count);{
    }
    }





    share|improve this answer































      1














      If you want the range to be from 0 to 12, this is one alternative solution for this problem without changing the condition in the while loop.



           int count = 0;
      while (count <= 12) {
      if (count == 9)
      ; //do nothing
      if (count % 2 == 0)
      ; //do nothing
      else
      System.out.println(count); //otherwise, print the count
      count++; //increment the count
      }





      share|improve this answer























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53405953%2fwhy-does-my-list-include-numbers-bigger-than-the-while-loop%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        5














        It entered the loop when count was equal to 12.



        And then you immediately did count++;, making it 13, while it was still inside the loop. The next iteration would fail the condition.






        share|improve this answer




























          5














          It entered the loop when count was equal to 12.



          And then you immediately did count++;, making it 13, while it was still inside the loop. The next iteration would fail the condition.






          share|improve this answer


























            5












            5








            5







            It entered the loop when count was equal to 12.



            And then you immediately did count++;, making it 13, while it was still inside the loop. The next iteration would fail the condition.






            share|improve this answer













            It entered the loop when count was equal to 12.



            And then you immediately did count++;, making it 13, while it was still inside the loop. The next iteration would fail the condition.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 21 '18 at 5:52









            KartikKartik

            4,18731537




            4,18731537

























                1














                You are printing the value of count after incrementing it. So when it entered the loop, it will be 12 and then the value is incremented to 13 which then gets printed. Now if you want to print numbers leass than or equal to 12 then you need to change your code like this:



                int count = 0;
                while (count < 12) {
                count++;

                if (count == 9)
                //break;
                continue;

                if (count % 2 == 0)
                continue;
                else
                System.out.println(count);{
                }
                }





                share|improve this answer




























                  1














                  You are printing the value of count after incrementing it. So when it entered the loop, it will be 12 and then the value is incremented to 13 which then gets printed. Now if you want to print numbers leass than or equal to 12 then you need to change your code like this:



                  int count = 0;
                  while (count < 12) {
                  count++;

                  if (count == 9)
                  //break;
                  continue;

                  if (count % 2 == 0)
                  continue;
                  else
                  System.out.println(count);{
                  }
                  }





                  share|improve this answer


























                    1












                    1








                    1







                    You are printing the value of count after incrementing it. So when it entered the loop, it will be 12 and then the value is incremented to 13 which then gets printed. Now if you want to print numbers leass than or equal to 12 then you need to change your code like this:



                    int count = 0;
                    while (count < 12) {
                    count++;

                    if (count == 9)
                    //break;
                    continue;

                    if (count % 2 == 0)
                    continue;
                    else
                    System.out.println(count);{
                    }
                    }





                    share|improve this answer













                    You are printing the value of count after incrementing it. So when it entered the loop, it will be 12 and then the value is incremented to 13 which then gets printed. Now if you want to print numbers leass than or equal to 12 then you need to change your code like this:



                    int count = 0;
                    while (count < 12) {
                    count++;

                    if (count == 9)
                    //break;
                    continue;

                    if (count % 2 == 0)
                    continue;
                    else
                    System.out.println(count);{
                    }
                    }






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 21 '18 at 5:52









                    Pooja AggarwalPooja Aggarwal

                    870212




                    870212























                        1














                        If you want the range to be from 0 to 12, this is one alternative solution for this problem without changing the condition in the while loop.



                             int count = 0;
                        while (count <= 12) {
                        if (count == 9)
                        ; //do nothing
                        if (count % 2 == 0)
                        ; //do nothing
                        else
                        System.out.println(count); //otherwise, print the count
                        count++; //increment the count
                        }





                        share|improve this answer




























                          1














                          If you want the range to be from 0 to 12, this is one alternative solution for this problem without changing the condition in the while loop.



                               int count = 0;
                          while (count <= 12) {
                          if (count == 9)
                          ; //do nothing
                          if (count % 2 == 0)
                          ; //do nothing
                          else
                          System.out.println(count); //otherwise, print the count
                          count++; //increment the count
                          }





                          share|improve this answer


























                            1












                            1








                            1







                            If you want the range to be from 0 to 12, this is one alternative solution for this problem without changing the condition in the while loop.



                                 int count = 0;
                            while (count <= 12) {
                            if (count == 9)
                            ; //do nothing
                            if (count % 2 == 0)
                            ; //do nothing
                            else
                            System.out.println(count); //otherwise, print the count
                            count++; //increment the count
                            }





                            share|improve this answer













                            If you want the range to be from 0 to 12, this is one alternative solution for this problem without changing the condition in the while loop.



                                 int count = 0;
                            while (count <= 12) {
                            if (count == 9)
                            ; //do nothing
                            if (count % 2 == 0)
                            ; //do nothing
                            else
                            System.out.println(count); //otherwise, print the count
                            count++; //increment the count
                            }






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 21 '18 at 6:46









                            siang swee kongsiang swee kong

                            663




                            663






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53405953%2fwhy-does-my-list-include-numbers-bigger-than-the-while-loop%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Guess what letter conforming each word

                                Port of Spain

                                Run scheduled task as local user group (not BUILTIN)