looping condition

157 views
Skip to first unread message

Jahid Hasan

unread,
Jan 21, 2021, 12:54:12 PM1/21/21
to C++ GCU Forum
arr[] is the array,   for(i=0; arr[i], i++)  how the condition works ?

zoso

unread,
Jan 23, 2021, 8:14:42 AM1/23/21
to C++ GCU Forum
I'm assuming that you meant for a semicolon after arr[i] there else this would fail to compile.

The for loop has 3 statements:

1. The initializer statement ---> i = 0. It initialized i to 0
2. The condition statement ---> This has to evaluate to a boolean.I'll discuss this below.
3. The modification statement ---> This changes something, maybe the loop counter or anything else.

In the example you've shared, arr[i] needs to be evaluated as a boolean. So this would evaluate to arr[i] != 0. 

So if we have something like this:

#include <iostream>

int main()
{
    int arr[] = {2, 4, 5, 0, 3, 6};
    
    for(int i = 0; arr[i]; i++) {
         std::cout<<arr[i]<<'\n';
    }
}

The expected output is:
4
5

Why? because once i become 3, arr[i] is arr[3] = 0 and the boolean condition arr[i] evalates to false since arr[i] != 0 evaluates to false. I hope that answers your query.

mr N

unread,
Jan 23, 2021, 8:26:40 AM1/23/21
to cpp-gc...@googlegroups.com
Hi!
What does this condition mean?

int main()
{
 int arr[] = {2, 4, 5, 0, 3, 6};  

for (int i = 0; 3 ; i++)
{
std::cout << arr[i]<< std::endl;
}
return 0;
}

--
You received this message because you are subscribed to the Google Groups "C++ GCU Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cpp-gcu-foru...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cpp-gcu-forum/dc936172-5af1-49c8-aa72-936a45b4f81fn%40googlegroups.com.

zoso

unread,
Jan 24, 2021, 7:03:09 AM1/24/21
to C++ GCU Forum
Hello Donald,

If you read my previous answer, the second statement within a for loop statement is the conditional statement. Here, in your example, the conditional statement is 3. As I'd mentioned, this would be evaluated as a boolean i.e. either true or false. Now in languages like C/C++, integers (and other integral types like long, etc.) can be implicitly converted to booleans. You can always verify what your conditional statement would evaluate to by thinking like follows:

    bool b = 3; //You can always try this as bool b = <whatever you have in the conditional statement of the for loop>*/
    if(b) {
        std::cout<<"3 evaluates to true\n";
    } else {
        std::cout<<"3 evaluates to false\n";
    }

If you actually run this, you'll see that 3 evaluates to true. Now the thing to note is that 3 will always evaluate to true. So your for-loop becomes an infinite for-loop since the conditional statement is always true. Once i becomes > 5 whatever is printed as arr[i] is not guaranteed by the language. The program may crash immediately, it may run for some time and then crash or it may just hang. This is what is called "undefined behavior" in C++. I hope that answers your query.

mr N

unread,
Jan 24, 2021, 7:16:34 AM1/24/21
to cpp-gc...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages