Hello , I want to ask why I am not receiving the same result using either the if with break inside the while loop ,either the while( x!=X && y!= Y ).

#include <stdio.h>
#include <stdlib.h>


int sol( int X, int Y )
{

    int x = 0, y = 1,i = 0;

    while ( x!= X &&  y!= Y )
    //while (1)
    {

        if ( i % 2 != 0 ) x += 1;

        if ( i % 2 == 0 ) y += 1;

        i++;

        printf( "x = %d\t y = %d\t i = %d\n",x,y,i );

        //if ( x == X && y == Y ) break;

    }

    return i; 
}


int main( int argc, char ** argv )
{

    printf( "sol = %d\n",sol(2,3) );
    return 0;
}

Click Here

You get different results for two reasons. For one thing, one example has the test at the top of the loop and the other has the test at the bottom (guaranteeing at least one loop execution). Also, translating while (A and B) to the (effective) opposite (in one case you want to continue if the conditional is true, in the other you want to exit) you would get if (not A or not B).

If I run the following script (vbScript is not case sensitive so I'm using xx, yy for X and Y).

for x = 0 to 1
    for xx = 0 to 1
        for y = 0 to 1
            for yy = 0 to 1
                top = x <> xx and y <> yy
                bot = x =  xx and y =  yy
                Wscript.Echo x,xx,y,xx, Left(cstr(top),1), Left(cstr(bot),1)
            next
        next
    next
next

You'll see the results.

0 0 0 0 F T
0 0 0 0 F F
0 0 1 0 F F
0 0 1 0 F T
0 1 0 1 F F
0 1 0 1 T F
0 1 1 1 T F
0 1 1 1 F F
1 0 0 0 F F
1 0 0 0 T F
1 0 1 0 T F
1 0 1 0 F F
1 1 0 1 F T
1 1 0 1 F F
1 1 1 1 F F
1 1 1 1 F T

Column 5 is the result of the while test at the top of the loop and column 6 is the if test at the bottom of the loop. Note that the if test should be the opposite of the while test because in one case you want to continue if true and in the other you want to continue if false.

So if your test at the top of the loop is

while (x != X && y != Y)

then your test at the bottom should be

if (!(x != X && y != Y))

or (de Morgan's theorem where not(A and B) = not(A) or not(B))

if (!(x != X) || !(y != Y))

or finally

if (x == X || y == Y)

Hmm..

This `if (!(x != X && y != Y))` isn't equal to     `if (x == X && y == Y)` ?

But , generally , I can't understand the logic?

In one case I am using while x and y are not equal ,continue to do work.So,when they become equal stop.

In the other, if x and y become equal stop.So, while they are not equal continue to work.

Why aren't the same?

Thank you!

In the one case you want to continue if x is not equal to X and y is not equal to Y. You have two conditionals that must both be true to continue. If either conditional is false then you do not execute the loop. In other words if condition A is false or if condition B is false then you break. Your conditionals become

while (x != X && y != Y)

and the equivalent at the bottom is

if (!(x != X && y != Y))

which becomes (once you simplify)

if (x == X || y == Y) break;

Thanks for the help.

I can understand that from if (!(x != X && y != Y)) we can write if (x == X || y == Y) break; . But , I can't understand how this is right for my problem.Because I want to breal when both conditions apply , when x and y are equal to X ,Y ,not one of them.

Your choice will depend on whether of not you want the loop to execute at the least once or zero times. If you want to execute at the least zero times then use

while (x != X || y != Y) {
    .
    .
    .
}

If you want to ensure at least one execution then use

while (1) {
    .
    .
    .
    if (x == X && y == Y) break;
}

However, I'm basing that on your most recent post which says

Because I want to breal (sic) when both conditions apply , when x and y are equal to X ,Y ,not one of them.

Your original post stated

while( x!=X && y!= Y )

which means execute while neither of the conditions is true. If you can restate what you want to do then I can show you the proper statement(s). Ideally you should state it as

I want to execute the loop (at least once/at least zero times) while the following conditions are true...

Also... So , I have to use while ( x !=X && y!=Y ) always?To be right?

Ok, so , I want to execute the loop at least zero times while x!=X and y!=Y.
When x == X and y == Y break the loop.

( If I want to execute it at leat once,I have to use do - while , right?)

Thank you very much.

Sorry , I just saw that for at least zero times you use while (x != X || y != Y)
I don't want something like that.

Generally, I want to execute until x equals X and y equals Y.( I think I want to execute at least one time,since I will always start from x=0,y=1 and I will go up)

Sorry if I am confusing you..

If your starting values guarantee at least one execution then use the while as in

while (x != X && y != Y) {
> If your starting values guarantee at least one execution then use the while as in
> while (x != X && y != Y) {

Above you said:

> If you want to ensure at least one execution then use
> 
>     while (1) {
>         .
>         .
>         .
>         if (x == X && y == Y) break;
> 
   > }

And then , we go back to the original problem I have..

Anyway , I think the whole situation is to decide if you want the last execution or not.

Thanks for the help.

If you want to use the if at the end then use

if (x == X || y == Y) break;

Ok, thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.