Like Tree5Likes

Switch-Case vs If-Else loops

This is a discussion on Switch-Case vs If-Else loops within the C Programming forums, part of the General Programming Boards category; Originally Posted by comocomocomo I don't know where is the bottleneck, but... But... wait a minute... The 'switch(state)' doesn't make ...

  1. #16
    Registered User
    Join Date
    Dec 2012
    Posts
    37
    Quote Originally Posted by comocomocomo View Post
    I don't know where is the bottleneck, but...

    But... wait a minute... The 'switch(state)' doesn't make any sense! The variable 'state' will always be zero at this point. It is local, you initialized it to zero, you didn't change it... Maybe you meant to make it 'static':
    in case 0, (the initial state), there is a line that updates the variable 'state'. Same goes for case 1.

  2. #17
    Registered User
    Join Date
    Dec 2012
    Posts
    45
    Quote Originally Posted by kim15 View Post
    in case 0, (the initial state), there is a line that updates the variable 'state'. Same goes for case 1.
    Yes, but this doesn't help, since 'state' is an automatic local variable, which gets initialized to 0 every time the processor enters the function. If the compiler follows the rules of the language, 'state' will always be 0 when the 'switch' is reached.

  3. #18
    Registered User
    Join Date
    Dec 2012
    Posts
    37
    oh. so should i declare it as a global?

  4. #19
    Registered User
    Join Date
    Dec 2012
    Posts
    45
    You don't need it in any other function, and global variables are bad. Leave it local but make it static. It will live like a global variable (whole program execution lifespan, initialized only once), but it will remain private to its function.

  5. #20
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,344
    You are using MPLAB from memory -> But you still haven't told us what compiler you are using!

    It would also depend on whether you are using the LITE/Standard/PRO (You pay more money, your code gets optimised more)

    The best way would be to actually see what is being generated -> The switch statement is usually better when programming your PIC, because it generates an optimised branching table.

    Do the two different ways in your code. Click "View" -> "Disassembly listing"

    I'm using the C18 compiler PRO -

    Code:
    280:               	if (test_data == 1)
     039AE    0102     MOVLB 0x2
     039B0    0E01     MOVLW 0x1
     039B2    19FE     XORWF 0xfe, W, BANKED
     039B4    E101     BNZ 0x39b8
     039B6    51FF     MOVF 0xff, W, BANKED
     039B8    E10C     BNZ 0x39d2
    281:               	{
    282:               		puts("1");
     039BA    0E76     MOVLW 0x76
     039BC    6EE6     MOVWF 0xfe6, ACCESS
     039BE    0E4E     MOVLW 0x4e
     039C0    6EE6     MOVWF 0xfe6, ACCESS
     039C2    0E00     MOVLW 0
     039C4    6EE6     MOVWF 0xfe6, ACCESS
     039C6    ECFA     CALL 0x53f4, 0
     039C8    F029     NOP
     039CA    52E5     MOVF 0xfe5, F, ACCESS
     039CC    52E5     MOVF 0xfe5, F, ACCESS
     039CE    52E5     MOVF 0xfe5, F, ACCESS
    283:               	}
    284:               	else if	(test_data == 2)
     039D0    D04F     BRA 0x3a70
     039D2    0E02     MOVLW 0x2
     039D4    19FE     XORWF 0xfe, W, BANKED
     039D6    E101     BNZ 0x39da
     039D8    51FF     MOVF 0xff, W, BANKED
     039DA    E10C     BNZ 0x39f4
    285:               	{
    286:               		puts("2");
     039DC    0E74     MOVLW 0x74
     039DE    6EE6     MOVWF 0xfe6, ACCESS
     039E0    0E4E     MOVLW 0x4e
     039E2    6EE6     MOVWF 0xfe6, ACCESS
     039E4    0E00     MOVLW 0
     039E6    6EE6     MOVWF 0xfe6, ACCESS
     039E8    ECFA     CALL 0x53f4, 0
     039EA    F029     NOP
     039EC    52E5     MOVF 0xfe5, F, ACCESS
     039EE    52E5     MOVF 0xfe5, F, ACCESS
     039F0    52E5     MOVF 0xfe5, F, ACCESS
    287:               	}
    288:               	else if	(test_data == 3)
     039F2    D03E     BRA 0x3a70
     039F4    0E03     MOVLW 0x3
     039F6    19FE     XORWF 0xfe, W, BANKED
     039F8    E101     BNZ 0x39fc
     039FA    51FF     MOVF 0xff, W, BANKED
     039FC    E10C     BNZ 0x3a16
    289:               	{
    290:               		puts("3");
     039FE    0E72     MOVLW 0x72
     03A00    6EE6     MOVWF 0xfe6, ACCESS
     03A02    0E4E     MOVLW 0x4e
     03A04    6EE6     MOVWF 0xfe6, ACCESS
     03A06    0E00     MOVLW 0
     03A08    6EE6     MOVWF 0xfe6, ACCESS
     03A0A    ECFA     CALL 0x53f4, 0
     03A0C    F029     NOP
     03A0E    52E5     MOVF 0xfe5, F, ACCESS
     03A10    52E5     MOVF 0xfe5, F, ACCESS
     03A12    52E5     MOVF 0xfe5, F, ACCESS
    291:               	}	
    292:               	else if	(test_data == 4)
     03A14    D02D     BRA 0x3a70
     03A16    0E04     MOVLW 0x4
     03A18    19FE     XORWF 0xfe, W, BANKED
     03A1A    E101     BNZ 0x3a1e
     03A1C    51FF     MOVF 0xff, W, BANKED
     03A1E    E10C     BNZ 0x3a38
    293:               	{
    294:               		puts("4");
     03A20    0E70     MOVLW 0x70
     03A22    6EE6     MOVWF 0xfe6, ACCESS
     03A24    0E4E     MOVLW 0x4e
     03A26    6EE6     MOVWF 0xfe6, ACCESS
     03A28    0E00     MOVLW 0
     03A2A    6EE6     MOVWF 0xfe6, ACCESS
     03A2C    ECFA     CALL 0x53f4, 0
     03A2E    F029     NOP
     03A30    52E5     MOVF 0xfe5, F, ACCESS
     03A32    52E5     MOVF 0xfe5, F, ACCESS
     03A34    52E5     MOVF 0xfe5, F, ACCESS
    295:               	}
    296:               	else if	(test_data == 5)
     03A36    D01C     BRA 0x3a70
     03A38    0E05     MOVLW 0x5
     03A3A    19FE     XORWF 0xfe, W, BANKED
     03A3C    E101     BNZ 0x3a40
     03A3E    51FF     MOVF 0xff, W, BANKED
     03A40    E10C     BNZ 0x3a5a
    297:               	{
    298:               		puts("5");
     03A42    0E6E     MOVLW 0x6e
     03A44    6EE6     MOVWF 0xfe6, ACCESS
     03A46    0E4E     MOVLW 0x4e
     03A48    6EE6     MOVWF 0xfe6, ACCESS
     03A4A    0E00     MOVLW 0
     03A4C    6EE6     MOVWF 0xfe6, ACCESS
     03A4E    ECFA     CALL 0x53f4, 0
     03A50    F029     NOP
     03A52    52E5     MOVF 0xfe5, F, ACCESS
     03A54    52E5     MOVF 0xfe5, F, ACCESS
     03A56    52E5     MOVF 0xfe5, F, ACCESS
    299:               	}
    300:               	else 
     03A58    D00B     BRA 0x3a70
    301:               	{
    302:               		puts("?");
     03A5A    0E6C     MOVLW 0x6c
     03A5C    6EE6     MOVWF 0xfe6, ACCESS
     03A5E    0E4E     MOVLW 0x4e
     03A60    6EE6     MOVWF 0xfe6, ACCESS
     03A62    0E00     MOVLW 0
     03A64    6EE6     MOVWF 0xfe6, ACCESS
     03A66    ECFA     CALL 0x53f4, 0
     03A68    F029     NOP
     03A6A    52E5     MOVF 0xfe5, F, ACCESS
     03A6C    52E5     MOVF 0xfe5, F, ACCESS
     03A6E    52E5     MOVF 0xfe5, F, ACCESS
    303:               	}
    304:               	
    305:               	switch (test_data)
     03A70    0E05     MOVLW 0x5
     03A72    0102     MOVLB 0x2
     03A74    19FE     XORWF 0xfe, W, BANKED
     03A76    E101     BNZ 0x3a7a
     03A78    51FF     MOVF 0xff, W, BANKED
     03A7A    E045     BZ 0x3b06
     03A7C    0E04     MOVLW 0x4
     03A7E    19FE     XORWF 0xfe, W, BANKED
     03A80    E101     BNZ 0x3a84
     03A82    51FF     MOVF 0xff, W, BANKED
     03A84    E034     BZ 0x3aee
     03A86    0E03     MOVLW 0x3
     03A88    19FE     XORWF 0xfe, W, BANKED
     03A8A    E101     BNZ 0x3a8e
     03A8C    51FF     MOVF 0xff, W, BANKED
     03A8E    E023     BZ 0x3ad6
     03A90    0E02     MOVLW 0x2
     03A92    19FE     XORWF 0xfe, W, BANKED
     03A94    E101     BNZ 0x3a98
     03A96    51FF     MOVF 0xff, W, BANKED
     03A98    E012     BZ 0x3abe
     03A9A    0E01     MOVLW 0x1
     03A9C    19FE     XORWF 0xfe, W, BANKED
     03A9E    E101     BNZ 0x3aa2
     03AA0    51FF     MOVF 0xff, W, BANKED
     03AA2    E001     BZ 0x3aa6
     03AA4    D03C     BRA 0x3b1e
    306:               	{ 
    307:               		case 1:
    308:               			puts("1");
     03AA6    0E76     MOVLW 0x76
     03AA8    6EE6     MOVWF 0xfe6, ACCESS
     03AAA    0E4E     MOVLW 0x4e
     03AAC    6EE6     MOVWF 0xfe6, ACCESS
     03AAE    0E00     MOVLW 0
     03AB0    6EE6     MOVWF 0xfe6, ACCESS
     03AB2    ECFA     CALL 0x53f4, 0
     03AB4    F029     NOP
     03AB6    52E5     MOVF 0xfe5, F, ACCESS
     03AB8    52E5     MOVF 0xfe5, F, ACCESS
     03ABA    52E5     MOVF 0xfe5, F, ACCESS
    309:               			break;
     03ABC    D03B     BRA 0x3b34
    310:               			
    311:               		case 2:
    312:               			puts("2");
     03ABE    0E74     MOVLW 0x74
     03AC0    6EE6     MOVWF 0xfe6, ACCESS
     03AC2    0E4E     MOVLW 0x4e
     03AC4    6EE6     MOVWF 0xfe6, ACCESS
     03AC6    0E00     MOVLW 0
     03AC8    6EE6     MOVWF 0xfe6, ACCESS
     03ACA    ECFA     CALL 0x53f4, 0
     03ACC    F029     NOP
     03ACE    52E5     MOVF 0xfe5, F, ACCESS
     03AD0    52E5     MOVF 0xfe5, F, ACCESS
     03AD2    52E5     MOVF 0xfe5, F, ACCESS
    313:               			break;
     03AD4    D02F     BRA 0x3b34
    314:               			
    315:               		case 3:
    316:               			puts("3");
     03AD6    0E72     MOVLW 0x72
     03AD8    6EE6     MOVWF 0xfe6, ACCESS
     03ADA    0E4E     MOVLW 0x4e
     03ADC    6EE6     MOVWF 0xfe6, ACCESS
     03ADE    0E00     MOVLW 0
     03AE0    6EE6     MOVWF 0xfe6, ACCESS
     03AE2    ECFA     CALL 0x53f4, 0
     03AE4    F029     NOP
     03AE6    52E5     MOVF 0xfe5, F, ACCESS
     03AE8    52E5     MOVF 0xfe5, F, ACCESS
     03AEA    52E5     MOVF 0xfe5, F, ACCESS
    317:               			break;
     03AEC    D023     BRA 0x3b34
    318:               			
    319:               		case 4:
    320:               			puts("4");
     03AEE    0E70     MOVLW 0x70
     03AF0    6EE6     MOVWF 0xfe6, ACCESS
     03AF2    0E4E     MOVLW 0x4e
     03AF4    6EE6     MOVWF 0xfe6, ACCESS
     03AF6    0E00     MOVLW 0
     03AF8    6EE6     MOVWF 0xfe6, ACCESS
     03AFA    ECFA     CALL 0x53f4, 0
     03AFC    F029     NOP
     03AFE    52E5     MOVF 0xfe5, F, ACCESS
     03B00    52E5     MOVF 0xfe5, F, ACCESS
     03B02    52E5     MOVF 0xfe5, F, ACCESS
    321:               			break;
     03B04    D017     BRA 0x3b34
    322:               			
    323:               		case 5:
    324:               			puts("5");
     03B06    0E6E     MOVLW 0x6e
     03B08    6EE6     MOVWF 0xfe6, ACCESS
     03B0A    0E4E     MOVLW 0x4e
     03B0C    6EE6     MOVWF 0xfe6, ACCESS
     03B0E    0E00     MOVLW 0
     03B10    6EE6     MOVWF 0xfe6, ACCESS
     03B12    ECFA     CALL 0x53f4, 0
     03B14    F029     NOP
     03B16    52E5     MOVF 0xfe5, F, ACCESS
     03B18    52E5     MOVF 0xfe5, F, ACCESS
     03B1A    52E5     MOVF 0xfe5, F, ACCESS
    325:               			break;
     03B1C    D00B     BRA 0x3b34
    326:               			
    327:               		default:
    328:               			puts("?");
     03B1E    0E6C     MOVLW 0x6c
     03B20    6EE6     MOVWF 0xfe6, ACCESS
     03B22    0E4E     MOVLW 0x4e
     03B24    6EE6     MOVWF 0xfe6, ACCESS
     03B26    0E00     MOVLW 0
     03B28    6EE6     MOVWF 0xfe6, ACCESS
     03B2A    ECFA     CALL 0x53f4, 0
     03B2C    F029     NOP
     03B2E    52E5     MOVF 0xfe5, F, ACCESS
     03B30    52E5     MOVF 0xfe5, F, ACCESS
     03B32    52E5     MOVF 0xfe5, F, ACCESS
    329:               	}
    Fact - Beethoven wrote his first symphony in C

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 08-25-2008, 12:01 PM
  2. noob question re: switch case & loops
    By scooglygoogly in forum C++ Programming
    Replies: 13
    Last Post: 08-25-2008, 11:08 AM
  3. switch case
    By Raeliean in forum C++ Programming
    Replies: 1
    Last Post: 07-04-2005, 10:29 PM
  4. Switch case
    By DeepFyre in forum C++ Programming
    Replies: 3
    Last Post: 09-25-2004, 06:29 PM
  5. Switch Case vs if else
    By WebSnozz in forum C++ Programming
    Replies: 15
    Last Post: 01-28-2003, 08:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21