You do realize that ++x is not a shortcut for x+1, do you? |
Well before you explain everything in detail:
https://cplusplus.com/forum/beginner/284503/#msg1232157
I had the same concept on my mind! I thought ++x <= 9 is x+1 <=9 and it's only related to this expression. but it's funny because I had already learned how pre-post increments worked. here is a good example from the link seeplus provided:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
#include <iostream>
int main()
{
int x{ 5 };
int y{ 5 };
std::cout << x << ' ' << y << '\n'; //5 5
std::cout << ++x << ' ' << --y << '\n'; // 6 4
std::cout << x << ' ' << y << '\n'; // 6 4
std::cout << x++ << ' ' << y-- << '\n'; // 6 4
std::cout << x << ' ' << y << '\n'; // 7 3
return 0;
}
|
Just like the example, I thought incs/decs would only work for Bitwise operators and each cout expression can only have one variable of the same type. that's why I got confused when it came to logical operators that check whether an expression is true or false
And btw you gave another example in one of your previous posts that either I missed or you edited your post later. You said:
Instead of
|
std::cout << (++x <= 9 && x + 2 >= 10);
|
write
1 2
|
++x;
std::cout << (x <= 9 && x + 2 >= 10);
|
It was a very good example and I fully understood what you meant by that but is it the same case for suffix as well ? I mean what if it's x++ ? then the result will be completely different right ? take this new one as an example:
1 2
|
x = 7;
std::cout << (x++ == 7 && x + 2 >= 10); //1 (7 == 7 && 8 + 2 >= 10)
|
1 2 3
|
x = 7;
x++; //or ++x
std::cout << (x == 7 && x + 2 >= 10); //0 (8 == 7 && 8 + 2 >= 10)
|
I was in the middle of learning C++ through YouTube courses (beginner to advanced) and I thought it would have been easier to learn things visually. maybe it wasn't a good idea because I think they might miss some important programming tips and I might end up asking such questions.
The link that you provided, on the other hand, is great. I think I should start learning from here:
https://www.learncpp.com/cpp-tutorial
Thanks again guys and I'm gonna mark this topic as solved