Is it possible to use array in a loop as condition?

Is it possible to use an array as condition in a loop? For example in while loop?

For instance: I want while loop recur unless index in array[2][6] = {'G'}.

1
2
while ({'G'} != array[2][6]) {
}


I found absolutely nothing using 2 search engines...
Last edited on
well, say that :)
while(array[2][6] != 'G')
{
???
}

you basically had it apart from the {}.
Last edited on
you basically had it apart from the {}.

Assuming it's not a 3D array.
If it is 3D then it'll take a loop (or a hidden loop in an <algorithm> function such as mismatch) to compare them.
I had it as you I just can't write sorry.

This code doesn't work:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

int main()
{
    char array[6][6];

    for (int i = 0; i < 6; i++)
    {
        for (int j; j < 6; j++) {
            array[i][j] = { 'T' };
        }
    }

    while (array[2][6] != {'G'}) {

        std::cout << "test";
        std::cin >> array[2][6];
    }

    return 0;
}

BTW it says error C2143, C2059 on this line: while (array[2][6] != {'G'}) {

I get these a lot something with semicolon, while code is perfectly correct, don't get it why!
Last edited on
code is perfectly correct

No, it is not!

Line 9: the j is not initialized. Undefined behaviour.


Line 10 has direct assignment. Since C++11 it has allowed the braces, {}.
That is why line 10 is ok.
https://en.cppreference.com/w/cpp/language/operator_assignment


Line 14 should compare two characters.
The 'G' is a character.
The {'G'} is not a character. It is an array that contains one character.
There is no != that compares two plain arrays.


Remember: There is no element at index N in an array that has N elements.
Your matrix has 6 columns. Columns 0, 1, 2, 3, 4, and 5. The 6 is not a valid index.

However, the array[2][6] is technically a character. The first character of fourth row.
It should be written: array[3][0]
I bet that was not your intention.
Last edited on
You are going out of bounds when you index with array[2][6]. You are assigning a value to the 7th element, not the 6th. Remember arrays are zero-based.

You forgot to initialize j in the second for loop. That could cause the loop to make assigning values to the array's elements go out of bounds.

Why are you enclosing the constant char 'G' in brackets? Remove them.

Line 14 should be something like while (array[2][5] != 'G')

Enclosing 'G' in brackets makes it appear to the compiler like it is part of an initialization statement instead of part of a comparison.

You don't need the brackets when assigning 'T' as well.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

int main()
{
   char array[6][6];

   for (int i { }; i < 6; i++)
   {
      for (int j { }; j < 6; j++)
      {
         array[i][j] = 'T';
      }
   }

   while (array[2][5] != 'G')
   {
      std::cout << "test\n: ";
      std::cin >> array[2][5];
   }
}

test
: w
test
: g
test
: G


So yes, it is possible to use an array as a condition in a loop.
Last edited on
Furry Guy

Thanks furry guy!

I Am not in a best mood, I had it fixated that that arrays initialize characters in {}, completely forgot I was testing it against other expression?

Oh so C2134 is just syntax error, I had some code. I was confused with message: "missing ; before statement" because it didn't make logically sense. Also in this case intellisense highlights incorrect character in a syntax. Maybe it will help next time.
Last edited on
When you enclosed 'G' in brackets in the while statement Visual Studio 2019 reports the problem as being a different error than what you received:
Error (active)	E0029	expected an expression

Same problem, just a different way to report the error. It isn't a missing semi-colon despite what the error report from your compiler indicates.

Oh so C2134 is just syntax error

Not necessarily, but it might not be the actual cause of the error (or warning) as your initial code snippet shows.

A lot of times the line(s) your compiler says where an error occurs might not be the actual problem. It might be a few lines before where the real culprit is.

More times than I can count fixing one error, usually the first in that gawd-awful humongous list, can cause multiple errors to go away without doing anything to fix them.

Be flexible when (not if) errors and warnings rear their ugly heads. Take a break for a bit if frustration begins to crop up.

And fix one error at a time.

Topic archived. No new replies allowed.