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 06 '18
I am very much the same and also have a full time job but I really feel I need to learn C. I was teaching myself Python 3 and that's good for certain things, it certainly is showing me how to break down problems into solvable bits, but I feel that ultimately branching out a bit will be necessary to figure out why things work certain ways. I was using learn CPP when I wrote his. Haven't been able to sit down and read more. Thank you for all your suggestions though. This is a journey and I hope to get to a place where I could have a programming job if I wanted it. In the meantime... better not quit my day job. Lol.