Contenido del Curso
C++ Introduction
4. Introduction to Program Flow
5. Introduction to Functions
C++ Introduction
Switch Statement
The switch-case construct allows you to compare the result of an expression against a set of predefined values. Structure of switch-case:
main.cpp
break
- statement means an exit from a block of code;default
- is an optional part but a useful one. This part will be executed if none of the cases doesn't fit.
In our case, we check the variable
, if it is equal to 5
, then the corresponding text will be displayed and, using the break
statement, the program flow will leave the entire switch-case
construction, and there will be no processing of other cases.
But the switch
statement has one caveat. We intentionally remove the break
statement:
main.cpp
Without the break
command, the program flow will ignore all the following checks and simply execute the commands of the following cases until it encounters the break
statement or the end of the entire switch
block.
¿Todo estuvo claro?
Contenido del Curso
C++ Introduction
4. Introduction to Program Flow
5. Introduction to Functions
C++ Introduction
Switch Statement
The switch-case construct allows you to compare the result of an expression against a set of predefined values. Structure of switch-case:
main.cpp
break
- statement means an exit from a block of code;default
- is an optional part but a useful one. This part will be executed if none of the cases doesn't fit.
In our case, we check the variable
, if it is equal to 5
, then the corresponding text will be displayed and, using the break
statement, the program flow will leave the entire switch-case
construction, and there will be no processing of other cases.
But the switch
statement has one caveat. We intentionally remove the break
statement:
main.cpp
Without the break
command, the program flow will ignore all the following checks and simply execute the commands of the following cases until it encounters the break
statement or the end of the entire switch
block.
¿Todo estuvo claro?