Thread: True "if" not executing

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    20

    True "if" not executing

    Ok, i'm having trouble with this if statement. I'm pretty sure I understand if statments well enough to make one this simple work right.

    For aurguments sake, and also because I've tried this combonation, well say the variable equal the following

    pl1attack = 2
    pl2move = 2
    those two variables should produce a true
    and

    pl2attack = 2
    pl1move = 1
    those to variable should produce a false, which combined with a
    "!=", should produce a true, right?

    Code:
    if (pl1attack == pl2move && pl2attack != pl1move)
            {
                    printf("You hit your opponent for 1 hp\n");
                    pl2hp--;
            }
    so all conditions should produce a "true", therefor the code should execute correct? But whenever this is true, it executes on of the "else if" statments thats is a part of this code block.

    if you wish to view the whole code block, here it is

    Code:
    //Check for damage
            if (pl1attack == pl2move && pl2attack != pl1move)
            {
                    printf("You hit your opponent for 1 hp\n");
                    pl2hp--;
            }
            else if (pl1attack != pl2move && pl2attack != pl1move)
            {
                    printf("You both miss.\n");
            }
            else if (pl1attack != pl2move && pl2attack == pl1move)
            {
                    printf("You are hit for 1 hp\n");
                    pl1hp--;
            }
            else if (pl1attack == pl2move && pl2attack == pl1move)
            {
                    printf("You block his attack.\n");
            }
            else
            {
                    printf("Something weird happened\n");
            }
    Thanks for the help, i'm still new to C, but I thought I had a good grasp on "if".

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by m.mixon
    pl1attack = 2
    pl2move = 2
    those two variables should produce a true
    and

    pl2attack = 2
    pl1move = 1
    those to variable should produce a false, which combined with a
    "!=", should produce a true, right?
    When a certain part of your program gives you a different answer than what you expect, there are two possibilities:

    1. You aren't feeding the values that you thought.
    2. The expression evaluates differently than you thought.

    Assuming that your analysis is correct, what could be the problem?

    You could try to see what the program is working on:

    Code:
    .
    .
    .
        printf("pl1attack = %d, pl1move = %d, pl2attack = %d, pl2move = %d\n",
                pl1attack, pl1move, pl2attack, pl2move);
        if (pl1attack == pl2move && pl2attack != pl1move)
            {
                    printf("You hit your opponent for 1 hp\n");
            }
    .
    .
    .

    If you are feeding these values to the expression and it's evaluating differently, then you need to check again. If you aren't feeding the values that you thought, then find out how they got that way.

    D

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Dave Evans
    When a certain part of your program gives you a different answer than what you expect, there are two possibilities:

    1. You aren't feeding the values that you thought.
    2. The expression evaluates differently than you thought.
    A third possibility is that the variables are being changed between the point where you set them and where they are tested.

    This can result from correct program logic (eg a global being changed by a called function, or a function changing an argument that is passed to it via a pointer).

    It can also result from incorrect program behaviour (eg falling off the end of an array, molesting a pointer, etc etc). This type of problem is harder to track down (technically: it is often some form undefined behaviour) as the code making the change might appear to have nothing to do with the affected variables.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Applied philosophy refresher
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 25
    Last Post: 04-28-2009, 09:55 PM
  2. Looking for feedback on program segment
    By avron in forum C++ Programming
    Replies: 4
    Last Post: 05-07-2007, 04:38 PM
  3. C++ Operator Overloading help
    By Bartosz in forum C++ Programming
    Replies: 2
    Last Post: 08-17-2005, 12:55 PM
  4. MCI CD Player
    By soutine in forum Windows Programming
    Replies: 0
    Last Post: 11-02-2001, 05:03 PM
  5. True or False Quiz (Need help)
    By Twiggy in forum C Programming
    Replies: 9
    Last Post: 10-12-2001, 04:25 AM