Monday, December 19, 2022

switch statement in java, python 3, c++ and javascript

 Here is how you can write a switch statement in each of the languages:

Java:

int x = 10
switch (x) { 
    case 1: System.out.println("x is 1"); 
        break
    case 2
        System.out.println("x is 2"); 
        break
    default
        System.out.println("x is neither 1 nor 2"); 
        break
}

Python 3:

x = 10 
if x == 1
    print('x is 1'
elif x == 2
    print('x is 2'
else
    print('x is neither 1 nor 2')

C++:

int x = 10
switch (x) { 
    case 1
        std::cout << "x is 1" << std::endl
        break
    case 2
        std::cout << "x is 2" << std::endl
        break
    default
        std::cout << "x is neither 1 nor 2" << std::endl
        break
}

JavaScript:

const x = 10
switch (x) { 
    case 1
        console.log('x is 1'); 
        break
    case 2
        console.log('x is 2'); 
        break
    default
        console.log('x is neither 1 nor 2'); 
        break
}

I hope this helps! Let me know if you have any questions.

No comments:

Post a Comment

meta.ai impression

Meta.ai is released by meta yesterday, it is super fast you can generate image while typing! You can ask meta.ai to draw a cat with curvy fu...