Switch Case - Break and Return inside a Function - Arduino

Asked

Viewed 248 times

2

Hello, good afternoon! I would like to understand how the switch case works in the Arduino C++ language. I have the following hypothetical situation:


estado_principal=0;
estados_Japão=0;

setup () {
}

loop {

switch case (estado_principal) {
    case 0:
    irparaJapão();
    estado_principal=1;
    break;
    
    case 1:
    irparaChina();
    estado_principal=2;
    break;
    
    case 2:
    irparaEUA();
    estado_principal=0;
}
}

void irparaJapão () {


switch case (estados_Japão) {
    
    case 0:
    comprarpassagem();
    estados_Japão = 1;
    break;
    
    case 1:
    checarbagagem();
    estados_Japão = 2;
    break;
    
    case 2:
    pegaroavião();
    return;

}
}

  1. In this situation, for me to exit the switch case inside the function will go to Zipan() and return to the next line of code in the main loop (in this case, state=1), just use the command Return, as I put inside the case 2 of the switch case inside the function will be Zipan()?

  2. If, for example, I am inside the case 1 of the switch case present in the function irparaJapan(), after running checkmatch(), on the next line I declare state_Japan=2 (in order to declare the value of the variable for the next run of the switch case). After that I put the break! What does this break do in my case? My goal is to stay in the function and run the case 2 of the switch case! How do I do it?

Thanks in advance!

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.