Why does this if statement return trueWhy can't variables be declared in a switch statement?What does the...
What did the 'turbo' button actually do?
Making a electromagnet
How to politely tell someone they did not hit "reply to all" in an email?
Why does the hash of infinity have the digits of π?
Why did Drogon spare this character?
Gravitational Force Between Numbers
Take elements from a list based on two criteria
Dad jokes are fun
Time complexity of an algorithm: Is it important to state the base of the logarithm?
ESTA validity after a visa denial
Which European Languages are not Indo-European?
How can I make an argument that my time is valuable?
Are runways booked by airlines to land their planes?
Gravitational effects of a single human body on the motion of planets
Why do we need to chain the blocks (creating blockchain) in a permissioned blockchain?
Did this character show any indication of wanting to rule before S8E6?
How did NASA Langley end up with the first 737?
Why does Bran want to find Drogon?
Is it possible to prohibit all prohibitable schools of magic with a single character?
Can I install a back bike rack without attachment to the rear part of the frame?
Do photons bend spacetime or not?
Why are Stein manifolds/spaces the analog of affine varieties/schemes in algebraic geometry?
Dealing with spaghetti codebase, manager asks for things I can't deliver
Security vulnerabilities of POST over SSL
Why does this if statement return true
Why can't variables be declared in a switch statement?What does the explicit keyword mean?Why can templates only be implemented in the header file?Why is “using namespace std” considered bad practice?What does T&& (double ampersand) mean in C++11?Why are elementwise additions much faster in separate loops than in a combined loop?Why does changing 0.1f to 0 slow down performance by 10x?Why is reading lines from stdin much slower in C++ than Python?Image Processing: Algorithm Improvement for 'Coca-Cola Can' RecognitionWhy is it faster to process a sorted array than an unsorted array?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I've been thinking of some beginner mistakes and I ended up with the one on the if statement. I expanded a bit the code to this:
if (i = 1 && i == 0) {
cout << i;
}
what I have seen is that the if statement returns true and it cout's i as 1. If i is 1 why the boolean expression returned true?
c++ if-statement
add a comment |
I've been thinking of some beginner mistakes and I ended up with the one on the if statement. I expanded a bit the code to this:
if (i = 1 && i == 0) {
cout << i;
}
what I have seen is that the if statement returns true and it cout's i as 1. If i is 1 why the boolean expression returned true?
c++ if-statement
5
Guys, this is not a typo question. The OP wants to know why the if statement is entered with this code sinceiis set to1.
– NathanOliver
4 hours ago
You assign 1 to i, though it should never print from what I can see
– JVApen
4 hours ago
Or does it assign the result of1 && i == 0?
– JVApen
4 hours ago
add a comment |
I've been thinking of some beginner mistakes and I ended up with the one on the if statement. I expanded a bit the code to this:
if (i = 1 && i == 0) {
cout << i;
}
what I have seen is that the if statement returns true and it cout's i as 1. If i is 1 why the boolean expression returned true?
c++ if-statement
I've been thinking of some beginner mistakes and I ended up with the one on the if statement. I expanded a bit the code to this:
if (i = 1 && i == 0) {
cout << i;
}
what I have seen is that the if statement returns true and it cout's i as 1. If i is 1 why the boolean expression returned true?
c++ if-statement
c++ if-statement
edited 4 hours ago
πάντα ῥεῖ
75k1078147
75k1078147
asked 4 hours ago
TehMattGRTehMattGR
647
647
5
Guys, this is not a typo question. The OP wants to know why the if statement is entered with this code sinceiis set to1.
– NathanOliver
4 hours ago
You assign 1 to i, though it should never print from what I can see
– JVApen
4 hours ago
Or does it assign the result of1 && i == 0?
– JVApen
4 hours ago
add a comment |
5
Guys, this is not a typo question. The OP wants to know why the if statement is entered with this code sinceiis set to1.
– NathanOliver
4 hours ago
You assign 1 to i, though it should never print from what I can see
– JVApen
4 hours ago
Or does it assign the result of1 && i == 0?
– JVApen
4 hours ago
5
5
Guys, this is not a typo question. The OP wants to know why the if statement is entered with this code since
i is set to 1.– NathanOliver
4 hours ago
Guys, this is not a typo question. The OP wants to know why the if statement is entered with this code since
i is set to 1.– NathanOliver
4 hours ago
You assign 1 to i, though it should never print from what I can see
– JVApen
4 hours ago
You assign 1 to i, though it should never print from what I can see
– JVApen
4 hours ago
Or does it assign the result of
1 && i == 0?– JVApen
4 hours ago
Or does it assign the result of
1 && i == 0?– JVApen
4 hours ago
add a comment |
2 Answers
2
active
oldest
votes
This has to do with operator precedence.
if (i = 1 && i == 0)
is not
if ((i = 1) && (i == 0))
because both && and == have a higher precedence than =. What it really works out to is
if (i = (1 && (i == 0))
which assigns the result of 1 && (i == 0) to i. So, if i starts at 0 then i == 0 is true, so 1 && true is true (or 1), and then i gets set to 1. Then since 1 is true, you enter the if block and print the value you assigned to i.
add a comment |
Assuming your code actually looks like this:
#include <iostream>
using namespace std;
int main() {
int i = 0;
if (i = 1 && i == 0) {
cout << i;
}
}
Then this:
if (i = 1 && i == 0) {
evaluates as
if (i = (1 && i == 0)) {
and so i is set to 1.
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%2f56264674%2fwhy-does-this-if-statement-return-true%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
This has to do with operator precedence.
if (i = 1 && i == 0)
is not
if ((i = 1) && (i == 0))
because both && and == have a higher precedence than =. What it really works out to is
if (i = (1 && (i == 0))
which assigns the result of 1 && (i == 0) to i. So, if i starts at 0 then i == 0 is true, so 1 && true is true (or 1), and then i gets set to 1. Then since 1 is true, you enter the if block and print the value you assigned to i.
add a comment |
This has to do with operator precedence.
if (i = 1 && i == 0)
is not
if ((i = 1) && (i == 0))
because both && and == have a higher precedence than =. What it really works out to is
if (i = (1 && (i == 0))
which assigns the result of 1 && (i == 0) to i. So, if i starts at 0 then i == 0 is true, so 1 && true is true (or 1), and then i gets set to 1. Then since 1 is true, you enter the if block and print the value you assigned to i.
add a comment |
This has to do with operator precedence.
if (i = 1 && i == 0)
is not
if ((i = 1) && (i == 0))
because both && and == have a higher precedence than =. What it really works out to is
if (i = (1 && (i == 0))
which assigns the result of 1 && (i == 0) to i. So, if i starts at 0 then i == 0 is true, so 1 && true is true (or 1), and then i gets set to 1. Then since 1 is true, you enter the if block and print the value you assigned to i.
This has to do with operator precedence.
if (i = 1 && i == 0)
is not
if ((i = 1) && (i == 0))
because both && and == have a higher precedence than =. What it really works out to is
if (i = (1 && (i == 0))
which assigns the result of 1 && (i == 0) to i. So, if i starts at 0 then i == 0 is true, so 1 && true is true (or 1), and then i gets set to 1. Then since 1 is true, you enter the if block and print the value you assigned to i.
answered 4 hours ago
NathanOliverNathanOliver
102k17148229
102k17148229
add a comment |
add a comment |
Assuming your code actually looks like this:
#include <iostream>
using namespace std;
int main() {
int i = 0;
if (i = 1 && i == 0) {
cout << i;
}
}
Then this:
if (i = 1 && i == 0) {
evaluates as
if (i = (1 && i == 0)) {
and so i is set to 1.
add a comment |
Assuming your code actually looks like this:
#include <iostream>
using namespace std;
int main() {
int i = 0;
if (i = 1 && i == 0) {
cout << i;
}
}
Then this:
if (i = 1 && i == 0) {
evaluates as
if (i = (1 && i == 0)) {
and so i is set to 1.
add a comment |
Assuming your code actually looks like this:
#include <iostream>
using namespace std;
int main() {
int i = 0;
if (i = 1 && i == 0) {
cout << i;
}
}
Then this:
if (i = 1 && i == 0) {
evaluates as
if (i = (1 && i == 0)) {
and so i is set to 1.
Assuming your code actually looks like this:
#include <iostream>
using namespace std;
int main() {
int i = 0;
if (i = 1 && i == 0) {
cout << i;
}
}
Then this:
if (i = 1 && i == 0) {
evaluates as
if (i = (1 && i == 0)) {
and so i is set to 1.
answered 4 hours ago
Neil ButterworthNeil Butterworth
27.8k54983
27.8k54983
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%2f56264674%2fwhy-does-this-if-statement-return-true%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
5
Guys, this is not a typo question. The OP wants to know why the if statement is entered with this code since
iis set to1.– NathanOliver
4 hours ago
You assign 1 to i, though it should never print from what I can see
– JVApen
4 hours ago
Or does it assign the result of
1 && i == 0?– JVApen
4 hours ago