what is wrong with these 'if' condition?

please see these IF:
 
  else if(VarStep=="vartype" && tokens[index-1].Description=="as")

- 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?
Last edited on
if #1 assumes index-1 is a legal index. can index be zero or create other out of bounds issues?

if #2 assumes size is not zero. it returns false if size is 1, else its true, which may be a bug.
it assumes index+1 is in bounds.

the statements do what you asked (look for not '= 'and not ',') but it will go nuts if your indexing assumptions are bugged.
but if i can enter on 1st 'if', why the 2nd is 'losed' or 'ignored'?
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.
The || should be &&.
so i did a different 'if'(instead the 1st showed code with 2 if's):
1
2
3
4
5
if(VarStep=="vartype" && index==tokens.size()-1 && IsType(tokens[index-1].Description) && (tokens[index].Description!="=" || tokens[index].Description!="," ))
            {
                string strError = "Line: " + to_string(LineError) + "\t" + to_string(index) + " - syntax error!!!\t" + tokens[index].Description;
                ErrorList.push_back(strError);
            }

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
Last edited on
now i see about your, both, suggestions about changing the '||' to '&&'.
thank you so much for all.
why, sometimes, is very hard to make some 'if' conditions?

You may be interested in this link that explains some of the logical pitfalls and how to avoid them.

https://www.viva64.com/en/b/0390/

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.

Clear?
i see.... thank you so much for all
You're welcome!
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
Topic archived. No new replies allowed.