0

If I did change the == into a = in an if statement will it change the value of the of the variable

8th Jul 2017, 9:38 AM
Pixels Gamer
Pixels Gamer - avatar
9 Respostas
0
yes it will change the value of variable and make your if statement always true and it will be executed every time and in case of loop sometime it goes into infinite loop also
8th Jul 2017, 9:57 AM
vaibhav
+ 5
@Karl I think I meant to say that = can be used in if conditional statements without producing compilation errors, not literally = being ==. I'll just leave my original post there as a monument to my derpyness. :>
8th Jul 2017, 10:19 AM
Hatsy Rei
Hatsy Rei - avatar
+ 5
int main() { int a=7; if (a=1){a++;} cout<<a; //outputs 2 } Same for C++ It does not result in an error. If a is assigned 0 in the condition block, the statements in {} wont run. The statements run for all other values of a.
8th Jul 2017, 11:17 AM
Pixie
Pixie - avatar
+ 4
Hatsy- you sure about that? I think JS considers = as a =, not ==.
8th Jul 2017, 9:51 AM
Karl T.
Karl T. - avatar
+ 4
@Hatsy- Ok gotcha. =)
8th Jul 2017, 10:22 AM
Karl T.
Karl T. - avatar
+ 3
IIRC, JS is tolerant towards the use of = in if statements, and will evaluate it as a == (?). For C++ and Java, this should result in a compilation error.
8th Jul 2017, 9:40 AM
Hatsy Rei
Hatsy Rei - avatar
+ 1
yes value will be changed and if block will be executed if value is not false a = 5; if(a = 7){ alert('true'); } alert(a); this will show alert "true" and alert "7"
8th Jul 2017, 10:27 AM
A l
A l - avatar
0
no @vaibhav it will not be executed everytime, it depends on values try this a = 5; if(a = false){ alert('true'); } alert(a);
8th Jul 2017, 10:23 AM
A l
A l - avatar
0
Thank you
8th Jul 2017, 10:38 AM
Pixels Gamer
Pixels Gamer - avatar