Friday, 6 October 2023

JAVA

1.switch statement in Java

Lets get started

In Java, the switch statement is used for flow control based on the value of an expression. It allows you to write more concise and readable code when you have multiple conditions to check against a single variable. The basic syntax of a switch statement looks like this:

syntax :

            switch (expression) {
                case value1:
                    // Code to execute if expression matches value1
                    break;
                case value2:
                    // Code to execute if expression matches value2
                    break;
                // More case statements...
                default:
                    // Code to execute if none of the cases match
            }
        

Elaborate with a simple example :

 
          
  1. /*Now we are going to design a simple code in switch case in java
  2. here we will take the month number number as the input from the user
  3. and then we will check each case and print the appropriate month name
  4. */
  5. import java.util.* ;
  6. public class MINtech{
  7. public static void main(String args[]){
  8. // calling the scanner class
  9. Scanner sc = new Scanner(System.in);
  10. System.out.println("Enter Month numebr(1-12) : ") ;
  11. // taking int as a input
  12. int num = sc.nextInt();
  13. // Initializing switch condition
  14. switch(num){
  15. // checking whether the num is equals to 1 or not
  16. case 1 :
  17. // Printing month name
  18. System.out.println("Jan");
  19. //Using break to exit case
  20. break ;
  21. // checking whether the num is equals to 1 or not
  22. case 2 :
  23. // Printing month name
  24. System.out.println("Feb");
  25. //Using break to exit case
  26. break;
  27. case 3 :
  28. System.out.println("Mar");
  29. break ;
  30. case 4:
  31. System.out.println("Apr");
  32. break;
  33. case 5 :
  34. System.out.println("May");
  35. break ;
  36. case 6 :
  37. System.out.println("Jun");
  38. break;
  39. case 7 :
  40. System.out.println("Jul");
  41. break ;
  42. case 8 :
  43. System.out.println("Aug");
  44. break;
  45. case 9 :
  46. System.out.println("Sep");
  47. break ;
  48. case 10 :
  49. System.out.println("Oct");
  50. break;
  51. case 11 :
  52. System.out.println("Nov");
  53. break ;
  54. case 12 :
  55. System.out.println("Dec");
  56. break;
  57. // default will executed if num is not matched with
  58. //the anyone of the above mentioned cases
  59. default:
  60. System.out.println("Inavalid month");
  61. break ;
  62. }
  63. }
  64. }

Output

        Enter Month numebr(1-12) : 
        3
        Mar
                
        Enter Month numebr(1-12) : 
        5
        May
                
        Enter Month numebr(1-12) : 
        12
        Dec
                
        Enter Month numebr(1-12) : 
        14
        Inavalid month

        
https://www.highcpmrevenuegate.com/s4ttwmua2u?key=c02c9252219c2ddb8d72b452016a4796

No comments:

Post a Comment