explain with the help of an example the purpose of default in a switch statement
Answers
Answer:
The purpose of the default in the switch case is to provide an option in case the conditions specified to trigger the execution of code within the other options does not occur so that the program will not crash.
For example lets say you have a case if the number 1 is entered by the user print ‘one’ to a display, and if number 2 print ‘two’ to the display. If the user enters any other number of which their is an infinite amount what would occur? Without the default statement the program would crash or make the user annoyed because they do not get any feedback from the program.
switch( x )
{
case 1 :
. . .
( print 'one')
. . .
case 2 :
. . .
( print 'two')
. . .
default:
(print ' invalid number entered')
}
If this answer was helpful, please mark it as the brainliest:)
Explanation:
- The purpose of the default in the switch case is to provide an option in case the conditions specified to trigger the execution of code within the other options does not occur so that the program will not crash(◠‿◕)