Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

nested if statements

drumberdadrumberda Member Posts: 1
I'm trying to write this program that counts word length repetitions

I'm using if statements three layers deep, but the program seems to ignore the first two layers and only recognizes the third.

looks like:
if( r1==0 )
if(r2==0)
if(s1 != " " )
a3=1;

I know I'm supposed to use brackets. I tried that too so it would look like:
if
{
if
{
if
{
a3;
}
}
}
but it still did the same thing.

How can I make sure that all the conditions I want to impose are taken together, with none being ignored? Should I be using if statements, or would something else work better? I was thinking that i could cut down on one layer by saying:
if(r1==r2)
if(s1 != " ")
a3=1;
but the first condition is still ignored it seems.

is there a way to put all three conditions inside a single if statement?
Thanks for any help anyone can lend me.



Comments

  • kreitlerkreitler Member Posts: 218
    Hey drum,
    There's nothing wrong with the statements as written--it could be that you have a different problem (like r1 & r2 really [b]are[/b] zero, even though you don't expect them to be.
    That said, you can combine all these statements into a single conditional using the '&&' operator (read as: "and operator"). Anywhere you see '&&', read it as 'and'. So:

    [code]
    if (r1 == 0 && r2 == 0 && s1 != " ")
    a3 = 1;
    [/code]

    The above is logically equivalent to your original 3 'if' statements.

    Another useful logical operator is '||' -- which is read as "or". Tou would use it in a similar fashion:

    [code]
    if (bCheck == true || bOverride == true)
    {
    // Do something in here.
    }
    [/code]

    In this case, the "do something here" block gets executed if either bCheck is true [b]or[/b] bOverride is true.

    Although 'or' doesn't help you in your current situation, you'll surely be able to use it later.

    Hope this helps.

    Kreitler


    : I'm trying to write this program that counts word length repetitions
    :
    : I'm using if statements three layers deep, but the program seems to ignore the first two layers and only recognizes the third.
    :
    : looks like:
    : if( r1==0 )
    : if(r2==0)
    : if(s1 != " " )
    : a3=1;
    :
    : I know I'm supposed to use brackets. I tried that too so it would look like:
    : if
    : {
    : if
    : {
    : if
    : {
    : a3;
    : }
    : }
    : }
    : but it still did the same thing.
    :
    : How can I make sure that all the conditions I want to impose are taken together, with none being ignored? Should I be using if statements, or would something else work better? I was thinking that i could cut down on one layer by saying:
    : if(r1==r2)
    : if(s1 != " ")
    : a3=1;
    : but the first condition is still ignored it seems.
    :
    : is there a way to put all three conditions inside a single if statement?
    : Thanks for any help anyone can lend me.
    :
    :
    :
    :

Sign In or Register to comment.