what is wrong with these 'if' combination?

else if(index<CodeLine.size()-2 && (CodeLine[index+1].Token!="]" || (CodeLine[index+1].Type!=TokenType::Integer && CodeLine[index+2].Token!="]")))
i'm testing if the next token is ']' or is an integer and the next is ']', unless isn't valid...
what is wrong between the or and the and boolean combinations?
i fix it in a different way with several tries :(
1 - i test the right rules for ignore it;
2 - unless will be an error.

1
2
3
4
5
6
7
8
9
 else if(index<CodeLine.size() && (CodeLine[index+1].Token=="]" || (CodeLine[index+1].Type==TokenType::Integer && CodeLine[index+2].Token=="]" )))
                {
                    //correct combination
                }
                else
                {
                    ErrorList.push_back({"Var creation error!!!", "You must use ']' and or a size for close the array creation and the type!!!",intCodeLine, index});
                    break;
                }

it's different, but works like i need
1. You are checking "!=" rather than "==". Is that what you want?
2. Is token a char or a string? If it's a char, you want to check against ']', not "]".
3. When you have a convoluted if condition like this, it helps to break it down into more lines. That will make it not only easier for you to see your logic, but also allows others to read and comment on your code. The following may be overkill, but it shows you how breaking up a condition can make it a little bit easier to understand.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
else if(index<CodeLine.size()-2)
{
    bool gotIt = false;
    if(CodeLine[index+1].Token!="]")
    {
        gotIt = true;
    }
    else if (CodeLine[index+1].Type!=TokenType::Integer)
    {
        if (CodeLine[index+2].Token!="]")
        {
            gotIt = true;
        }
    }

    if (gotIt == true)
    {
        // do your thing here
    }
}
Last edited on
that's why i divided and works like i need.
but i used an 'if' empty condition for works
thank you so much for all
i'm sorry but i need ask: my last code works fine.. but i still thinking: why the previous code don't works?
on last code, i have an empty 'if' for works. but i do it directly on previous code:
else if(index<CodeLine.size()-2 && (CodeLine[index+1].Token!="]" || (CodeLine[index+1].Type!=TokenType::Integer && CodeLine[index+2].Token!="]")))
you can see the difference between the 2 codes.. that's why is make me so confused
In one code snippet you compare using "!=", but in the other you compare using "==". That's why one works and one doesn't.

Also, the first thing you check is "index < CodeLine.size()-2" compared to "index<codeLine.size()".

You are checking different things in your 2 if statements.
Last edited on
forget the 'size()'(is for avoid index problems).. my problem is these line:
(CodeLine[index+1].Token!="]" || (CodeLine[index+1].Type!=TokenType::Integer && CodeLine[index+2].Token!="]")))
but seems not working... that's why i use these empty 'if':
(CodeLine[index+1].Token=="]" || (CodeLine[index+1].Type==TokenType::Integer && CodeLine[index+2].Token=="]" ))
what i'm doing wrong on 1st 'if'?
Boolean logic:
1
2
3
A or B
// is neither equal nor opposite of
(not A) or (not B)


You should look at De Morgan's laws.

7 or 42 is true if the value is either of those two values.
!7 or !42 is true with all values, because at least one side of or is always true.
!(7 or 42) is true when the value is not 7 and is not 42. Anything, but 7 and 42.
Last edited on
keskiverto: i'm thinking and i think that i can use that empty 'if' and it's the best for do the correct code...
i'm sorry, but can you share the link?
i don't remember it :(
thank you so much for all to all
If:
1
2
3
4
5
6
7
8
9
 else if(index<CodeLine.size() && (CodeLine[index+1].Token=="]" || (CodeLine[index+1].Type==TokenType::Integer && CodeLine[index+2].Token=="]" )))
                {
                    //correct combination
                }
                else
                {
                    ErrorList.push_back({"Var creation error!!!", "You must use ']' and or a size for close the array creation and the type!!!",intCodeLine, index});
                    break;
                }

is indeed the correct logic you could just negate the entire statement to get rid of the empty if():
1
2
3
4
5
6
 else if(!(index<CodeLine.size() && (CodeLine[index+1].Token=="]" || (CodeLine[index+1].Type==TokenType::Integer && CodeLine[index+2].Token=="]" ))))

                {
                    ErrorList.push_back({"Var creation error!!!", "You must use ']' and or a size for close the array creation and the type!!!",intCodeLine, index});
                    break;
                }


Last edited on
jlb: thank you so much for all. i never thot that ideia ;)
some confused, but we can learn much more:
https://runestone.academy/runestone/books/published/csawesome/Unit3-If-Statements/topic-3-6-DeMorgan.html
Note that

index<CodeLine.size()

does not prevent the following expression from getting out of bounds.

CodeLine[index+1].Token=="]" CodeLine[index+2].Token=="]"


It needs to be

(index + 1)<CodeLine.size() (index + 2)<CodeLine.size()

I suggest that you break this single if into multiple.
coder777: you have right... thank you so much
Topic archived. No new replies allowed.