1 What behavior do you observe from a program if you omit br

(1) What behavior do you observe from a program if you omit \"break\" statement at the end of case statements? Please describe in less than 100 words. (2) Propose a program that utilizes this behavior in less than 100 words.

Solution

(1) What behavior do you observe from a program if you omit \"break\" statement at the end of case statements? Please describe in less than 100 words.

Answer:

if yo omit \"break\" statement at the end of case statement, then next case also will execute which is wrong. if you write 4 cases in switch statement, you have to write \"break\" statment in all four cases. Otherwise the case which does not have break statement, that case will execute and afte that case, other cases also will execute until finds \"break\" statement in any other case. So we should provide the break statement in all cases.

(2) Propose a program that utilizes this behavior in less than 100 words.

Answer:

int i=0;

switch(i){

case 1: System.out.println(\"A\");

case 2: System.out.println(\"B\");

case 3: System.out.println(\"C\"); break;

case 4: System.out.println(\"D\");

default: System.out.println(\"Invalid number\");

}

output:

A

B

C

(1) What behavior do you observe from a program if you omit \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site