switch { } need help

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.

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?

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

Ok, thanks.

Good.

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

:frowning:

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

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.

So if I have:

(indulge me)

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?

lost_and_confused:
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.

lost_and_confused:
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 }.

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"

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?

“break” means, break out of this bit of code.

take this for example:

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.

int j = 2
switch (j)
{
     case 1:
     //  blah
     break;

    case 2:
    // blah
    break;

    case 3:
    // blah
    break;

}

could be equally written as:

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:

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:

   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.)

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. :frowning:

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

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.

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.

I think the point there was that there is no "Arduino Language". 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.

PeterH:

[quote author=Tom Carpenter link=topic=119430.msg899406#msg899406 date=1345559635]
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.
[/quote]

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

retrolefty:
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

That same logic could be used to argue that FORTRAN is C because f2c exists. The fact is that pre-processing CAN break things so that the language is not C or C++ any more.

In my opinion, the amount of pre-processing done by the Arduino front-end is a trifle heavy handed. A few of the things it does are downright annoying -- like the way it misshandles things when there is a CPP directive it doesn't understand at the top of your file. But I would grant that the resulting language still counts as C/C++ in this case.

retrolefty:
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.

The differences between Wiring and C++ are small but IMO significant. The fact that what's compiled is not (quite) the same as your sketch matters when you're trying to understand why what looks like valid C++ doesn't compile, and would also matter if you took your experience with Wiring to a genuine C++ environment and expected it to work the same.

PeterH:

retrolefty:
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.

The differences between Wiring and C++ are small but IMO significant. The fact that what's compiled is not (quite) the same as your sketch matters when you're trying to understand why what looks like valid C++ doesn't compile, and would also matter if you took your experience with Wiring to a genuine C++ environment and expected it to work the same.

I don't understand your point. Isn't Wiring just a C function. At least that is what I see when I look at the Arduino core source files.

retrolefty:
Isn't Wiring just a C function. At least that is what I see when I look at the Arduino core source files.

"Wiring" is the name of the language/environment that is implemented for Arduino development.

In practice, it is just C with a dash of C++, some training wheels, and some bits and bobs whose purpose seems mainly to confuse folks who are used to C or C++, so I can understand your confusion.

gardner:

retrolefty:
Isn't Wiring just a C function. At least that is what I see when I look at the Arduino core source files.

"Wiring" is the name of the language/environment that is implemented for Arduino development.
Wiring (development platform) - Wikipedia

In practice, it is just C with a dash of C++, some training wheels, and some bits and bobs whose purpose seems mainly to confuse folks who are used to C or C++, so I can understand your confusion.

Calling Wiring a programming language is just wrong. It may be a helpful way to explain to raw beginners how the IDE, their sketches, and the arduino hardware work together but it's an improper usage of 'programming language', at least in my opinion.

Lefty