if

Discussion in 'C++' started by ahso, Feb 2, 2012.

  1. ahso

    ahso Guest

    Hi
    I only want either first or second if but not both executed. ie. if
    value 1 do not execute 2 even if 2nd value ==1 but also if 2nd do not
    execute first if.
    Thanks


    if (R22value == 1){
    DreiDimensional = 0;
    function();
    }

    if (R23value == 1){
    DreiDimensional = 1;
    function();
    }
    ahso, Feb 2, 2012
    #1
    1. Advertising

  2. ahso

    Ian Collins Guest

    On 02/ 2/12 11:13 PM, ahso wrote:
    > Hi
    > I only want either first or second if but not both executed. ie. if
    > value 1 do not execute 2 even if 2nd value ==1 but also if 2nd do not
    > execute first if.


    That requirement contradicts its self.

    --
    Ian Collins
    Ian Collins, Feb 2, 2012
    #2
    1. Advertising

  3. "ahso" wrote in message
    news:...
    >
    >Hi
    >I only want either first or second if but not both executed. ie. if
    >value 1 do not execute 2 even if 2nd value ==1 but also if 2nd do not
    >execute first if.
    >Thanks
    >
    >
    >if (R22value == 1){
    > DreiDimensional = 0;
    > function();
    >}
    >
    >if (R23value == 1){
    > DreiDimensional = 1;
    > function();
    >}


    It is not clear to me what you mean. As I read it, the obvious solution is:

    if (R22value == 1){
    DreiDimensional = 0;
    function();

    } else if (R23value == 1){
    DreiDimensional = 1;
    function();
    }
    Fred Zwarts \(KVI\), Feb 2, 2012
    #3
  4. ahso

    ahso Guest

    The problem is that either value can be 1. I only want to avoid
    running both simultanoulsy. I tried with setting within each if the
    other value to 0 but
    that's not working. I probably need to do as Fred so users have to
    click the first value to disable then click the second.
    ahso, Feb 2, 2012
    #4
  5. "ahso" wrote in message
    news:...
    >
    >The problem is that either value can be 1. I only want to avoid
    >running both simultanoulsy. I tried with setting within each if the
    >other value to 0 but
    >that's not working. I probably need to do as Fred so users have to
    >click the first value to disable then click the second.


    What are types of R22value, R23value and DrieDimensional?
    You use them as integer-like variables, what are their expected values?
    You seem to use only the values 0 and 1, maybe you should use bool type.
    It is still not clear to me what you want to do.
    Maybe you could explicitly program the four cases,
    which would make your intention more clear.
    What do you want to do in the other two cases?

    if (R22value == 1 ){
    if (R23value == 1){
    // R22value == 1, R23value == 1

    throw Inconsistency;????
    ComplainToUser ();?????

    } else {
    // R22value == 1, R23value != 1

    DreiDimensional = 0;
    function();

    }
    } else {
    if (R23value == 1){
    // R22value != 1, R23value == 1

    DreiDimensional = 1;
    function();

    } else {
    // R22value != 1, R23value != 1

    throw Inconsistency;???????
    ComplainToUser ();?????

    }
    }
    Fred Zwarts \(KVI\), Feb 2, 2012
    #5
  6. ahso

    copx Guest

    On 02.02.2012 11:13, ahso wrote:
    > Hi
    > I only want either first or second if but not both executed. ie. if
    > value 1 do not execute 2 even if 2nd value ==1 but also if 2nd do not
    > execute first if.
    > Thanks
    >
    >
    > if (R22value == 1){
    > DreiDimensional = 0;
    > function();
    > }
    >
    > if (R23value == 1){
    > DreiDimensional = 1;
    > function();
    > }


    English is not your first language, right? Seriously, your broken
    English makes it hard to understand what you actually mean.
    I guess you want this:

    if (R23value != 1 && R22value == 1){
    DreiDimensional = 0;
    function();
    }

    if (R22value != 1 && R23value == 1){
    DreiDimensional = 1;
    function();
    }
    copx, Feb 2, 2012
    #6
  7. ahso <> wrote:
    > Hi
    > I only want either first or second if but not both executed. ie. if
    > value 1 do not execute 2 even if 2nd value ==1 but also if 2nd do not
    > execute first if.
    > Thanks
    >
    >
    > if (R22value == 1){
    > DreiDimensional = 0;
    > function();
    > }
    >
    > if (R23value == 1){
    > DreiDimensional = 1;
    > function();
    > }


    Just put an "else" before the second "if".
    Juha Nieminen, Feb 2, 2012
    #7
  8. ahso

    LR Guest

    ahso wrote:
    > Hi
    > I only want either first or second if but not both executed. ie. if
    > value 1 do not execute 2 even if 2nd value ==1 but also if 2nd do not
    > execute first if.
    > Thanks
    >
    >
    > if (R22value == 1){
    > DreiDimensional = 0;
    > function();
    > }
    >
    > if (R23value == 1){
    > DreiDimensional = 1;
    > function();
    > }



    What you want to do isn't entirely clear to me, but perhaps this snippet
    will be of use, if only as a point of departure,


    void f2(const int R22value, const int R23value) {

    std::cout << "(" << R22value << "," << R23value << ") ";

    static const int threeDim = 1;
    static const int twoDim = 0;
    static const int invalidDim = -1;

    const bool r2 = R22value != 0;
    const bool r3 = R23value != 0;

    const std::pair<bool,bool> ab(r2,r3);


    const int dim =
    ab == std::make_pair(false,true) ? threeDim :
    ab == std::make_pair(true,false) ? twoDim :
    invalidDim;


    if(dim != invalidDim) {
    // call function here
    std::cout << "It\'s valid, it was " << dim << std::endl;
    }
    else {
    // don't call function here
    std::cout << "It was invalid" << std::endl;
    }

    }
    LR, Feb 2, 2012
    #8
  9. ahso

    ahso Guest

    Thanks copx. I added after your code (only boolean 0/1 values):

    if (R23value == 1 && R22value == 1){
    R22value = 0;
    R23value = 0;

    }

    That way users have to double click but ok.
    ahso, Feb 3, 2012
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page