Computer Science, asked by AVINASHKUMARGAURAV, 1 year ago

write the uses of switch statement.

Answers

Answered by pragyakata
1
Switch statements are used when you clearly know what are possible values for the condition variable. 
Each value in that list of possible value is called case. 
When the value given in input matches the value in the case statement, the block of code below case gets executed until it reaches the breakstatement.
Break is optional. If break statement is not given, the next case statement (if any) will also get executed.

for example, consider the following code:

#include <stdio.h>
 int main ()

 char grade = 'B'; 
   switch(grade)   { 
   case 'A' :
             printf("Excellent!\n" ); 
             break; 
   case 'B' : 
             printf("Well done\n" ); 
   case 'C' : 
              printf("Good\n" ); 
              break; 
   case 'D' : 
              printf("You passed\n" ); 
              break; 
   case 'F' : 
             printf("Better try again\n" ); 
             break; 
   default : 
              printf("Invalid grade\n" ); 
   } 
   printf("Your grade is  %c\n", grade );
     return 0; 
}

The output of the above program is :
Well done
Good
Your grade is B


The value of the variable passed to the switch statement is checked against the case statements. 
Case 'B' matches. Hence the printf() executed. (Well Done - printed).
As there is no break statement, the next case 'C' also executed. (Good - printed). As there is 'break' statement, the statement after the switch is executed. Therefore 'Your grade is B' - printed.


An if statement can be followed by an optional else if...else statement, which is very useful to test various conditions. Once the condition is true, other conditions are not validated.
In switch case, mostly constant values are given in the condition while in if..else mostly boolean expressions are given. 


for example,

#include <stdio.h>
int main ()

int a=10;
int b=20;

if (a>100)
    {
     printf("Value of a is greater than 100");
    }
elseif (b==20)
    {
     printf("Value of b is 20");
     }
elseif (a<100)
    {
     printf("Value of a is less than 100");
    }
else
   {
    printf("Condition fails");
    }
printf("Value of a is %d", a);
return 0;
}


Output of above program is:
Value of b is 20
Value of a is 10

The first if condition fails, so second if is checked.
The condition is true, so the statements inside that if gets executed.
The next if statements are not executed even if the condition is true.


Hope it will help you

champyash: hi
pragyakata: what is your name bro
champyash: yash
champyash: but why
pragyakata: only chatting
champyash: ok
champyash: why chatting
pragyakata: ok fine
champyash: bye
pragyakata: bye bye
Answered by champyash
1
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 a multiway branch.

Switch statements function somewhat similar to the if statement used in programming languages like C/C++, C#, Visual Basic .NET, Java and exists in most high-level imperative programming languages such as Pascal, Ada, C/C++, C#, Visual Basic .NET, Java, and in many other types of language, using such keywords as switch, case, selector inspect.

Switch statements come in two main variants: a structured switch, as in Pascal, which takes exactly one branch, and an unstructured switch, as in C, which functions as a type of goto. The main reasons for using a switch include improving clarity, by reducing otherwise repetitive coding, and (if the heuristics permit) also offering the potential for faster execution through easier compiler optimization in many cases.

plz mark this as brainliest

champyash: i need brainliest answer...plz bro i need it
champyash: plz plz mark my answer as brainliest plz bro
pragyakata: here bro don't beg
Similar questions