Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[General] Switch case doubt help

Status
Not open for further replies.

varunme

Advanced Member level 3
Joined
Aug 10, 2011
Messages
741
Helped
17
Reputation
36
Reaction score
17
Trophy points
1,318
Activity points
5,764
can we write a switch case statement as below?

Code:
char uart_rd;

switch (uart_rd )       // If data is received,
    {

	case 1:  char sequence[3]={0b11000011,0b11011011,0b11100011};
	case 2:  char sequence[3]={0b11000011,0b11111011,0b11100011};
	case 3:  char sequence[3]={0b11000011,0b11011011,0b11100011};
	case 4:  char sequence[3]={0b11010011,0b11011011,0b11110011};
    }
 

I don't see why not. If 'uart_rd' is known value at the point of entry and you can place any valid instructions in each 'case' statement.

Things to watch for are:
1. what happens if uart_rd is NOT 1, 2, 3, or 4. Should you have a 'default:' to trap other values.
2. you might want to put a 'break' instruction after each 'case' line. Some compilers will wastefully check all the following lines for a match, some will skip them.

Brian.
 
  • Like
Reactions: varunme

    varunme

    Points: 2
    Helpful Answer Positive Rating
2. you might want to put a 'break' instruction after each 'case' line. Some compilers will wastefully check all the following lines for a match, some will skip them.
If the compiler behaves according to the C specification (I hope, it does), the code execution will proceed downwards til a break statement or end of the switch construct. The switch expression works like a computed goto, the next case label doesn't change the execution flow.

you can place any valid instructions in each 'case' statement.
There are various syntax errors involved with the shown code:
- the case label must preceed a statement, not a variable declaration
- repeating a declaration within the same scope (e.g. code block) isn't allowed
- a declaration in the middle of a code block is C++ style and might be unsupported by the compiler
 
  • Like
Reactions: varunme

    varunme

    Points: 2
    Helpful Answer Positive Rating
I took that as being two sections of code, the declaration somewhere earlier and the switch statement inside main() or another function, just placed together in the example so we could see what kind of variable 'uart_rd' is. I take the point that 'sequence[]' should be declared earlier and it's value modified if necessary in the switch statement. I think if the language is C++ the redeclaration would be allowed, even if it would be pointless to do so.

I agree a 'break' is a good idea, some compilers will automatically insert one as part of the code optimization but adding one manually will always work.

Brian.
 
  • Like
Reactions: varunme

    varunme

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top