Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 220
  • Last Modified:

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 ?
0
andyw27
Asked:
andyw27
1 Solution
 
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
 
PaulCaswellCommented:
You may be looking for sometrhing like:

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

  case 'n':
         ...
         break;

  default:
        break;
   
}

Paul
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now