Thread: if statement multiple conditions

  1. #1
    Registered User
    Join Date
    Jan 2018
    Posts
    1

    Angry if statement multiple conditions

    how would i fix this conditional error?

    Code:
    		switch(on_off){			case 0:{
    				if(send_bits==0x000||0x4000||0x4080||0x4081||0x4001||0x0081||0x0080||0x0016){
    					send_bits = send_bits - LEDs;
    				}
    				break; //off
    			}
    			case 1:{
    				if(send_bits==0x000||0x4000||0x4080||0x4081||0x4001||0x0081||0x0080||0x0016){
    					send_bits = send_bits + LEDs;
    				}
    				break; //on
    			}
    	}

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,390
    Your tests of send_bits need to all mention send_bits, like:
    Code:
        if (send_bits==0x0000||send_bits==0x4000||send_bits==0x4080||
            send_bits==0x4081||send_bits==0x4001||send_bits==0x0081||
            send_bits==0x0080||send_bits==0x0016)
    But it's complicated enough that you could write a function (or macro function).
    Code:
    int test_bits(SendBitsType b) {
        return b==0x0000||b==0x4000||b==0x4080||b==0x4081||
               b==0x4001||b==0x0081||b==0x0080||b==0x0016;
    }
    
    // ... some function
        if (test_bits(send_bits))
            switch (on_off) {
            case 0:
                send_bits -= LEDs;
                break;
            case 1:
                send_bits += LEDs;
                break;
            }
    Alternatively, you could use multiple switch cases together:
    Code:
        switch (send_bits) {
        case 0x0000: case 0x4000: case 0x4080: case 0x4081:
        case 0x4001: case 0x0081: case 0x0080: case 0x0016:
            send_bits += (on_off ? 1 : -1) * LEDs;
        }
    A long time ago being crazy meant something. - Charles Manson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. if statement with 2 conditions
    By robando in forum C Programming
    Replies: 2
    Last Post: 04-12-2015, 04:18 AM
  2. Help - If statement with multiple conditions
    By trintiin in forum C Programming
    Replies: 3
    Last Post: 02-27-2013, 01:49 AM
  3. multiple conditions - if statement
    By dibble in forum C Programming
    Replies: 8
    Last Post: 03-28-2009, 12:41 PM
  4. IF statement - to conditions
    By cornacum in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 08:39 PM
  5. If statement conditions
    By Brewer in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:55 AM

Tags for this Thread