Notice: Undefined offset: 13918365 in /var/www/html/qa-external/qa-external-users.php on line 744
what is switch case - OnlineGDB Q&A
Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

what is switch case

+28 votes
asked Dec 3, 2024 by (220 points)

8 Answers

+2 votes
answered Dec 4, 2024 by Peter Minarik (101,360 points)
+6 votes
answered Dec 6, 2024 by comunaprograma (260 points)
switch case is used like if and else, but more simple, because u just need to atribute values to the cases, like

case 1 (begins if choseen variable is equal to 1)

his sintaxe is;

switch(variable_name):

case 1{
command here

break;}
case 2{

command here

break;}
+4 votes
answered Dec 6, 2024 by KJ Dudley (220 points)

It's kind of a shorthand way of testing for multiple possible values of a variable.

if (x == 1) {

    methodA();

} else if (x == 2) {

    methodB();

} else if (x == 3) {

    methodC();

} else if (x == 4) {

    methodD();

} else {

    methodE();

}

is the same as 

switch (x) {

    case 1: {

        methodA();

    } break;

    case 2: {

        methodB();

    } break;

    case 3: {

        methodC();

    } break;

    case 4: {

        methodD();

    } break;

    default: {

        methodE();

    } break;

}

+1 vote
answered Dec 8, 2024 by POOJA GADHEWAL (170 points)
A  Switch case is a programming construct that executes different blocks of code based on specified conditions.
+1 vote
answered Dec 9, 2024 by BASHITHA KSHEERASAGAR (160 points)

Switch is an alternative for ladder if - else statements which allows user to execute multiple operations. It helps to reduce length of the code

syntax : 

switch()

{

case 1 : printf ("  ");

            break;

case 2 : printf(" ");

             break;

default : printf (" ");

             break;

}

+1 vote
answered Dec 22, 2024 by Divya Dixit (160 points)
so, a switch case, it is used same as if-else. but it is used when there is more number of possible cases. we have to pass an expression and we have to give different cases to check for the given expression and if any matches our expression then the given block of code is executed. otherwise a default case is also given which is executed when no case matches to our expression.
+1 vote
answered Dec 31, 2024 by Tushar Pancheshwar (260 points)
it is a condition that use for  run specialy part  of program that do if ,else or other any conditional statement .

switch(value);

do anything
0 votes
answered Jan 25, 2025 by abhiram rangavajhala (810 points)
It is a conditional statement, meant to substitube large ifs, and else ifs
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...