Computer Science, asked by elango03052003, 5 months ago

Program
Which of the following keywords is followed
by an integer or character constant?
a,for and b,case and c,void and d,switch​

Answers

Answered by sujatanawale
0

Answer:

Before we see how a switch case statement works in a C program, let’s checkout the syntax of it.

switch (variable or an integer expression)

{

case constant:

//C Statements

;

case constant:

//C Statements

;

default:

//C Statements

;

}

Flow Diagram of Switch Case

C switch case

Example of Switch Case in C

Let’s take a simple example to understand the working of a switch case statement in C program.

#include <stdio.h>

int main()

{

int num=2;

switch(num+2)

{

case 1:

printf("Case1: Value is: %d", num);

case 2:

printf("Case1: Value is: %d", num);

case 3:

printf("Case1: Value is: %d", num);

default:

printf("Default: Value is: %d", num);

}

return 0;

}

Output:

Default: value is: 2

Explanation: In switch I gave an expression, you can give variable also. I gave num+2, where num value is 2 and after addition the expression resulted 4. Since there is no case defined with value 4 the default case is executed.

Twist in a story – Introducing Break statement

Before we discuss more about break statement, guess the output of this C program.

#include <stdio.h>

int main()

{

int i=2;

switch (i)

{

case 1:

printf("Case1 ");

case 2:

printf("Case2 ");

case 3:

printf("Case3 ");

case 4:

printf("Case4 ");

default:

printf("Default ");

}

return 0;

}

Output:

Case2 Case3 Case4 Default

I passed a variable to switch, the value of the variable is 2 so the control jumped to the case 2, However there are no such statements in the above program which could break the flow after the execution of case 2. That’s the reason after case 2, all the subsequent cases and default statements got executed.

How to avoid this situation?

We can use break statement to break the flow of control after every case block.

Break statement in Switch Case

Break statements are useful when you want your program-flow to come out of the switch body. Whenever a break statement is encountered in the switch body, the control comes out of the switch case statement.

Example of Switch Case with break

I’m taking the same above that we have seen above but this time we are using break.

#include <stdio.h>

int main()

{

int i=2;

switch (i)

{

case 1:

printf("Case1 ");

break;

case 2:

printf("Case2 ");

break;

case 3:

printf("Case3 ");

break;

case 4:

printf("Case4 ");

break;

default:

printf("Default ");

}

return 0;

Answered by SharadSangha
0

Case is the keyword which is followed by an integer or character constant.

  • The case keyword indicates a statement in a switch statement.
  • A single statement can have multiple labels. we cannot use the case keyword outside a switch statement.
  • Case labels generally have no effect on the order in which the sub statements are executed inside a switch statement.
  • We generally use the break statement to exit from a switch statement.

                                                  For example:

switch(c) {case '+':

 p = add(q, r);

 break;

case '-':

 s = subtract(q, r);

 break;

}

Hence, the answer is option B.

#SPJ3

Similar questions