- the 'VarStep' is the var declaration step;
- the 'tokens' is a structure where i save the words;
- the 'index' it's the 'tokens' vector position;
i can enter on these 'if' without problems.
now see the next 'if': if( index<tokens.size()-1 && (tokens[index+1].Description!="=" || tokens[index+1].Description!="," ))
for test the next index i must test if i'm on last index\vector item.
my objective is test if the next vector item description isn't '=' or ','.
i'm doing a beginner error, but i can't see what i'm doing wrong :(
can anyone explain more please?
but if i can enter on 1st 'if', why the 2nd is 'losed' or 'ignored'?
Because that is how the language was designed. Why evaluate the rest of the if() statement if it won't alter the outcome?
Since you're using "and" for the first part of the statement , if the left part of the statement evaluates to false the rest of the statement doesn't need to be evaluated.
With "or" if the left part of the statement evaluates to true then the rest of the statement doesn't need to be evaluated.
now seems to works like i need. was so hard.
let me ask: why, sometimes, is very hard to make some 'if' conditions?
i understand that it's a vague question... but maybe i can learn much more
the '||', if one is '1', the result is '1'(true).
so why is the best use '&&' instead '||'? (tokens[index].Description!="=" || tokens[index].Description!="," )
like you see i only need test 1 string on same index. but i accept corrections.
thank you so much for all
If tokens[index].Description is ",", then the condition tokens[index].Description != "=" will be true. Therefore, the whole compound condition will be true.
There is no value for tokens[index].Description which will make both
tokens[index].Description != "="
and
tokens[index].Description != ","
false. Therefore, there is no value of tokens[index].Description for which your entire condition can ever be false.
maybe the others can read these topic, because have very important information from some beginners mistakes about conditions.
thanks to all and stay well
the ideas expressed there, and more advanced ones like studying boolean logic/math and k-maps, can help you to write much more efficient code as well as correct code. This is a very important topic, but it is also a pretty cut and dried one -- with a bit of reading and a bit of practice, it will become much easier and second nature to you.
as you learn, keep in the back of your mind, that conditions tend to be a bit expensive (esp when checking strings or classes where the comparison operation is really doing a LOT of hidden work). the better you express what you want to happen, the more efficient your code will be.
for example this is a bit costly: VarStep=="vartype" (it has to compare letter by letter to match this in a loop) --- maybe it would be better if varstep was some sort of enum, but if it has to be a string, then it has to be a string, but you can put this comparison last so it is skipped as often as possible, maybe... things like that.
The most important thing is to get the logic right, though. Worry about speed later, for now, being aware of it is sufficient.
thank you so much for all... thank you.
what i have learned about C\C++ and more was by books and tutorials from internet... and much more from forums. and not so much by school, so i'm learning by experience and yours experience.
thank you so much for all