Pages: [1] 2   Go Down
Author Topic: switch { } need help  (Read 1254 times)
0 Members and 1 Guest are viewing this topic.
Oz
Offline Offline
God Member
*****
Karma: 4
Posts: 727
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi again.

I'm going through a C++ book trying to learn.

Anyway the problem with which I am stuck is the SWITCH command.

Though an easy concept I think I am not understanding it too good.

Code:
int j = 2
swtich j
{
     case 1:
     //  blah
     break;

    case 2:
    // blah
    break;

    case 3:
    // blah
    break;

}



Do I need the extra line/s:
default:
break;

in there to make it work?

What will happen if they are not in the code?


« Last Edit: August 20, 2012, 05:11:28 pm by lost_and_confused » Logged

Chile
Offline Offline
Edison Member
*
Karma: 35
Posts: 1251
Arduino rocks?
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Default is not needed, if not present, and none of your cases matchs, nothing will be executed by that switch.
Logged

My website: http://ried.cl

Oz
Offline Offline
God Member
*****
Karma: 4
Posts: 727
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok, thanks.

Good.

Now I have to work out what is wrong with some part of my code.

smiley-sad

I was kind of hoping it may be something to do with switch{ } but seems it isn't.

Logged

Manchester (England England)
Online Online
Brattain Member
*****
Karma: 619
Posts: 33966
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You need the break word to stop the code going further.
If you leave it out, and you can, then after doing the match code it will fall through and do the code following. If you use it in this way it is a good idea to comment the fact that the break is deliberately missing.
Logged

Oz
Offline Offline
God Member
*****
Karma: 4
Posts: 727
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So if I have:

(indulge me)

Code:
switch j
{
    case 1:
    // blah 1

    case 2:
    // blah 2

    case 3:
    // blah 3

}


and j = 1, if there is no break after each of the blah things, why/how would blah 2 and 3 be executed?

I can understand (not really) that break; stops the program continuing to look for cases where it matches
and so "saves time", but is there more to this than I am seeing?

Logged

California
Online Online
Faraday Member
**
Karma: 88
Posts: 3376
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

why/how would blah 2 and 3 be executed?

Because that's how switch statements work.

When it finds the valid case, it jumps to that point in the code and continues down until it reaches a break, or the end.
« Last Edit: August 20, 2012, 05:54:24 pm by Arrch » Logged

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


and j = 1, if there is no break after each of the blah things, why/how would blah 2 and 3 be executed?
The switch statement says "Go to this point if j is 1, this point if j is 2, or this point if j is 3." Once there, it will keep executing code, going down, until it either hits the } or a break -- the "break" sends it to the }.

Quote
I can understand (not really) that break; stops the program continuing to look for cases where it matches
and so "saves time", but is there more to this than I am seeing?

The break just says "skip over the rest"
Logged

Oz
Offline Offline
God Member
*****
Karma: 4
Posts: 727
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So basically break; is simiar to "else"?

It worries me how long it has been since I have programmed and all these "quirks" I used to read around and now can't.

So what is the difference between swtich{ }
and
if( )
{
}
?

Is it just layout, or again:  am I missing something?
Logged

Leeds, UK
Offline Offline
Edison Member
*
Karma: 80
Posts: 1726
Once the magic blue smoke is released, it won't go back in!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

"break" means, break out of this bit of code.

take this for example:

Code:
int i = 0;
while(1){
  i++;
  if (i == 10){
    break; //when i = 10, the break statement is executed and the code jumps...
  }
}
//... to here.


Switches are just a convinient way of performing actions depending on the value of a variable.
Code:
int j = 2
switch (j)
{
     case 1:
     //  blah
     break;

    case 2:
    // blah
    break;

    case 3:
    // blah
    break;

}
could be equally written as:
Code:
int j = 2
if (j == 1){
     //  blah
} else if (j == 2){
    // blah
} else if (j == 3){
    // blah
}

The two differ when you end up with other operators such as > or <. This works as an if, but not as a switch:
Code:
int j = 2
if (j <= 1){
     //  blah
} else if (j <= 2){
    // blah
} else if (j <= 10){
    // blah
}

Another use of a switch, which can't be done with if() is something like this:
Code:
  switch (nodeOperation) {
        case  NSNodeNodeBNetA:
            //Swap them around so we can use the same code.
            tempNode = nodeA;
            nodeA = nodeB;
            nodeB = tempNode;
        case  NSNodeNodeANetB:
            
            //Do some stuff here...            

            break;
            
        case  NSNodeKeepMajor:
            if (nodeA.net.components.count >= nodeB.net.components.count) {
                tempNode = nodeA;
                nodeA = nodeB;
                nodeB = tempNode;
            }
            //the nodes are now backwards (node B should be where nodeA is and vice versa) but this will be corrected on fall through
        case  NSNodeNodeBNetB:
            //swap the nodes around to reduce the amount of code required.
            tempNode = nodeA;
            nodeA = nodeB;
            nodeB = tempNode;
        case  NSNodeNodeANetA:
            
            //do some stuff here.
            break;
            
        case  NSNodeWireMajor:
            if (nodeA.net.components.count >= nodeB.net.components.count) {
                tempNode = nodeA;
                nodeA = nodeB;
                nodeB = tempNode;
            }
            //the nodes are now backwards (node B should be where nodeA is and vice versa) but this will be corrected on fall through
        case  NSNodeWireWithB:
            tempNode = nodeA;
            nodeA = nodeB;
            nodeB = tempNode;
        case  NSNodeWireWithA:

            //Do some stuff here.

            break;
            
        case  NSNodeDontMerge:
        default:
            return nil; //Cant do this operation as it isn't defined, also, the 'Dont Merge' option comes here
    }
By using a switch, I saved a lot of code, as all though there are 9 cases, those cases are essentially grouped into three, the difference being that nodeA and nodeB were switched. So by relying on fall-through as it is known (no break;), the nodes could be switched and use the same code.
(Oh, in case anyone recognises the NS reference in that last bit of code, it is actually written in objective-C, but I have stripped it down to a point which it is perfectly valid in C++ as well.)
« Last Edit: August 20, 2012, 06:35:11 pm by Tom Carpenter » Logged

~Tom~

Oz
Offline Offline
God Member
*****
Karma: 4
Posts: 727
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm starting to understand it.

I guess it is becuase when I did this sort of programming it was "Level 2 BASIC" on my old TRS-80 - for those of  you who can remember those things.

I did a bit of Z80 programming with it as well.

Then I did a bit of "AREXX" programming which was basically a version of REXX.

Had fun with that too, but the machine died and so that kind of stopped as well.   smiley-sad

Now I am into Arduino and there is a whole new set of commands I have to get my head around.
Logged

Offline Offline
God Member
*****
Karma: 17
Posts: 746
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What you're learning right now isn't Arduino.  What you are learning right now is C++ and there's a whole world of stuff you can do with that. 
Logged

Oz
Offline Offline
God Member
*****
Karma: 4
Posts: 727
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yeah, but the Arduino's language is pretty well C++, or similar.

I am trying to get my head around the code and rules/syntax and all that stuff.

It has been a long time and as I said:  The book takes a lot for granted even "for dummies".

Ah, onwards and upwards.

Logged

Leeds, UK
Offline Offline
Edison Member
*
Karma: 80
Posts: 1726
Once the magic blue smoke is released, it won't go back in!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I think the point there was that there is no "Arduino Language". Arduino IS C++.
Logged

~Tom~

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12631
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Arduino IS C++.

It more or less is, but it's not exactly the same. The Arduino IDE provides preprocessing of your source before it is compiled to automate some declarations for you, and the Wiring API is also specific to Arduino.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17293
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Arduino IS C++.

It more or less is, but it's not exactly the same. The Arduino IDE provides preprocessing of your source before it is compiled to automate some declarations for you, and the Wiring API is also specific to Arduino.

That is pretty much just arguing systematics in my opinion. The fact that the arduino IDE does some preprocessing and that the IDE has lots of supplied optional arduino functions does not mean it's not truly C++, because it in fact calls the gcc C++ compiler, so by definition it is truly and only pure C++ has defined by the gcc compiler.

Lefty
« Last Edit: August 21, 2012, 11:33:46 am by retrolefty » Logged

Pages: [1] 2   Go Up
Jump to: