So I would like to know if there's any way to do the following more efficiently:

``while(g[0] = 0 || g[0] = 1 || g[0] = 2 || g[0] = 3 || g[0] = 4 || g[0] = 5 || g[0] = 6 || g[0] = 7 || g[0] = 8 || g[0] = 9)``

I think in other languages (not sure) there's a keyword called in so it would end up something like this:

``while(in.g[0](1, 2, 3, 4, 5, 6, 7, 8, 9))``

Is there anyway to compress all my or's into something more compact?

5
Contributors
6
Replies
7
Views
8 Years
Discussion Span
Last Post by Sky Diploma

Cant you just say while (i< 9 && i > 0)?

I only want integers.

Firstly, I would like to point out to you that your code is actually assigning values but not comparing them.
[

``while(g[0] = 0 || g[0] = 1 || g[0] = 2 || g[0] = 3 || g[0] = 4 || g[0] = 5 || g[0] = 6 || g[0] = 7 || g[0] = 8 || g[0] = 9)``

This should be ...

``while(g[0] == 0 || g[0] == 1 || g[0] == 2 || g[0] == 3 || g[0] == 4 || g[0] == 5 || g[0] == 6 || g[0] == 7 || g[0] == 8 || g[0] == 9)``

And if you want a better way you can always declare a function,

``````bool check_valid ( int x )
{
for ( int a = 0 ;a < 10; a++ )
if( x == a )
return true ;
return false ;
}``````

And the while loop can be.

``while( check_valid( g[0] ) )``

And if you want a better way you can always declare a function,

``````bool check_valid ( int x )
{
for ( int a = 0 ;a < 10; a++ )
if( x == a )
return true ;
return false ;
}``````

Only problem is that your function has massive overhead. Why not just do:

``````bool check_valid ( int x )
{
if (x >= 0 && x < 10)
return true;
return false;
}``````

or even smaller:

``````bool check_valid ( int x )
{
return (x>=0 && x <10);
}``````

Only problem is that your function has massive overhead. Why not just do:

``````bool check_valid ( int x )
{
if (x >= 0 && x < 10)
return true;
return false;
}``````

or even smaller:

``````bool check_valid ( int x )
{
return (x>=0 && x <10);
}``````

I agree that this makes it more efficient . My only approach was to exhibit what the OP was trying to do, with a for loop!!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.