C++ for loop syntax ?
-
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 }
-
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 }
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
-
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 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 }
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 -
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 -
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.
-
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.
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 implementbegin()
andend()
. That's true for STL containers, and lots of other container like objects from other libraries. Addendum In addition tobegin()
andend()
the container also needs to implement iterators. In the STL, you cannot use a ranged for loop with astack
,queue
orpriority_queue
Keep Calm and Carry On
-
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 }
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
-
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 }
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.