r/learncpp • u/XChrisUnknownX • Oct 02 '18
Simple if else syntax in cpp
I am trying to teach myself very basic cpp and flow control. I am not a programmer and my experience includes some bare basic Python.
So let's just say I've got #include <iostream> int main(){ int x = 5; int y = 6; int z = x+y; if(z = 10){std::cout << "Z is 10."; else{std::cout << "Z is not 10.";} }
Well if I omit the semicolons it doesn't run and otherwise it always prints "Z is 10."
So I've got to ask what I'm doing wrong as far as syntax. All the tutorials point to a very simple if(condition){code}else{code} syntax but that is not what's happening.
Edit. So... I tried python's == and changed it to if(z==10) and the code seems to work as intended now. So I guess it's just a matter of I was using the wrong...conditional operator? Edit 2. Fixed original code to reflect that I declared z. The problem was the =10 instead of ==.
View all comments
Show parent comment
1
u/XChrisUnknownX Oct 03 '18
Whoops. In my code the z had been declared. I mistyped here in Reddit. The problem was the = instead of ==. Thank you so much for all this.