Computer Science, asked by sanzidawasi, 2 months ago

Demonstrate how 'switch statement is used in the programs
instead of 'if-else' statements with a suitable example
program​

Answers

Answered by mamtajain09101986
0

In computer programming languages, a switch statement is a type of selection control mechanism used to allow the value of a variable or expression to change the control flow of program execution via search and map.

Answered by XxDazzledSweetiexX
28

\huge\green{\sf{Question♡}}

Demonstrate how 'switch statement is used in the programs instead of 'if-else' statements with a suitable example program.

\huge\pink{\sf{Answer♡}}

\sf\underline{{Switch }{ \: statement \:  :  —}}

The switch is a special selective structure where the value of expression is matched by the list of integer or character constants, specified in the case statements. When a match is found, the statements specified within the case is executed until the break statement or end of Switch is found. Switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task, where none of the cases is true. No break statement is needed in the default case. It can be used in place of multiple concatenated if statements.

Note :

There is no limitations to the number of case statements that can be used within a single switch statement. Each case is followed by the value to be compared to and a colon (:).

Example of a Program :

The following program demonstrates the use of switch statement —

switch (x)

{

case 1:

System.out.printIn("you have chosen one");

break;

case 2:

System.out.printIn("you have chosen second");

break;

case 3:

System.out.printIn("you have chosen three");

break;

default:

System.out.printIn("invalid choice");

}

▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬

\sf{{Hope \: it\: }{helps \: you...♡}}

Similar questions