Thread: Weird behavior with &&

  1. #1
    Registered User
    Join Date
    Jul 2015
    Posts
    33

    Weird behavior with &&

    It's been awhile since I've worked with C, and I was playing around with some basic code to refresh, and I'm getting a weird result.

    I've initialized a 2d array of dxd - let's say d = 4. Then using the following code, I just print it out, but do not want to print the bottom right corner.

    Code:
    for(int i = 0; i < d; i++) {
            for(int j = 0; j < d; j++) {
                if (i != (d - 1) && j != (d - 1)) {
                    // printf("(i = %i and j = %i)", i, j);
                    printf("%i  ", board[i][j]);
                }
            }
            printf("\n");
    }
    So instead of print out something like this:
    15 14 13 12
    11 10 9 8
    7 6 5 4
    3 1 2 _

    I'm getting this:
    15 14 13
    11 10 9
    7 6 5

    I've looked over the loop several times and it seems correct. Am I wrong here?

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Yes, because only when both i and j are not 3 a number is printed. The logic you want is:
    Code:
    if (i == (d-1) && j == (d-1)) {
        continue;
    }
    printf("%i  ", board[i][j]);
    for some reason, you reversed it but forgot to reverse the operation. !(a && b) is equivalent to !a || !b, not !a && !b.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    You need to use OR, not AND. The code as written says to only print the element if neither of the indices are d - 1, so it doesn't print the last column or the last row.

    You want to say "don't print the element if both indices are d - 1", basically something like:
    Code:
    if (i == d-1 && j == d-1)
        do nothing
    else
        print the element
    Since we don't generally want a branch of an if statement that does nothing, we can negate the condition:
    Code:
    if (!(i == d-1 && j == d-1))
        print the element
    Using De Morgan's Laws we can express it as:
    Code:
    if (i != d-1 || j != d-1)
        print the element

  4. #4
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    It would be far easier to understand if you just rewrote the conditional not using as many logical nots.

    e.g.
    Code:
    if (!(i == d - 1 && j == d - 1)) {}
    you can then use DeMorgan's rules to rearrange it to your liking.

    Anyway, looking at your expression you have if (i != d -1 && j != d -1). Now look at j != d - 1. This is going to be false at the end of every "row" making the whole conditional false at the end of every row. This is obviously not your intention.


    Edit: Walk away from computer to get a drink without pressing submit and all of a sudden there are 2 answers before mine already

    Edit 2: To @jjwooyoung Thank you for posting such a small snippet, it's always helpful. Even more helpful is if you reduce the small snippet to code that can be compiled and executed as below (note, that this is the corrected version. Also I used c89 and not for no particular reason... in hindsight I shouldn't have done that).

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int i, j;
        int d = 4;
        int n = 0;
        
        for(i = 0; i < d; i++) {
            for(j = 0; j < d; j++) {
                if (i != d - 1 || j != d - 1) {
                    printf("%2i  ", n);
                }
                n++;
            }
            printf("\n");
        }
        return 0;
    }
    Last edited by Hodor; 07-30-2017 at 08:53 PM.

  5. #5
    Registered User
    Join Date
    Jul 2015
    Posts
    33
    Thanks everyone! For some reason my brain was telling me that statement evaluates to true when both were d - 1. I feel like such an idiot now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Weird behavior on Macro...
    By Sink0 in forum C Programming
    Replies: 9
    Last Post: 06-13-2011, 12:07 PM
  2. Weird cin behavior
    By Link_26 in forum C++ Programming
    Replies: 4
    Last Post: 06-25-2006, 09:25 PM
  3. really weird behavior, 16 bit asm w/masm
    By BobMcGee123 in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-01-2005, 06:45 PM
  4. Weird computer behavior
    By ober in forum Tech Board
    Replies: 14
    Last Post: 03-08-2005, 12:26 PM
  5. Weird Excel behavior
    By Govtcheez in forum Tech Board
    Replies: 1
    Last Post: 12-03-2004, 01:53 PM

Tags for this Thread