Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. C++ for loop syntax ?

C++ for loop syntax ?

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
9 Posts 4 Posters 15 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.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    In English please. I like to understand the "for loop " iteration (better). So what are

    variableName : arrayName

    if I only have the attached single line of code ?

         QList adapters = QBluetoothLocalDevice::allDevices();
    
            Syntax
                    for (type variableName : arrayName) {
                // code block to be executed
            }
    
    CPalliniC L J 4 Replies Last reply
    0
    • L Lost User

      In English please. I like to understand the "for loop " iteration (better). So what are

      variableName : arrayName

      if I only have the attached single line of code ?

           QList adapters = QBluetoothLocalDevice::allDevices();
      
              Syntax
                      for (type variableName : arrayName) {
                  // code block to be executed
              }
      
      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #2

      Well, the documentation[^] is written in English. You may also find some tutorials on the web, check out, for instance: C++ Ranged for Loop (With Examples)[^]. Finally, in your scenario, it could be

      QList adapters = QBluetoothLocalDevice::allDevices()
      for (auto & adapter : adapters)
      {
      // code block to be executed: read/write access to adapter
      }

      or

      QList adapters = QBluetoothLocalDevice::allDevices()
      for (const auto & adapter : adapters)
      {
      // code block to be executed: read only access to adapter
      }

      "In testa che avete, Signor di Ceprano?" -- Rigoletto

      In testa che avete, signor di Ceprano?

      L 1 Reply Last reply
      0
      • CPalliniC CPallini

        Well, the documentation[^] is written in English. You may also find some tutorials on the web, check out, for instance: C++ Ranged for Loop (With Examples)[^]. Finally, in your scenario, it could be

        QList adapters = QBluetoothLocalDevice::allDevices()
        for (auto & adapter : adapters)
        {
        // code block to be executed: read/write access to adapter
        }

        or

        QList adapters = QBluetoothLocalDevice::allDevices()
        for (const auto & adapter : adapters)
        {
        // code block to be executed: read only access to adapter
        }

        "In testa che avete, Signor di Ceprano?" -- Rigoletto

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        So only the "array" name is already "assigned" , the (loop ) item of interest is my choice. Little confusing. Thanks Yes, I RTFM and this "descioption" , makes better sense for (rangeDeclaration : rangeExpression) { // code }

        1 Reply Last reply
        0
        • L Lost User

          In English please. I like to understand the "for loop " iteration (better). So what are

          variableName : arrayName

          if I only have the attached single line of code ?

               QList adapters = QBluetoothLocalDevice::allDevices();
          
                  Syntax
                          for (type variableName : arrayName) {
                      // code block to be executed
                  }
          
          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          In some ways it is just like an ordinary for loop. The difference being that the variable is intialised and incremented automatically. Given the following:

          for (int foo : arayOfIntegers)
          {
          // each time these lines are executed
          // foo will contain the value of the next tiem in the list
          // until the list is exhausted
          }

          Think of it as:

          foreach item in arayOfIntegers
          BEGIN
          set foo to the value of the next item in the list
          do stuff with foo
          END

          L 1 Reply Last reply
          0
          • L Lost User

            In some ways it is just like an ordinary for loop. The difference being that the variable is intialised and incremented automatically. Given the following:

            for (int foo : arayOfIntegers)
            {
            // each time these lines are executed
            // foo will contain the value of the next tiem in the list
            // until the list is exhausted
            }

            Think of it as:

            foreach item in arayOfIntegers
            BEGIN
            set foo to the value of the next item in the list
            do stuff with foo
            END

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Yes, but I did not know that I can "name" the "item" and it is not related to actual "name" of items in the array.

            L K 2 Replies Last reply
            0
            • L Lost User

              Yes, but I did not know that I can "name" the "item" and it is not related to actual "name" of items in the array.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              It is just a loop variable, the same as in a standard for loop.

              1 Reply Last reply
              0
              • L Lost User

                Yes, but I did not know that I can "name" the "item" and it is not related to actual "name" of items in the array.

                K Offline
                K Offline
                k5054
                wrote on last edited by
                #7

                You should be thinking in terms of containers, elements and iterators. While the ranged for loop does work for POD arrays, the dimension has to be known at compile time. For example

                void showarray(int *arr)
                {
                for(i : arr)
                std::cout << i << ' ';
                std::cout << '\n';
                }

                will not compile, since the dimension of arr is not known. On the other hand, if the compiler can deduce the size of the array, then a ranged for loop may be used with a POD array:

                #include

                int main()
                {
                std::cout << "Enter size : ";
                int n;
                std::cin >> n;
                int arr[n];
                for(int i = 0; i < n; ++i) {
                arr[i] = i;
                }
                for( auto x : arr) {
                std::cout << x << ' ';
                }
                std :: cout << '\n';
                }

                Here the compiler can deduce the dimension of the array arr and this program compiles and executes as expected. In the general case, though the ranged for loop works with containers which implement begin() and end(). That's true for STL containers, and lots of other container like objects from other libraries. Addendum In addition to begin() and end() the container also needs to implement iterators. In the STL, you cannot use a ranged for loop with a stack, queue or priority_queue

                Keep Calm and Carry On

                1 Reply Last reply
                0
                • L Lost User

                  In English please. I like to understand the "for loop " iteration (better). So what are

                  variableName : arrayName

                  if I only have the attached single line of code ?

                       QList adapters = QBluetoothLocalDevice::allDevices();
                  
                          Syntax
                                  for (type variableName : arrayName) {
                              // code block to be executed
                          }
                  
                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  A culture of entitlement.

                  "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

                  1 Reply Last reply
                  0
                  • L Lost User

                    In English please. I like to understand the "for loop " iteration (better). So what are

                    variableName : arrayName

                    if I only have the attached single line of code ?

                         QList adapters = QBluetoothLocalDevice::allDevices();
                    
                            Syntax
                                    for (type variableName : arrayName) {
                                // code block to be executed
                            }
                    
                    J Offline
                    J Offline
                    jschell
                    wrote on last edited by
                    #9

                    No idea why your question was down voted. So I up voted it. Sometimes I can guess at skill level so it helps to frame an answer but I am unsure of your skill level. (I am using 'skill' intentionally in a very ambiguous way here.) Do you understand what the following loop construct does?

                    for (int i = 1; i <= 5; ++i)

                    If you do not then I suggest that you experiment with that for a while. And look up different ways to structure it including replacing it with a while loop. The structure that you asked about is just syntactic sugar that ends up implementing something similar to the above. So understanding it first helps.

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups