using '?' conditional with for loop

see these line:
for (unsigned int index=blnFunction? 2 : 1 , varcount=1; index< blnFunction? ...
i don't get any compiler errors, but when i execute it, the exe give me an error(like an exit error)..
but if i use the parentheses, will works normaly:
for (unsigned int index=(blnFunction? 2 : 1) , varcount=1; index< (blnFunction? CodeLine.size(): CodeLine.size()-1);...
why the compiler don't give me an error\warning about using it with or without parenthese?
Last edited on

You're completely changing the meaning by adding those parenthese.

First, did you actually mean = , or did you mean == ?

What are you trying to do with index? Are you trying to set it equal to blnFunction, or are you trying to set it to the value 2 or 1, depending on blnFunction?
Last edited on
The keyword here is precedence of operators. See:

http://www.cplusplus.com/doc/tutorial/operators/

The ?: has a very low precedence.
thank for correct me Coder777.
Repeater: the index value depends on blnFunction value.

for(initialvalue; condition; increment)
increment or decrement, it depends what order we need

but like Coder777 said, and makes sence is the precedence of operators.
thanks to all
Last edited on
> why the compiler don't give me an error\warning about using it

Some compilers do warn you if warnings are enabled.

1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
    extern bool b ;
    for( int i = b ? 2 : 1 , v = 1 ; i < b ? 10 : 20 ; ++i, ++v )  ;
    
    // GNU (-Wall -Wextra):
    //  warning: ?: using integer constants in boolean context, the expression will always evaluate to 'true' [-Wint-in-bool-context]
    // 5 |     for( int i = b ? 2 : 1 , v = 1 ; i < b ? 10 : 20 ; ++i, ++v )  ;
    //   |                                      ~~~~~~^~~~~~~~~
    
    // Microsoft (-W4):
    //  warning C4804: '<': unsafe use of type 'bool' in operation
}

http://coliru.stacked-crooked.com/a/0e9bbad9f4692a57
https://rextester.com/LYDD97379

Perhaps the more important question: why do you want to write this kind of code?
"Perhaps the more important question: why do you want to write this kind of code?"
1
2
3
4
5
6
7
8
9
10
11
12
13
 void RulesVar(vector<TokensType> CodeLine, unsigned int intCodeLine, bool blnFunction=false)
    {
        //var [const] VarName [as VarType [= VarValue]]
        if( CodeLine.size()==1)
        {
            ErrorList.push_back({"Var creation error!!!", "You must give a var name!!!", 0, intCodeLine});
        }
        bool blnConst=false;
        bool blnArray=false;
        bool blnArraySize=false;
        for (unsigned int index=(blnFunction? 2 : 1), varcount=1; index<blnFunction? CodeLine.size(): CodeLine.size()-1; index++, varcount++)
        {
            if(CodeLine[index].Token=="[")

is for i see where i must start and where i must stop
Something like this, perhaps:

1
2
3
4
5
6
7
8
9
std::size_t index = 1 + blnFunction ; // blnFunction ? 2 : 1
const std::size_t ubound = CodeLine.size()-1 + blnFunction ; // blnFunction ? CodeLine.size() : CodeLine.size()-1
for( ; index < ubound ; ++index )
{
    // if( CodeLine.at(index) ) etc.
    // ...
}

const std::size_t varcount = index - blnFunction ;

true.... thanks
Topic archived. No new replies allowed.