• C

While statement - multiple criteria


Hi

I have a while statement that looks like this:

        while (test1 != 'y')

which means while the value of test1 is not equal to 'y'.

If I wanted to ammend this so that it was:

While the value of test1 is not equal to y or /and x what would the syntax look like ?
andyw27Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Sys_ProgCommented:
Hi andyw27,

And
while (test1 != 'y' && test1 != 'x')

OR
while (test1 != 'y' || test1 != 'x')


Cheers!
0
PaulCaswellCommented:
The concern here is that you can easily end up with what I call Logic Bloat.

You'll see it in code that has been enhanced to almost death. Statements that look like:

if ( (!SomeLogic && !OtherLogic) || !(SomeCompletelyUnrelatedLogic || SomethingElseEntirely) )
{
 ...
}

To avoid this I usually try to understand more about the tests. If they really relate to each other then by all means combine them into one. If they dont relate then you should use:

if ( test1 )
{
  if ( test2 )
  {
  }
}

or

if ( test2 )
{
  if (test1)
  {
  }
}

Or something like it.

Paul
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PaulCaswellCommented:
You may be looking for sometrhing like:

switch ( test1 )
{
  case 'y':
          ...
          break;

  case 'n':
         ...
         break;

  default:
        break;
   
}

Paul
0
Firewall Management 201 with Professor Wool

In this whiteboard video, Professor Wool highlights the challenges, benefits and trade-offs of utilizing zero-touch automation for security policy change management. Watch and Learn!

Sjef BosmanGroupware ConsultantCommented:
The construction
    while (test1 != 'y' || test1 != 'x')
is totally useless: the expression will always yield true!
0
Jaime OlivaresSoftware ArchitectCommented:
The OR test is usually something like:

while ( !(test1== 'y' || test1 == 'x') )

Also, when you have a very complex situation (more complex like this), you can build you evaluate function, so you can use:

while ( Evaluate(somedata) )

where evaluate is a function containing some 'if's or switch/case, and returns a boolean value (0/1) depending on evaluation results.
0
baboo_Commented:
This is the syntax you want:

while ( test1 != 'y' && test1 != 'x' )

It reads in English, "while test1 isn't 'y', and it isn't 'x'..."  It is equivalent to the statement:

while ( !( test1 == 'y' || test1 == 'x') )

which reads, "while it is NOT the case that test1 is equal to 'y' or 'x'..."

Interestingly enough, the equality of these 2 statements is acording to DeMorgan's law, but it should be easy enough to intuitively convince yourself of their equality.

baboo_
0
PaulCaswellCommented:
I use:

#define until(e) while(!(e))

You can then use the much more readable:

until ( test1 == 'y' || test1 == 'x' )

but I got into a 'discussion' over this a few months ago with some exceptionally knowledgable people on this site and they suggested that this could cause problems for people from a non-english background.

Paul
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.