Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. one else for two if possible?
Forum Updated to NodeBB v4.3 + New Features

one else for two if possible?

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 4 Posters 1.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • QT-static-prgmQ Offline
    QT-static-prgmQ Offline
    QT-static-prgm
    wrote on last edited by
    #1

    Hi,

    my question is more a general c++ question then Qt, but i hope you can help me with that, too.

    Example 1:
    first condition: the index is not greater then the container
    2nd condition: the value in the container at the index is set
    if one of those is not given, the else should be executed.

    So it need to be like this:

    if (segmentIterator->textureIndex < m_materials->size() && m_materials->at(segmentIterator->textureIndex).transparent)
    	m_drawList.push_back(new_info);
    else
    	m_drawList.push_front(new_info);
    

    problem: if the first condition is false, i cannot even test the 2nd condition, so it need to be like this:

    if (segmentIterator->textureIndex < m_materials->size())
            if(m_materials->at(segmentIterator->textureIndex).transparent)
            	m_drawList.push_back(new_info);
    else
    	m_drawList.push_front(new_info);
    

    problem: dangling else. the compiler will read it like this:

    if (segmentIterator->textureIndex < m_materials->size())
    {
    	if(m_materials->at(segmentIterator->textureIndex).transparent)
    		m_drawList.push_back(new_info);
    	else
    		m_drawList.push_front(new_info);
    }
    

    Any solutions for that?? In theory an good compiler should optimize the code and if the first condition of a && expression is false, the 2nd shouldn't be even touched. But i'm not sure if that works. Any ideas about that??

    jsulmJ 1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      usa a variable...

      bool check = segmentIterator->textureIndex < m_materials->size();
      if(check) check =m_materials->at(segmentIterator->textureIndex).transparent;
      if(check) 
      m_drawList.push_back(new_info);
      else
      m_drawList.push_front(new_info);
      

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      1
      • QT-static-prgmQ QT-static-prgm

        Hi,

        my question is more a general c++ question then Qt, but i hope you can help me with that, too.

        Example 1:
        first condition: the index is not greater then the container
        2nd condition: the value in the container at the index is set
        if one of those is not given, the else should be executed.

        So it need to be like this:

        if (segmentIterator->textureIndex < m_materials->size() && m_materials->at(segmentIterator->textureIndex).transparent)
        	m_drawList.push_back(new_info);
        else
        	m_drawList.push_front(new_info);
        

        problem: if the first condition is false, i cannot even test the 2nd condition, so it need to be like this:

        if (segmentIterator->textureIndex < m_materials->size())
                if(m_materials->at(segmentIterator->textureIndex).transparent)
                	m_drawList.push_back(new_info);
        else
        	m_drawList.push_front(new_info);
        

        problem: dangling else. the compiler will read it like this:

        if (segmentIterator->textureIndex < m_materials->size())
        {
        	if(m_materials->at(segmentIterator->textureIndex).transparent)
        		m_drawList.push_back(new_info);
        	else
        		m_drawList.push_front(new_info);
        }
        

        Any solutions for that?? In theory an good compiler should optimize the code and if the first condition of a && expression is false, the 2nd shouldn't be even touched. But i'm not sure if that works. Any ideas about that??

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @QT-static-prgm said in one else for two if possible?:

        problem: if the first condition is false, i cannot even test the 2nd condition, so it need to be like this:

        What is the problem? Why do you want to test the second condition if the first is false?
        As far as I can see the second condition does not have side effects, so it should be fine.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        QT-static-prgmQ 1 Reply Last reply
        0
        • jsulmJ jsulm

          @QT-static-prgm said in one else for two if possible?:

          problem: if the first condition is false, i cannot even test the 2nd condition, so it need to be like this:

          What is the problem? Why do you want to test the second condition if the first is false?
          As far as I can see the second condition does not have side effects, so it should be fine.

          QT-static-prgmQ Offline
          QT-static-prgmQ Offline
          QT-static-prgm
          wrote on last edited by
          #4

          @jsulm i want to test the 2nd condition, but to prevent errors, i check the index to be not out of the range.

          btw for me works

          if (segmentIterator->textureIndex < m_materials->size() && m_materials->at(segmentIterator->textureIndex).transparent)
          	m_drawList.push_back(new_info);
          else
          	m_drawList.push_front(new_info);
          

          But i'm not sure if it is compiler specific.

          1 Reply Last reply
          0
          • Chris KawaC Offline
            Chris KawaC Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Your code is ok.
            Order of evaluation for && operator is specified and it is left to right. && also uses short circuit evaluation, so the right side is evaluated if and only if the left side is evaluated to true.

            VRoninV 1 Reply Last reply
            3
            • QT-static-prgmQ Offline
              QT-static-prgmQ Offline
              QT-static-prgm
              wrote on last edited by
              #6

              @Chris-Kawa all right, thank you. I just wasn't sure if it was only theory in my university lecture, or if it is implemented in all C++ compiler

              1 Reply Last reply
              0
              • Chris KawaC Chris Kawa

                Your code is ok.
                Order of evaluation for && operator is specified and it is left to right. && also uses short circuit evaluation, so the right side is evaluated if and only if the left side is evaluated to true.

                VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by VRonin
                #7

                @Chris-Kawa said in one else for two if possible?:

                also uses short circuit evaluation

                Disclaimer: I'm not 100% sure here

                I don't think this holds up in case of an overloaded operator&& so if transparent returns a type that has on overloaded operator&& that interacts with bool, the right part of the logical AND will be executed regardless of what the left part returns

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                1 Reply Last reply
                2
                • Chris KawaC Offline
                  Chris KawaC Offline
                  Chris Kawa
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @VRonin You're 100% right. That's a cool nitpick. I like it :D

                  In this case though I doubt it matters. As you said transparent would have to be a custom class and there would have to be a Something operator&&(bool a, const SomeClass& b) defined in the scope. That's not very common.

                  1 Reply Last reply
                  0
                  • QT-static-prgmQ Offline
                    QT-static-prgmQ Offline
                    QT-static-prgm
                    wrote on last edited by
                    #9

                    Ähm yes, transparent is a boolean value of a struct.

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved