Hello, people!
I would like to understand how switch case works if it is used inside a function that is called with in a switch case within a main loop.
For example
setup
main_state=0;
states_Japan=0;
loop {
switch case (main_state) {
case 0:
gotoJapan();
main_state=1;
break;
case 1:
gotoChina();
main_state=2;
break;
case 2:
gotoUSA();
main_state=0;
}
}
void gotoJapan () {
switch case (states_Japan) {
case 0:
buytickets();
states_Japan = 1;
break;
case 1:
checkbaggage();
states_Japan = 2;
break;
case 2:
taketheairplane();
break;
}
}
What I want to know is:
Suppose I'm within the function gotoJapan(). If I'm within the case 0 from the switch case, I run the other function buytickets() and when it ends, I call the next case (case 1) to be run. How is it work? It returns at the top of the switch cases and goes to the case 1?
Considering the same function -gotoJapan(), suppose I'm within the case 2 from the switch case and I finish to execute the taketheairplane() function. I don't put any transition code. In this case, what happens? Will the code return to the main loop (case 0 - switch case) and continue to execute the rest of the loope code?
You are not calling the state to be run, you are setting a variable that will run that state the next time you call the function. That also answers your question - it will happen the next time you run the switch statement from the top.
No, it will execute the same state again and again until you change the state variable to something different.
Think of a switch statement as being a 'jump' the the required state label that will execute all the code thereunder until it reaches a 'break', at which point it 'jumps' to the end of the switch statement brace. If you omit the 'break' the code will continue to execute until it finds a break. This is often a source of bugs but you can use it deliberately to your advantage. If you do just make sure there are comments about the fact there is deliberately no break or it will be confusing to everyone else.
You are not calling the state to be run, you are setting a variable that will run that state the next time you call the function. That also answers your question - it will happen the next time you run the switch statement from the top.
So, what will it be the next time I will run the switch statement from the top? It's a quite confusing to me!
marco_c:
2) No, it will execute the same state again and again until you change the state variable to something different.
So, considering that I'm an in a switch case within a function outside the loop, how can I return to the main loop after ending a specific case of the switch case?
My friend @gcjr, what is the effect of using return in a function that contains switch case and is outside the main loop?
And regarding to a the main_state=3 and the default break within a switch case in the main loop? What does it do?
luciocr:
My friend @gcjr, what is the effect of using return in a function that contains switch case and is outside the main loop?
The effect of return is ALWAYS exactly the same, no matter where or when it's used - the program immediately exits the currently executing function, and returns to whatever function called the currently executing function. ALWAYS.
RayLivingston:
The effect of return is ALWAYS exactly the same, no matter where or when it's used - the program immediately exits the currently executing function, and returns to whatever function called the currently executing function. ALWAYS.
And what's the effect of a break within a switch case inside a function outside main loop?
For example, I have the following situation:
void gotoJapan() {
switch case (states_Japan) {
case 0:
buytickets();
states_Japan = 1;
break;
case 1:
checkbaggage();
states_Japan = 2;
break;
case 2:
taketheairplane();
if (money >1000) {
states_Japan =3;
}
states_Japan=4;
break;
case 3:
7starshotel();
return;
case 4:
5starshotel();
return;
}
For example, what I want is when I end to run the case 1, I want to go to the case 2, run it and depends on the situation (in this case, my money) I want to go to case 3 or 4! How do I do it in this sequence within a function outside main loop?
gcjr:
a switch statement is the equivalent of a if/else if/else if/...
a "break" exits the case, continuing execution at the end of the switch (the closing "}");
if there is no break, it will continue thru the next case
if there's a return, it exits the routine.
That's nice, my friend, @gcjr! It is clear to me now!
Only one more doubt:
If I'm inside a function gotoJapan(), for example, and I run the case 2 (in the switch case), set the variable to the next case (case 3 or 4, depends on the amount of money I have) and put the command break at the end! So, the break will end the switch case within a function! But how do I do to go to the case 3 in this same switch case within the same function outside the main loop?
The code I'm saying is in the previous reply of me.
For me here, I finish the case 2 and automatically go to the case 3 or 4 within the same switch case!
I understand that the break does the code return to the top of the function that I'm it - gotoJapan().
Am I right?
luciocr:
For me here, I finish the case 2 and automatically go to the case 3 or 4 within the same switch case!
I understand that the break does the code return to the top of the function that I'm it - gotoJapan().
Am I right?
Changing the value of the state variable does NOTHING until the switch() is executed again. There is no magic in programming. Things do not magically happen in the background. What happens is whatever action the code being executed performs. Changing the state variable does not magically make the code jump to another case. It simply changes the value stored in that variable. Nothing more. If you change the state variable, and want to then perform the action associated with that state, YOU have to explicitly write code to make execution return to the switch() statement, and re-execute it. If absolutely necessary, you can put the switch statement in a while loop that exits only when a specific state is reached, though in many cases, that is probably the wrong approach...
luciocr:
For me here, I finish the case 2 and automatically go to the case 3 or 4 within the same switch case!
I understand that the break does the code return to the top of the function that I'm it - gotoJapan().
Am I right?
if (money >1000) {
states_Japan =3;
}
states_Japan=4;
break;
Let’s assume this is true:
if (money >1000)
We will execute this line of code:
states_Japan =3;
But, we then execute this line of code:
states_Japan=4;
As a result, states_Japan =3; never really does anything :(.