Why does my list include numbers bigger than the while loop?
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
add a comment |
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
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
add a comment |
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
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
java
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
add a comment |
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
add a comment |
3 Answers
3
active
oldest
votes
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.
add a comment |
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);{
}
}
add a comment |
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
}
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 21 '18 at 5:52
KartikKartik
4,18731537
4,18731537
add a comment |
add a comment |
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);{
}
}
add a comment |
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);{
}
}
add a comment |
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);{
}
}
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);{
}
}
answered Nov 21 '18 at 5:52
Pooja AggarwalPooja Aggarwal
870212
870212
add a comment |
add a comment |
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
}
add a comment |
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
}
add a comment |
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
}
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
}
answered Nov 21 '18 at 6:46
siang swee kongsiang swee kong
663
663
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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