how to exit from if-else

Go To Last Post
13 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have something like this.

 void function(void)
 {
	if (a) {
		if (b) {
			// do something 1
		} else {
			// do something 2
		}
	}
	
	if (c) {
		if (d) {
			// do something 3
		}  else {
			// do something 4
		}
	}
	
	if (e) {
		if (f) {
			// do something 5
		} else {
			// do something 6
		}
	}
 }

The problem is if "something 1" is already executed, I want to exit the function. I don't need to check the succeeding ifs. The same goes for all the odd-numbered somethings.

I am thinking of putting return(0)'s there but that would make my function non-void. And i don't really need it to return anything.

Can i call a non-void function without equating it to anything?
e.g.
uint8_t function(void);

function();
and not
foo = function();

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
void function(void)
 {
   if (a) {
      if (b) {
         // do something 1
         return; // a and b are true. exit function 
      } else {
         // do something 2
      }
   }
   
   if (c) {
      if (d) {
         // do something 3
      }  else {
         // do something 4
      }
   }
   
   if (e) {
      if (f) {
         // do something 5
      } else {
         // do something 6
      }
   }
 } 

this should do the trick.
note I only added the return statement to 1

but.....
are you not better of with a case statement instead of a wood of if else statements?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Read a C book or online reference and write "else" before "if (b)" and before "if (c)", or just use "return" so it just returns without anything and you can have a void function.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

actually this is a big checking of io port's states.
the io ports are left over from other peripherals so they are scattered and not lumped together in a single port.

 void function(void)
 {
	if (PIN_A1) {
		if (PIN_B2) {
			// do something 1
		} else {
			// do something 2
		}
	}
	
	if (PIN_G3) {
		if (PIN_C3) {
			// do something 3
		}  else {
			// do something 4
		}
	}
	
	if (PIN_D1) {
		if (PIN_D2) {
			// do something 5
		} else {
			// do something 6
		}
	}
 }

Quote:
this should do the trick.
note I only added the return statement to 1

b-b-but there is a warning
Quote:
Warning 1 'return' with a value, in function returning void

EDIT:
return (without anything) did the trick. thanks!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Just a word of warning. Having multiple return; from a function can be as confusing as using goto. Another approach would be:

 void function(void)
 {
   bool havent_done_something = true;
   if (a) {
      if (b) {
         // do something 1
      } else {
         // do something 2
      }
      havent_done_something = false;
   }
   
   if (c && havent_done_something) {
      if (d) {
         // do something 3
      }  else {
         // do something 4
      }
      havent_done_something = false;
   }
   
   if (e && havent_done_something) {
      if (f) {
         // do something 5
      } else {
         // do something 6
      }
   }
 } 

or as Jepael said simply:

 void function(void)
 {
   if (a) {
      if (b) {
         // do something 1
      } else {
         // do something 2
      }
   }
   else if (c) {
      if (d) {
         // do something 3
      }  else {
         // do something 4
      }
   }   
   else if (e) {
      if (f) {
         // do something 5
      } else {
         // do something 6
      }
   }
 } 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Cliff wrote:

void function(void) 
 { 
   bool havent_done_something = true; 
   if (a) { 
      if (b) { 
         // do something 1 
         havent_done_something = false; 
      } else { 
         // do something 2 
      } 
   } 
    
   if (c && havent_done_something) { 
      if (d) { 
         // do something 3 
         havent_done_something = false; 
      }  else { 
         // do something 4 
      } 
   } 
    
   if (e && havent_done_something) { 
      if (f) { 
         // do something 5 
      } else { 
         // do something 6 
      } 
   } 
 } 


Am I blind today or would that simply be

 void function(void) 
 { 
   if (a) { 
      if (b) { 
         // do something 1 
      } else { 
         // do something 2 
      } 
   } 
   else if (c) { 
      if (d) { 
         // do something 3 
      }  else { 
         // do something 4 
      } 
   } 
   else if (e) { 
      if (f) { 
         // do something 5 
      } else { 
         // do something 6 
      } 
   } 
 }

?

And anyway, this does not solve the OPs stated problem. This might:

void function(void) 
 { 
   bool havent_done_something = true; 
   if (a) { 
      if (b) { 
         // do something 1 
      } else { 
         // do something 2 
      } 
      havent_done_something = false; 
   } 
    
   if (c && havent_done_something) { 
      if (d) { 
         // do something 3 
      }  else { 
         // do something 4 
      } 
      havent_done_something = false; 
   } 
    
   if (e && havent_done_something) { 
      if (f) { 
         // do something 5 
      } else { 
         // do something 6 
      } 
   } 
 } 

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

Last Edited: Tue. Jun 18, 2013 - 10:12 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Gee I wish I'd said that ;-)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Another approach:

void foo()
{
  do{
    if( a ){
      // do something
      break;
    }
    if( b ){
      // do something
      break;
    }
    if( c ){
      // do something
      break;
    }
  }while( 0 );
}

Peter

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

Gee I wish I'd said that

Well, I believe I spotted a mistake in your suggestion. Then when I posted my response I made a mistake when editing it. It should go like this:

--------------------

Cliff wrote:

void function(void) 
 { 
   bool havent_done_something = true; 
   if (a) { 
      if (b) { 
         // do something 1 
      } else { 
         // do something 2 
      } 
      havent_done_something = false; 
   } 
    
   if (c && havent_done_something) { 
      if (d) { 
         // do something 3 
      }  else { 
         // do something 4 
      } 
      havent_done_something = false; 
   } 
    
   if (e && havent_done_something) { 
      if (f) { 
         // do something 5 
      } else { 
         // do something 6 
      } 
   } 
 } 


Am I blind today or would that simply be

 void function(void) 
 { 
   if (a) { 
      if (b) { 
         // do something 1 
      } else { 
         // do something 2 
      } 
   } 
   else if (c) { 
      if (d) { 
         // do something 3 
      }  else { 
         // do something 4 
      } 
   } 
   else if (e) { 
      if (f) { 
         // do something 5 
      } else { 
         // do something 6 
      } 
   } 
 }

?

And anyway, this does not solve the OPs stated problem. This might:

void function(void) 
 { 
   bool havent_done_something = true; 
   if (a) { 
      if (b) { 
         // do something 1 
         havent_done_something = false; 
      } else { 
         // do something 2 
      } 
   } 
    
   if (c && havent_done_something) { 
      if (d) { 
         // do something 3 
         havent_done_something = false; 
      }  else { 
         // do something 4 
      } 
   } 
    
   if (e && havent_done_something) { 
      if (f) { 
         // do something 5 
      } else { 
         // do something 6 
      } 
   } 
 }

--------------------

Now then.. Am I blind?

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

actually this is a big checking of io port's states.
the io ports are left over from other peripherals so they are scattered and not lumped together in a single port.


Very probably, I'd be entering the state of these inputs into a debounce routine. So I gather them together in the "fetch" routine, often every 10ms, and enter that value (usually either 8 or 16 bits) into the debounce sequence.

Then, operations work off a mask. Granted, for a few cases a switch()-generated code sequence may look a lot like the repeated/nested if().

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
or as Jepael said simply:
The 'else if' solutions will not work. The exit from the function must be when both 'a' and 'b' are true. The else if only looks at 'a'.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Very informative!

> yes, else-if will not work for my case

> i love the idea of the do-while, break

> yes i am entering them in a debounce routine but i have a variable for each io e.g. raw_state = PIN_A1, state = debounce(raw_state)
it did not occur to me to just lump them in one variable and just mask them.
I will check this in the simulator!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

> yes i am entering them in a debounce routine but i have a variable for each io e.g. raw_state = PIN_A1, state = debounce(raw_state)
it did not occur to me to just lump them in one variable and just mask them.


//
// **************************************************************************
// *
// *		K E Y _ F E T C H
// *
// **************************************************************************
//
//
//
unsigned char				key_fetch			(void)
{
unsigned char 		work;			// working value of inputs
unsigned char 		work2;			// working value of inputs

#if 0
// Inputs

#define	SW1				PIND.6
#define	SW2				PIND.5
#define	SW3				PIND.4
#define	SW4				PIND.3

// Masks into input_*
#define	MASK_SW4	0x02	// UP
#define	MASK_SW3	0x04	// DOWN
#define	MASK_SW2	0x08	// SET
#define	MASK_SW1	0x10	// MENU

#define	MASK_SW		(MASK_SW1 | MASK_SW2 | MASK_SW3 | MASK_SW4)

// Alternate names for above
#define	SW_MENU		MASK_SW1
#define	SW_SET		MASK_SW2
#define	SW_DOWN		MASK_SW3
#define	SW_UP		MASK_SW4

// Trigger inputs, active low
#define	TR1				PINA.7
#define	TR2				PINA.6
#define	TR3				PINA.5
#define	TR4				PINB.1

// Masks into input_*
#define	MASK_TR4	0x01	//
#define	MASK_TR3	0x20	//
#define	MASK_TR2	0x40	//
#define	MASK_TR1	0x80	//

#endif

// Four buttons
	work = ~PIND;
	work >>= 2;
	work &= MASK_SW;

// Four trigger inputs
	work2 = ~PINA;
	work2 &= 0xe0;	// high 3 bits
	// Low bit
	if (TR4 == 0)
		{
		// Active low
		work2 |= 1;
		}
	work |= work2;


			// 8 inputs, TR1-TR2-TR3-MENU-SET-DOWN-UP-TR4
	return (work);
}
...
// ***********
// Inputs
// ***********
//

		// Fetch input state every 10ms.
		input_prev = input_current;	// for calculating edges

 		scratch = key_fetch();		// strobe and process switch inputs

		input_current = signal_debounce(scratch, inputs);		//  and create edge triggers & current state

// Rising & falling edges
//
// Note:  no edges.  If needed:
//	Rising & falling edges = composite_io ^ previous_io;
//	Rising edges = (composite_io ^ previous_io) & composite_io;
//	Falling edges = (composite_io ^ previous_io) & previous_io;
//
//		rising_io = (composite_io ^ previous_io) & composite_io;
//		falling_io = (composite_io ^ previous_io) & previous_io;

		input_rising = (input_current ^ input_prev) & input_current;
//		input_falling = (input_current ^ input_prev) & input_prev;
...
...
// Quick-and-dirty calibration
// ***************************
			if (input_current & SW_SET)
				{
				// SET key held down, and ...
				if (input_rising & SW_DOWN)
					{
					// DOWN key pressed, and ...

...
					}

				if (input_rising & SW_UP)
					{
					// UP key pressed, and ...

...
					}

				}

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.