Thread: Can someone decipher the difference between this switch and an equivalent for?

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    1

    Can someone decipher the difference between this switch and an equivalent for?

    This one works as intended:

    Code:
    		if (clu.raw.butsnum) {
    			int i;
    			for (i=0;i<clu.raw.butsnum;i++){
    				if (clu.raw.buts[i] & RI_MOUSE_BUTTON_1_DOWN)
    					Com_QueueEvent( 0, SE_KEY, K_MOUSE1, qtrue, 0, NULL );
    				if (clu.raw.buts[i] & RI_MOUSE_BUTTON_2_DOWN)
    					Com_QueueEvent( 0, SE_KEY, K_MOUSE2, qtrue, 0, NULL );
    				if (clu.raw.buts[i] & RI_MOUSE_BUTTON_3_DOWN)
    					Com_QueueEvent( 0, SE_KEY, K_MOUSE3, qtrue, 0, NULL );
    				if (clu.raw.buts[i] & RI_MOUSE_BUTTON_4_DOWN)
    					Com_QueueEvent( 0, SE_KEY, K_MOUSE4, qtrue, 0, NULL );
    				if (clu.raw.buts[i] & RI_MOUSE_BUTTON_5_DOWN)
    					Com_QueueEvent( 0, SE_KEY, K_MOUSE5, qtrue, 0, NULL );
    				if (clu.raw.buts[i] & RI_MOUSE_BUTTON_1_UP)
    					Com_QueueEvent( 0, SE_KEY, K_MOUSE1, qfalse, 0, NULL );
    				if (clu.raw.buts[i] & RI_MOUSE_BUTTON_2_UP)
    					Com_QueueEvent( 0, SE_KEY, K_MOUSE2, qfalse, 0, NULL );
    				if (clu.raw.buts[i] & RI_MOUSE_BUTTON_3_UP)
    					Com_QueueEvent( 0, SE_KEY, K_MOUSE3, qfalse, 0, NULL );
    				if (clu.raw.buts[i] & RI_MOUSE_BUTTON_4_UP)
    					Com_QueueEvent( 0, SE_KEY, K_MOUSE4, qfalse, 0, NULL );
    				if (clu.raw.buts[i] & RI_MOUSE_BUTTON_5_UP)
    					Com_QueueEvent( 0, SE_KEY, K_MOUSE5, qfalse, 0, NULL );
    			}
    			clu.raw.butsnum = 0;
    		}

    but this one,

    Code:
    		if (clu.raw.butsnum) {
    			short int	down		= qfalse;
    			int			but	= 0, i;
    
    			for (i = 0; i < clu.raw.butsnum; i++) {
    				switch( clu.raw.buts[i]) {
    					case	 RI_MOUSE_BUTTON_1_DOWN:		down = qtrue;	but = K_MOUSE1; break;
    					case	 RI_MOUSE_BUTTON_2_DOWN:		down = qtrue;	but = K_MOUSE2; break;
    					case	 RI_MOUSE_BUTTON_3_DOWN:		down = qtrue;	but = K_MOUSE3; break;
    					case	 RI_MOUSE_BUTTON_4_DOWN:		down = qtrue;	but = K_MOUSE4; break;
    					case	 RI_MOUSE_BUTTON_5_DOWN:		down = qtrue;	but = K_MOUSE5; break;
    					case	 RI_MOUSE_BUTTON_1_UP:		down = qfalse;	but = K_MOUSE1; break;
    					case	 RI_MOUSE_BUTTON_2_UP:		down = qfalse;	but = K_MOUSE2; break;
    					case	 RI_MOUSE_BUTTON_3_UP:		down = qfalse;	but = K_MOUSE3; break;
    					case	 RI_MOUSE_BUTTON_4_UP:		down = qfalse;	but = K_MOUSE4; break;
    					case	 RI_MOUSE_BUTTON_5_UP:		down = qfalse;	but = K_MOUSE5; break;
    				}
    				Com_QueueEvent( 0, SE_KEY, but, down, 0, NULL );
    			}
    		}
    		clu.raw.butsnum = 0;
    ..sometimes "loses it" and if some of the "mouse buttons" (in that code) aren't pressed if they are hit too fast.

    Anyway, the relevance of the code itself I guess is little.

    Why do those pieces of code don't do exactly the same thing?
    Last edited by whatever; 07-18-2010 at 06:43 AM.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Note how they are all if statements. So it checks "all" states, while the case only checks ONE state.
    If RI_MOUSE_BUTTON_2_DOWN and RI_MOUSE_BUTTON_4_DOWN both are true, then the case will only execute the code for RI_MOUSE_BUTTON_2_DOWN and RI_MOUSE_BUTTON_4_DOWN, but the if statements will.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    If you just like using if statements, you could do one if with a crap load if else ifs(if that's even possible), but that would be ugly and I don't recommend it. I would be surprised if half the forum chastises me for even thinking of such a thing.

  4. #4
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    Because you're using bitwise ANDs in your first piece of code, I'll assume that clu.raw.buts[i] is a bitmask of one or more buttons. Switch cases only test for equality and so if more than one button is pressed then nothing at all will happen.

    (I realise that this doesn't differ much from Elysia's post, but I think there's a typo somewhere in her last sentence which makes it confusing)

Popular pages Recent additions subscribe to a feed