<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Kaviya.k</title>
    <description>The latest articles on DEV Community by Kaviya.k (@kaviyak).</description>
    <link>https://dev.to/kaviyak</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2597584%2F46e447e6-2a3c-4587-81fb-e66c62a4e725.png</url>
      <title>DEV Community: Kaviya.k</title>
      <link>https://dev.to/kaviyak</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kaviyak"/>
    <language>en</language>
    <item>
      <title>For loop Number pattern:</title>
      <dc:creator>Kaviya.k</dc:creator>
      <pubDate>Fri, 14 Feb 2025 06:57:17 +0000</pubDate>
      <link>https://dev.to/kaviyak/number-pattern-1kl</link>
      <guid>https://dev.to/kaviyak/number-pattern-1kl</guid>
      <description>&lt;p&gt;&lt;strong&gt;Numbers pattern(1to9):&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package B1;

public class Npattern {

    public static void main(String[] args) {
        // Npattern1();
        // Npattern2();
        // Npattern3();
        // Npatern4();
        // Npattern5();
        // Npattern6();
        // Npattern7();
        // Npattern8();
        // Npattern9();
        Npattern10();

    }

    private static void Npattern10() {
        for (int row = 1; row &amp;lt;= 9; row++) {
            for (int col = 1; col &amp;lt;= 9; col++) {
                if (col == 3 || col == 9 || col == 1)
                    System.out.print("* ");
                else if (row == 1 &amp;amp;&amp;amp; col &amp;gt;= 3 || row == 9 &amp;amp;&amp;amp; col &amp;gt;= 3)
                    System.out.print("* ");
                else
                    System.out.print("  ");
            }
            System.out.println();
        }

    }
output:

*   * * * * * * * 
*   *           * 
*   *           * 
*   *           * 
*   *           * 
*   *           * 
*   *           * 
*   *           * 
*   * * * * * * * 


    private static void Npattern9() {
        for (int row = 1; row &amp;lt;= 9; row++) {
            for (int col = 1; col &amp;lt;= 9; col++) {
                if (row == 9 || row == 1 || col == 9)
                    System.out.print("* ");
                else if (row == 5 || col == 1 &amp;amp;&amp;amp; row &amp;lt;= 5)
                    System.out.print("* ");
                else
                    System.out.print("  ");
            }
            System.out.println();
        }

    }
output:

* * * * * * * * * 
*               * 
*               * 
*               * 
* * * * * * * * * 
                * 
                * 
                * 
* * * * * * * * * 



    private static void Npattern8() {
        for (int row = 1; row &amp;lt;= 9; row++) {
            for (int col = 1; col &amp;lt;= 9; col++) {
                if (row == 5 || row == 1 || row == 9)
                    System.out.print("* ");
                else if (col == 1 || col == 9)
                    System.out.print("* ");
                else
                    System.out.print("  ");
            }
            System.out.println();
        }
    }
output:

* * * * * * * * * 
*               * 
*               * 
*               * 
* * * * * * * * * 
*               * 
*               * 
*               * 
* * * * * * * * * 


    private static void Npattern7() {
        for (int row = 1; row &amp;lt;= 9; row++) {
            for (int col = 1; col &amp;lt;= 9; col++) {
                if (row == 1 || row + col == 10)
                    System.out.print("* ");

                else
                    System.out.print("  ");
            }
            System.out.println();
        }

    }
output:

* * * * * * * * * 
              *   
            *     
          *       
        *         
      *           
    *             
  *               
*          

    private static void Npattern6() {
        for (int row = 1; row &amp;lt;= 9; row++) {
            for (int col = 1; col &amp;lt;= 9; col++) {
                if (row == 5 || row == 1 || row == 9)
                    System.out.print("* ");
                else if (col == 1 || col == 9 &amp;amp;&amp;amp; row &amp;gt;= 5)
                    System.out.print("* ");
                else
                    System.out.print("  ");
            }
            System.out.println();
        }
    }
output:

* * * * * * * * * 
*                 
*                 
*                 
* * * * * * * * * 
*               * 
*               * 
*               * 
* * * * * * * * * 


    private static void Npattern5() {
        for (int row = 1; row &amp;lt;= 9; row++) {
            for (int col = 1; col &amp;lt;= 9; col++) {
                if (row == 5 || row == 1 || row == 9)
                    System.out.print("* ");
                else if (col == 1 &amp;amp;&amp;amp; row &amp;lt;= 5 || col == 9 &amp;amp;&amp;amp; row &amp;gt;= 5)
                    System.out.print("* ");
                else
                    System.out.print("  ");
            }
            System.out.println();
        }

    }
output:

* * * * * * * * * 
*                 
*                 
*                 
* * * * * * * * * 
                * 
                * 
                * 
* * * * * * * * * 


    private static void Npatern4() {

        for (int row = 1; row &amp;lt;= 9; row++) {
            for (int col = 1; col &amp;lt;= 9; col++) {
                if (row == 5 || col == 5)
                    System.out.print("* ");
                else if (row + col == 6 &amp;amp;&amp;amp; row &amp;lt;= 5)
                    System.out.print("* ");
                else
                    System.out.print("  ");
            }
            System.out.println();
        }
    }
output:

        *         
      * *         
    *   *         
  *     *         
* * * * * * * * * 
        *         
        *         
        *         
        * 

    private static void Npattern3() {
        for (int row = 1; row &amp;lt;= 9; row++) {
            for (int col = 1; col &amp;lt;= 9; col++) {
                if (row == 9 || row == 5 || row == 1)
                    System.out.print("* ");
                else if (col == 9 &amp;amp;&amp;amp; row &amp;lt;= 5 || col == 9 &amp;amp;&amp;amp; row &amp;gt;= 5)
                    System.out.print("* ");
                else
                    System.out.print("  ");
            }
            System.out.println();
        }

    }
output:

* * * * * * * * * 
                * 
                * 
                * 
* * * * * * * * * 
                * 
                * 
                * 
* * * * * * * * * 


    private static void Npattern2() {
        for (int row = 1; row &amp;lt;= 9; row++) {
            for (int col = 1; col &amp;lt;= 9; col++) {
                if (row == 9 || row == 5 || row == 1)
                    System.out.print("* ");
                else if (col == 1 &amp;amp;&amp;amp; row &amp;gt;= 5 || col == 9 &amp;amp;&amp;amp; row &amp;lt;= 5)
                    System.out.print("* ");
                else
                    System.out.print("  ");
            }
            System.out.println();
        }
    }
output:

* * * * * * * * * 
                * 
                * 
                * 
* * * * * * * * * 
*                 
*                 
*                 
* * * * * * * * * 


    private static void Npattern1() {
        for (int row = 1; row &amp;lt;= 9; row++) {
            for (int col = 1; col &amp;lt;= 9; col++) {
                if (row == 9 || col == 5 || col + row == 6 &amp;amp;&amp;amp; row &amp;lt;= 4)
                    System.out.print("* ");
                else
                    System.out.print("  ");
            }
            System.out.println();
        }
    }
}

output:

        *         
      * *         
    *   *         
  *     *         
        *         
        *         
        *         
        *         
* * * * * * * * * 


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Star and numbers pattern:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
package B1;

public class NSpattern {

    public static void main(String[] args) {
        pattern();
        // pattern1();
        // pattern2();
        // pattern3();
        // pattern4();
        // pattern5();
        // pattern6();

    }

    private static void pattern() {
        for (int row = 1; row &amp;lt;= 5; row++) {
            for (int col = 1; col &amp;lt;= row; col++)
                System.out.print("* ");

            System.out.println();
        }

    }
output:
* 
* * 
* * * 
* * * * 
* * * * * 


    private static void pattern6() {
        for (int row = 5; row &amp;gt;= 1; row--) {
            for (int col = 1; col &amp;lt;= row; col++)
                System.out.print(row + col + " ");
            System.out.println();
        }

    }
output:

6 7 8 9 10 
5 6 7 8 
4 5 6 
3 4 
2 


    private static void pattern5() {
        for (int row = 5; row &amp;gt;= 1; row--) {
            for (int col = 1; col &amp;lt;= row; col++)
                System.out.print(row / col + " ");
            System.out.println();
        }
    }
output:

5 2 1 1 1 
4 2 1 1 
3 1 1 
2 1 
1 


    private static void pattern4() {
        for (int row = 5; row &amp;gt;= 1; row--) {
            for (int col = 1; col &amp;lt;= row; col++)
                System.out.print(row * col + " ");
            System.out.println();
        }
    }
output:

5 10 15 20 25 
4 8 12 16 
3 6 9 
2 4 
1



    private static void pattern3() {
        for (int row = 5; row &amp;gt;= 1; row--) {
            for (int col = 1; col &amp;lt;= row; col++)
                System.out.print(col + " ");
            System.out.println();
        }
    }
output:

1 2 3 4 5 
1 2 3 4 
1 2 3 
1 2 
1 



    private static void pattern2() {
        for (int row = 5; row &amp;gt;= 1; row--) {
            for (int col = 1; col &amp;lt;= row; col++)
                System.out.print(row + " ");
            System.out.println();
        }
    }
output:

5 5 5 5 5 
4 4 4 4 
3 3 3 
2 2 
1 

    private static void pattern1() {
        for (int row = 5; row &amp;gt;= 1; row--) {
            for (int col = 1; col &amp;lt;= row; col++)
                System.out.print("* ");
            System.out.println();
        }

    }

}
output:

* * * * * 
* * * * 
* * * 
* * 
* 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>programming</category>
      <category>java</category>
    </item>
    <item>
      <title>while loop:</title>
      <dc:creator>Kaviya.k</dc:creator>
      <pubDate>Thu, 13 Feb 2025 10:53:40 +0000</pubDate>
      <link>https://dev.to/kaviyak/while-loop-5bb0</link>
      <guid>https://dev.to/kaviyak/while-loop-5bb0</guid>
      <description>&lt;p&gt;Number series:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1.Look at this series: 7, 10, 8, 11, 9, 12, ... What number should come next?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's analyze the pattern in this series:&lt;/p&gt;

&lt;p&gt;7 → 10 (add 3)&lt;br&gt;
10 → 8 (subtract 2)&lt;br&gt;
8 → 11 (add 3)&lt;br&gt;
11 → 9 (subtract 2)&lt;br&gt;
9 → 12 (add 3)&lt;/p&gt;

&lt;p&gt;The pattern alternates between adding 3 and subtracting 2.Since the last operation was adding 3 (from 9 to 12), the next operation should be subtracting 2.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;program 1:

public class Series {

    public static void main(String[] args) {
        int no=7;
        int count=0;
    while(count&amp;lt;=6) {
            System.out.println(no);
            no=no+3;
            System.out.println(no);
            no=no-2;
            count=count+2;
        }
    }

}

output:
7
10
8
11
9
12
10
13

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2.Look at this series: 36, 34, 30, 28, 24, ... What number should come next?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's analyze the pattern:&lt;/p&gt;

&lt;p&gt;36 → 34 (subtract 2)&lt;br&gt;
34 → 30 (subtract 4)&lt;br&gt;
30 → 28 (subtract 2)&lt;br&gt;
28 → 24 (subtract 4)&lt;/p&gt;

&lt;p&gt;The pattern alternates between subtracting 2 and subtracting 4.so the next operation should be to subtract 2 from 24.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;program 2:

package Numbers;

public class Series1 {

    public static void main(String[] args) {
        int no=36;
        int count=0;
        while(count&amp;lt;5) {
            System.out.println(no);
            no=no-2;
            System.out.println(no);
            no=no-4;
            count=count+2;
        }

    }

}
output:
36
34
30
28
24
22
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3.Look at this series: 53, 53, 40, 40, 27, 27, ... What number should come next?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;let's look at the pattern:&lt;/p&gt;

&lt;p&gt;53 → 53 (no change)&lt;br&gt;
53 → 40 (subtract 13)&lt;br&gt;
40 → 40 (no change)&lt;br&gt;
40 → 27 (subtract 13)&lt;br&gt;
27 → 27 (no change)&lt;/p&gt;

&lt;p&gt;The pattern alternates between no change and subtracting 13. the next step should be subtracting 13 from 27.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;program3:
package Numbers;

public class Series2 {

    public static void main(String[] args) {
        int no=53;
        int count=0;
        while(count&amp;lt;=5){
            System.out.println(no);
            System.out.println(no);
            no=no-13;
            count=count+2;
        }
    }

}
output:
53
53
40
40
27
27

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;4.Look at this series: 21, 9, 21, 11, 21, 13, 21, ... What number should come next?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's break down the pattern:&lt;/p&gt;

&lt;p&gt;21 → 9 (subtract 12)&lt;br&gt;
9 → 21 (add 12)&lt;br&gt;
21 → 11 (subtract 10)&lt;br&gt;
11 → 21 (add 10)&lt;br&gt;
21 → 13 (subtract 8)&lt;/p&gt;

&lt;p&gt;The pattern alternates between subtracting a number and then adding the same or a different number.&lt;br&gt;
The pattern for subtraction follows: 12, 10, 8 (subtracting 2 each time) the next step should be to add 8 (following the alternating pattern of addition and subtraction).13 + 8 = 21&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;program 4:
package Numbers;

public class Series3 {

    public static void main(String[] args) {
        int no=58;
        int count=1;
        while(count&amp;lt;=5) {
            System.out.println(no);
            no=no-6;
            count=count+1;
        }
    }

}
output:
58
52
46
40
34

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;5.Look at this series: 3, 4, 7, 8, 11, 12, ... What number should come next?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's look at the pattern:&lt;/p&gt;

&lt;p&gt;3 → 4 (add 1)&lt;br&gt;
4 → 7 (add 3)&lt;br&gt;
7 → 8 (add 1)&lt;br&gt;
8 → 11 (add 3)&lt;br&gt;
11 → 12 (add 1)&lt;/p&gt;

&lt;p&gt;The pattern alternates between adding 1 and adding 3. the next operation should be adding 3.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;program 5:
package Numbers;

public class Series4 {

    public static void main(String[] args) {
        int no=3;
        int count=1;
        while(count&amp;lt;=6){
            System.out.println(no);
            no=no+1;
            System.out.println(no);
            no=no+3;
            count=count+2;
        }
        }

}
output:
3
4
7
8
11
12
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;6.Look at this series: 8, 22, 8, 28, 8, ... What number should come next?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's analyze the pattern:&lt;/p&gt;

&lt;p&gt;8 → 22 (add 14)&lt;br&gt;
22 → 8 (subtract 14)&lt;br&gt;
8 → 28 (add 20)&lt;br&gt;
28 → 8 (subtract 20)&lt;/p&gt;

&lt;p&gt;The pattern alternates between adding a number and then subtracting the same number, with the numbers being added or subtracted increasing by 6 each time.&lt;br&gt;
The last operation was subtracting 20, so the next step should be to add 26 (because the previous addition increased by 6).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;program 6:
package Numbers;

public class Series5 {

    public static void main(String[] args) {
        int no=22;
        int count=0;
        while(count&amp;lt;=5){
            System.out.print(8 +" ");
            System.out.print(no +" ");
            no=no+6;
            count=count+2;
        }
}
}
output:
8 
22 
8 
28 
8 
34 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;7.Look at this series: 21, 9, 21, 11, 21, 13, 21, ... What number should come next?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's analyze the pattern:&lt;/p&gt;

&lt;p&gt;21 → 9 (subtract 12)&lt;br&gt;
9 → 21 (add 12)&lt;br&gt;
21 → 11 (subtract 10)&lt;br&gt;
11 → 21 (add 10)&lt;br&gt;
21 → 13 (subtract 8)&lt;/p&gt;

&lt;p&gt;The pattern alternates between subtracting an even number and adding the same number.&lt;br&gt;
The subtraction numbers decrease by 2 each time (12, 10, 8). Following this, the next subtraction should be by 6.&lt;br&gt;
So, we subtract 6 from 13:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;program 7:
package Numbers;

public class Series6 {

    public static void main(String[] args) {
        int no=9;
        int count=0;
        while(count&amp;lt;8) {
            System.out.println(21);
            System.out.println(no);
            no=no+2;
            count=count+2;
        }

    }

}
output:
21
9
21
11
21
13
21
15

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;8.Look at this series: 5.2, 4.8, 4.4, 4, ... What number should come next?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's look at the pattern:&lt;br&gt;
5.2 → 4.8 (subtract 0.4)&lt;br&gt;
4.8 → 4.4 (subtract 0.4)&lt;br&gt;
4.4 → 4 (subtract 0.4)&lt;/p&gt;

&lt;p&gt;The pattern consistently subtracts 0.4.&lt;br&gt;
Following this pattern, the next step would be to subtract 0.4 from 4:&lt;br&gt;
4 - 0.4 = 3.6&lt;br&gt;
So, the next number in the series is 3.6.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;program 8:

public class Series7 {
    public static void main(String args[])
    {
    double no=5.2f;
    int count=0;
    while(count&amp;lt;6) {
        System.out.println(no);
        no=  no-0.4;
        count++;
    }
    }
}

output:
5.2
4.8
4.3999999999999995
3.9999999999999996
3.5999999999999996

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;9.Look at this series: 2, 4, 6, 8, 10, ... What number should come next?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is a simple series where each number is increasing by 2:&lt;br&gt;
2 → 4 (add 2)&lt;br&gt;
4 → 6 (add 2)&lt;br&gt;
6 → 8 (add 2)&lt;br&gt;
8 → 10 (add 2)&lt;br&gt;
Following this pattern, the next step would be to add 2 to 10:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;program:9

package Numbers;

public class Series9 {

    public static void main(String[] args) {
        int no=2;
        int count=1;
        while(count&amp;lt;=6) {
            System.out.println(no);
            no=no+2;
            count=count+1;
        }
    }

}

output:
2
4
6
8
10
12

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;10.Look at this series: 31, 29, 24, 22, 17, ... What number should come next?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's look at the pattern in the series: 31, 29, 24, 22, 17.&lt;br&gt;
31 to 29, the difference is -2.&lt;br&gt;
29 to 24, the difference is -5.&lt;br&gt;
24 to 22, the difference is -2.&lt;br&gt;
22 to 17, the difference is -5.&lt;/p&gt;

&lt;p&gt;It seems the pattern alternates between subtracting 2 and subtracting 5. If this pattern continues, the next step would be to subtract 2 from 17.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;program 10:

package Numbers;

public class Series11 {

    public static void main(String[] args) {
        int no=31;
        int count=1;
        while(count&amp;lt;=5) {
            System.out.println(no);
            no=no-2;
            System.out.println(no);
            no=no-5;
        count=count+2;
    }

    }
}
output:
31
29
24
22
17
15

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>java</category>
      <category>programming</category>
    </item>
    <item>
      <title>Patterns:</title>
      <dc:creator>Kaviya.k</dc:creator>
      <pubDate>Thu, 13 Feb 2025 04:09:35 +0000</pubDate>
      <link>https://dev.to/kaviyak/patterns-4hpg</link>
      <guid>https://dev.to/kaviyak/patterns-4hpg</guid>
      <description>&lt;p&gt;&lt;strong&gt;for loop:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;for (statement 1; statement 2; statement 3) {&lt;br&gt;
  // code block to be executed&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;Statement 1 is executed (one time) before the execution of the code block.&lt;/p&gt;

&lt;p&gt;Statement 2 defines the condition for executing the code block.&lt;/p&gt;

&lt;p&gt;Statement 3 is executed (every time) after the code block has been executed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;br&gt;
public class Main {&lt;br&gt;
  public static void main(String[] args) {&lt;br&gt;
    for (int i = 0; i &amp;lt; 5; i++) {&lt;br&gt;
      System.out.println(i);&lt;br&gt;
    }&lt;br&gt;&lt;br&gt;
  }&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;output:&lt;/strong&gt;&lt;br&gt;
0&lt;br&gt;
1&lt;br&gt;
2&lt;br&gt;
3&lt;br&gt;
4 &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;shape pattern:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package B1;

public class Shape {

    public static void main(String[] args) {
        //shape1();
        //shape2();
         //shape3();
         //shape4();
         //shape5();
         //shape6();
         //shape7();
    }

    private static void shape7() {
        for (int row = 1; row &amp;lt;= 9; row++) {
            for (int col = 1; col &amp;lt;= 9; col++) {
                if(row+col==10||row==col)
                System.out.print("* ");
                else if(row==1||col==1||row==9)
                System.out.print("* ");
                else
                System.out.print("  ");
            }
            System.out.println();
        }

    }
output:


* * * * * * * * * 
* *           *   
*   *       *     
*     *   *       
*       *         
*     *   *       
*   *       *     
* *           *   
* * * * * * * * * 

    private static void shape6() {
        for (int row = 1; row &amp;lt;= 9; row++) {
            for (int col = 1; col &amp;lt;= 9; col++) {
                if(row+col==10||row==col)
                System.out.print("* ");
                else if(row==1||col==9||row==9)
                System.out.print("* ");
                else
                System.out.print("  ");
            }
            System.out.println();
        }

    }
output:

* * * * * * * * * 
  *           * * 
    *       *   * 
      *   *     * 
        *       * 
      *   *     * 
    *       *   * 
  *           * * 
* * * * * * * * * 


    private static void shape5() {
        for (int row = 1; row &amp;lt;= 9; row++) {
            for (int col = 1; col &amp;lt;= 9; col++) {
                if(row+col==10||row==col)
                System.out.print("* ");
                else if(col==1||col==9||row==1)
                System.out.print("* ");
                else
                System.out.print("  ");
            }
            System.out.println();
        }


    }
output:

*               * 
* *           * * 
*   *       *   * 
*     *   *     * 
*       *       * 
*     *   *     * 
*   *       *   * 
* *           * * 
* * * * * * * * * 


    private static void shape4() {
        for (int row = 1; row &amp;lt;= 9; row++) {
            for (int col = 1; col &amp;lt;= 9; col++) {
                if(row+col==10||row==col)
                System.out.print("* ");
                else if(col==1||col==9||row==9)
                System.out.print("* ");
                else
                System.out.print("  ");
            }
            System.out.println();
        }



    }
output:

*               * 
* *           * * 
*   *       *   * 
*     *   *     * 
*       *       * 
*     *   *     * 
*   *       *   * 
* *           * * 
* * * * * * * * * 


    private static void shape3() {
        for (int row = 1; row &amp;lt;= 9; row++) {
            for (int col = 1; col &amp;lt;= 9; col++) {
                if(row+col==10||row==col)
                System.out.print("* ");
                else if(col==1||col==9)
                System.out.print("* ");
                else
                System.out.print("  ");
            }
            System.out.println();
        }

    }
output:

*               * 
* *           * * 
*   *       *   * 
*     *   *     * 
*       *       * 
*     *   *     * 
*   *       *   * 
* *           * * 
*               * 


    private static void shape2() {
        for (int row = 1; row &amp;lt;= 9; row++) {
            for (int col = 1; col &amp;lt;= 9; col++) {
                if(row==1||row==9||row==col)
                System.out.print("* ");
                else if(row+col==10||col==1||col==9)
                System.out.print("* ");
                else
                System.out.print("  ");
            }
            System.out.println();
        }

    }
output:

* * * * * * * * * 
* *           * * 
*   *       *   * 
*     *   *     * 
*       *       * 
*     *   *     * 
*   *       *   * 
* *           * * 
* * * * * * * * * 


    private static void shape1() {
        for (int row = 1; row &amp;lt;= 9; row++) {
            for (int col = 1; col &amp;lt;= 9; col++) {
                if(row==1||row==9||row==col)
                System.out.print("* ");
                else if(row+col==10)
                System.out.print("* ");
                else
                System.out.print("  ");
            }
            System.out.println();
        }
        }

}
output:

* * * * * * * * * 
  *           *   
    *       *     
      *   *       
        *         
      *   *       
    *       *     
  *           *   
* * * * * * * * * 


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Letters pattern:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package B1;
public class Spattern2 {

    public static void main(String[] args) {
        //patternE();
        //patternI();
        //patternL();
        //patternF();
        //patternH();
        //patternM();(TBD)
        //patternK();
    }

    private static void patternK() {
        for(int row = 1; row&amp;lt;=9; row++)
        {
            for(int col=1; col&amp;lt;=9; col++) {
                if(row+col==6|| col==1)
                    System.out.print("* "); 
                else if(col==row-4)
                    System.out.print("* "); 
                else
                    System.out.print("  "); 
        }
               System.out.println(); 
        }


    }
output:

*       *         
*     *           
*   *             
* *               
*                 
* *               
*   *             
*     *           
*       * 

    private static void patternM() {
        for(int row = 1; row&amp;lt;=9; row++)
        {
            for(int col=1; col&amp;lt;=9; col++) {
                if(col==1||col==9-1)
                    System.out.print("* "); 
                else if(row==col&amp;amp;&amp;amp;col&amp;lt;=9/2||row+col==9-1&amp;amp;&amp;amp;col&amp;gt;=9/2)
                    System.out.print("* "); 
                else
                    System.out.print("  ");
        }
               System.out.println(); 
        }


    }
output:

*           * *   
* *       *   *   
*   *   *     *   
*     *       *   
*             *   
*             *   
*             *   
*             *   
*             *   


    private static void patternH() {
        for(int row = 1; row&amp;lt;=9; row++)
        {
            for(int col=1; col&amp;lt;=9; col++) {
                if(col==9|| row==5||col==1)
                    System.out.print("* "); 
                else
                    System.out.print("  "); 
        }
               System.out.println(); 
        }


    }
output:

*               * 
*               * 
*               * 
*               * 
* * * * * * * * * 
*               * 
*               * 
*               * 
*               * 


    private static void patternF() {
        for(int row = 1; row&amp;lt;=9; row++)
        {
            for(int col=1; col&amp;lt;=9; col++) {
                if(row==1|| row==5||col==1)
                    System.out.print("* "); 
                else
                    System.out.print("  "); 
        }
               System.out.println(); 
        }


    }
output:

* * * * * * * * * 
*                 
*                 
*                 
* * * * * * * * * 
*                 
*                 
*                 
*                 


    private static void patternL() {
        for(int row = 1; row&amp;lt;=9; row++)
        {
            for(int col=1; col&amp;lt;=9; col++) {
                if(row==9||col==1)
                    System.out.print("* "); 
                else
                    System.out.print("  "); 
        }
               System.out.println(); 
        }
    }
output:

*                 
*                 
*                 
*                 
*                 
*                 
*                 
*                 
* * * * * * * * * 


    private static void patternI() {
        for(int row = 1; row&amp;lt;=9; row++)
        {
            for(int col=1; col&amp;lt;=9; col++) {
                if(row==1||row==9||col==5)
                    System.out.print("* "); 
                else
                    System.out.print("  "); 
        }
               System.out.println(); 
        }
    }
output:

* * * * * * * * * 
        *         
        *         
        *         
        *         
        *         
        *         
        *         
* * * * * * * * * 





    private static void patternE() {
        for(int row = 1; row&amp;lt;=9; row++)
        {
            for(int col=1; col&amp;lt;=9; col++) {
                if(row==1||row==9||col==1)
                    System.out.print("* "); 
                else if(row==5)
                    System.out.print("* "); 
                else
                    System.out.print("  "); 
        }
               System.out.println(); 
        }
    }
    }
output:

* * * * * * * * * 
*                 
*                 
*                 
* * * * * * * * * 
*                 
*                 
*                 
* * * * * * * * * 

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>programming</category>
      <category>java</category>
    </item>
    <item>
      <title>Do and while loop:</title>
      <dc:creator>Kaviya.k</dc:creator>
      <pubDate>Thu, 13 Feb 2025 03:11:31 +0000</pubDate>
      <link>https://dev.to/kaviyak/while-and-do-loop-3lb6</link>
      <guid>https://dev.to/kaviyak/while-and-do-loop-3lb6</guid>
      <description>&lt;p&gt;&lt;strong&gt;while loop&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Java while loop is a control flow statement used to execute the block of statements repeatedly until the given condition evaluates to false. Once the condition becomes false, the line immediately after the loop in the program is executed.&lt;/p&gt;

&lt;p&gt;The while loop loops through a block of code as long as a specified condition is true.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax of while loop in Java:&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;while (test_expression) {   

 // statements        

update_expression; 


}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Execution of While Loop in Java:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnfdu6r6i6stlo1m2tdt7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnfdu6r6i6stlo1m2tdt7.png" alt="Image description" width="800" height="492"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;1.Control enters the while loop.&lt;br&gt;
2.The condition is tested.&lt;br&gt;
3.If true, execute the body of the loop.&lt;br&gt;
4.If false, exit the loop.&lt;br&gt;
5.After executing the body, update the loop variable.&lt;br&gt;
6.Repeat from step-2 until the condition is false.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;simple example:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Main {
  public static void main(String[] args) {
    int i = 0;
    while (i &amp;lt; 5) {
      System.out.println(i);
      i++;
    }  
  }
}

output:
0
1
2
3
4 

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;The Do-While Loop:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This do-while loop is an exit controlled loop that checks the condition at the end of the code. This loop ensures that the code is executed at least once.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm1a9u2ba1z4qbxo79ed2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm1a9u2ba1z4qbxo79ed2.png" alt="Image description" width="289" height="331"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;**simple example:**

package B10;

public class loop {

    public static void main(String[] args) {
        int no=1;
        do
        {
            System.out.println(no);
            no=no+1;
        }
        while(no&amp;lt;=5);
    }

}
output:
1
2
3
4
5



&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Differnce between while and do while loop:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiorp5cncdbg2r3lmjbkd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiorp5cncdbg2r3lmjbkd.png" alt="Image description" width="697" height="708"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2bj0uev1m68u9dbnscn2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2bj0uev1m68u9dbnscn2.png" alt="Image description" width="733" height="418"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Entry Control Loops&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;These control the entry to the loop. They check for a condition and the control is transferred inside the loop if, and only if, the condition is true. These loops are used when checking for a condition before execution is mandatory. &lt;strong&gt;Example&lt;/strong&gt; include for and while loops. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Exit Control Loops&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;An exit control loop controls the exit. The exit control loop checks the condition for an exit. If the given condition for exit is true, control will exit from the loop body, or else control will enter again into the loop.  &lt;strong&gt;Example&lt;/strong&gt; is a do-while loop.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Increment and Decrement Operators:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Increment and Decrement Operators are Unary Operators commonly used in programming to increase or decrease the value of a variable by one, respectively.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Increment Operators:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Increment operators are used to increase the value of a variable by one. There are two types of increment operators: &lt;br&gt;
&lt;strong&gt;The prefix increment operator (++x) and&lt;/strong&gt; &lt;br&gt;
&lt;strong&gt;The postfix increment operator (x++)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Prefix Increment Operator (++x):&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The prefix increment operator increases the value of the variable by 1 before the value is used in the expression.&lt;br&gt;
&lt;strong&gt;Syntax:&lt;/strong&gt; ++x&lt;br&gt;
&lt;strong&gt;Example:&lt;/strong&gt; If x is initially 5, ++x will increment x to 6 and return the new value (6).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Postfix Increment Operator (x++):&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The postfix increment operator increases the value of the variable by 1 after the value is used in the expression.&lt;br&gt;
&lt;strong&gt;Syntax:&lt;/strong&gt; x++&lt;br&gt;
&lt;strong&gt;Example:&lt;/strong&gt; If x is initially 5, x++ will return the current value of x (5) and then increment x to 6.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;**Example program:**(Increment)

public class Main {
    public static void main(String[] args)
    {
        int x = 5;
        // Prefix increment: increment x by 1 and then print
        // the value (6)
        System.out.println(++x);
        // Postfix increment: print the value of x (6) and
        // then increment x by 1
        System.out.println(x++);
        // Output: 7
        System.out.println(x);
    }
}
output:
6
6
7
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Decrement Operators:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Decrement operators are used in programming languages to decrease the value of a variable by one. Similar to increment operators, there are two types of decrement operators: &lt;br&gt;
&lt;strong&gt;The prefix decrement operator (--x)&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;The postfix decrement operator(x--)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Prefix Decrement Operator (--x):&lt;/strong&gt;&lt;br&gt;
The prefix decrement operator decreases the value of the variable by 1 before the value is used in the expression.&lt;br&gt;
&lt;strong&gt;Syntax:&lt;/strong&gt; --x&lt;br&gt;
&lt;strong&gt;Example:&lt;/strong&gt;  If x is initially 5, --x will decrement x to 4 and return the new value (4).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Postfix Decrement Operator (x--):&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The postfix decrement operator decreases the value of the variable by 1 after the value is used in the expression.&lt;br&gt;
&lt;strong&gt;Syntax:&lt;/strong&gt;  x--&lt;br&gt;
&lt;strong&gt;Example:&lt;/strong&gt;  If x is initially 5, x-- will return the current value of x (5) and then decrement x to 4.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Example program:(Decrement)

public class Main {
    public static void main(String[] args)
    {
        int x = 5;
        // Prefix decrement: decrement x by 1 and then print
        // the value (4)
        System.out.println(--x);
        // Postfix decrement: print the value of x (4) and
        // then decrement x by 1
        System.out.println(x--);
        // Output: 3
        System.out.println(x);
    }
}
output:
4
4
3

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;References:&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://www.simplilearn.com/tutorials/javascript-tutorial/javascript-loops" rel="noopener noreferrer"&gt;https://www.simplilearn.com/tutorials/javascript-tutorial/javascript-loops&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.geeksforgeeks.org/java-while-loop-with-examples/" rel="noopener noreferrer"&gt;https://www.geeksforgeeks.org/java-while-loop-with-examples/&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.w3schools.com/java/tryjava.asp?filename=demo_while_loop" rel="noopener noreferrer"&gt;https://www.w3schools.com/java/tryjava.asp?filename=demo_while_loop&lt;/a&gt;&lt;br&gt;
&lt;a href="https://studyopedia.com/java/loops-in-java/" rel="noopener noreferrer"&gt;https://studyopedia.com/java/loops-in-java/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>programming</category>
      <category>java</category>
    </item>
    <item>
      <title>GCD AND LCM:</title>
      <dc:creator>Kaviya.k</dc:creator>
      <pubDate>Wed, 05 Feb 2025 04:08:14 +0000</pubDate>
      <link>https://dev.to/kaviyak/gcd-and-lcm-1hi1</link>
      <guid>https://dev.to/kaviyak/gcd-and-lcm-1hi1</guid>
      <description>&lt;p&gt;&lt;strong&gt;Introduction to GCD and LCM:&lt;/strong&gt;(TBD)&lt;/p&gt;

&lt;p&gt;GCD (Greatest Common Divisor) and LCM (Least Common Multiple) are fundamental concepts in number theory and mathematics. They are used to find relationships between two or more numbers and play a significant role in various mathematical computations and problem-solving.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;GCD:&lt;/strong&gt;(TBD)&lt;/p&gt;

&lt;p&gt;The GCD of two or more integers is the largest positive integer that divides each of the given numbers without leaving a remainder. In other words, it is the greatest common factor shared by the numbers. GCD is often denoted by the symbol "gcd(a, b)" or simply "(a, b)". &lt;br&gt;
For example to : &lt;br&gt;
The GCD of { 34,56 } is 2 &lt;br&gt;
The GCD of { 64,96 } is 32 &lt;/p&gt;

&lt;p&gt;How to Find GCD?:(TBD)&lt;br&gt;
 Write all the factors of each number.&lt;br&gt;
 Select the common factors.&lt;br&gt;
 Select the largest number, as GCD.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;program:(TBD)

    public class FindGCDExample2  
    {  
    public static void main(String[] args)   
    {  
    int n1=50, n2=60;  
    while(n1!=n2)   
    {  
    if(n1&amp;gt;n2)  
    n1=n1-n2;  
    else  
    n2=n2-n1;  
    }  
    System.out.printf("GCD of n1 and n2 is: " +n2);  
    }  
    }  

Output:
GCD of n1 and n2 is: 10


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;LCM:&lt;/strong&gt;(TBD)&lt;/p&gt;

&lt;p&gt;The LCM of two or more integers is the smallest positive integer that is divisible by each of the given numbers. It is the least common multiple shared by the numbers. LCM is often denoted by the symbol "lcm(a, b)" or simply "[a, b]". For example to : &lt;br&gt;
The LCM of { 34,56 } is 952 &lt;br&gt;
The LCM of { 64,96 } is 192 &lt;/p&gt;

&lt;p&gt;How to Find the Greatest Common Factor:(TBD)&lt;/p&gt;

&lt;p&gt;Write all the factors of each number.&lt;br&gt;
 Select the common factors.&lt;br&gt;
 Select the greatest number, as GCF.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;program:(TBD)
    public class LcmExample1  
    {  
    public static void main(String args[])   
    {  
    int a = 12, b = 9, gcd = 1;  
    //finds GCD  
    for(int i = 1; i &amp;lt;= a &amp;amp;&amp;amp; i &amp;lt;= b; ++i)   
    {  
    //divides both the numbers by i, if the remainder   is 0 the number is completely divisible by i  
    //Checks that i is present in both or not  
    //returns true if both conditions are true  
    if(a % i == 0 &amp;amp;&amp;amp; b % i == 0)  
    //assigns i into gcd  
    gcd = i;  
    }  
    //determines lcm of the given number  
    int lcm = (a * b) / gcd;  
    //prints the result  
    System.out.printf("The LCM of %d and %d is %d.", a, b, lcm);  
    }  
    } 
output: 
  The LCM of 12 and 9 is 36.

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Task:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;program 1:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
package Numbers;

public class Pattern 
{
public static void main(String[] args) {
        int count=1;
        while (count&amp;lt;=5){
        System.out.println("1 2 3 4 5");
        count++;
        }
        }
                }
output:

1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;program 2:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package Numbers;

public class Pattern2 {

    public static void main(String[] args) {
        int count=1;
        while (count&amp;lt;5){
        System.out.println("1 0 1 0 1 ");
        count++;
        }
                }
                }

output:
1 0 1 0 1 
1 0 1 0 1 
1 0 1 0 1 
1 0 1 0 1 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;program 3:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package Numbers;

public class Multiply {

    public static void main(String[] args) {
        int num=1;
        while(num&amp;lt;=10) {
      System.out.println("3*"+ num +"="+(3*num));
            num++;
        }
}
}

output:

3*1=3
3*2=6
3*3=9
3*4=12
3*5=15
3*6=18
3*7=21
3*8=24
3*9=27
3*10=30


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;References:&lt;br&gt;
&lt;a href="https://www.javatpoint.com/lcm-of-two-numbers-in-java" rel="noopener noreferrer"&gt;https://www.javatpoint.com/lcm-of-two-numbers-in-java&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.javatpoint.com/java-program-to-find-gcd-of-two-numbers" rel="noopener noreferrer"&gt;https://www.javatpoint.com/java-program-to-find-gcd-of-two-numbers&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>programming</category>
    </item>
    <item>
      <title>While loop:</title>
      <dc:creator>Kaviya.k</dc:creator>
      <pubDate>Mon, 03 Feb 2025 02:00:29 +0000</pubDate>
      <link>https://dev.to/kaviyak/while-loop-p2e</link>
      <guid>https://dev.to/kaviyak/while-loop-p2e</guid>
      <description>&lt;p&gt;&lt;strong&gt;While loop:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Java while loop is a control flow statement used to execute the block of statements repeatedly until the given condition evaluates to false. Once the condition becomes false, the line immediately after the loop in the program is executed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax of while loop in Java:&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;while (test_expression) {   

 // statements        

update_expression; 


}


condition is evaluated before each iteration of the loop
If condition is true, the code block inside the while loop will execute
The process repeats until condition is false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Execution of While Loop in Java:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgdom8ort2ej6hlyn1gce.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgdom8ort2ej6hlyn1gce.png" alt="Image description" width="800" height="492"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;    Control enters the while loop.&lt;/li&gt;
&lt;li&gt;    The condition is tested.&lt;/li&gt;
&lt;li&gt;    If true, execute the body of the loop.&lt;/li&gt;
&lt;li&gt;    If false, exit the loop.&lt;/li&gt;
&lt;li&gt;    After executing the body, update the loop variable.&lt;/li&gt;
&lt;li&gt;    Repeat from step-2 until the condition is false.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Example 1: Display Numbers from 1 to 5

// Program to display numbers from 1 to 5

class Main {
  public static void main(String[] args) {

    // declare variables
    int i = 1, n = 5;

    // while loop from 1 to 5
    while(i &amp;lt;= n) {
      System.out.println(i);
      i++;
    }
  }
}

output:
1
2
3
4
5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Task1:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj8m96x3vxwjspa6qyc1r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj8m96x3vxwjspa6qyc1r.png" alt="Image description" width="667" height="750"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Program:
public class Count {

    public static void main(String[] args) {
    int count=1;
        while (count&amp;lt;=5){
        count=count+1;
        System.out.println("count: "+count);
        }
        }
        }

output:
count: 2
count: 3
count: 4
count: 5
count: 6

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Task2:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnwiv0qki1wmosq96zli7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnwiv0qki1wmosq96zli7.png" alt="Image description" width="667" height="750"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Program:

public class Count1 {

    public static void main(String[] args) {
        int count=5;
        while (count&amp;gt;=1) {
        count=count-1;
        {
        System.out.println("count:" +count);
        }
        }
    }
}

output:
count:4
count:3
count:2
count:1
count:0

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Task3:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6oqkz163n4dxq8lqo7wi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6oqkz163n4dxq8lqo7wi.png" alt="Image description" width="667" height="750"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Program:
public class Count2 {
    public static void main (String args[])
    {
    int count=0;
    while(count&amp;lt;10) {
    count=count+2;
    {
    System.out.println("count:" +count);
    }
    }
    }
}
output:
count:2
count:4
count:6
count:8
count:10

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Task4:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqtuts992v3z13swd7px8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqtuts992v3z13swd7px8.png" alt="Image description" width="667" height="750"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;program:
public class Count3 {
    public static void main (String args[])
    {
    int count=1;
    while(count&amp;lt;=10) {
    count=count+2;
    {
    System.out.println("count:" +count);
    }
    }
    }

}
output:

count:3
count:5
count:7
count:9
count:11

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;References:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.geeksforgeeks.org/java-while-loop-with-examples/" rel="noopener noreferrer"&gt;https://www.geeksforgeeks.org/java-while-loop-with-examples/&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.programiz.com/java-programming/do-while-loop" rel="noopener noreferrer"&gt;https://www.programiz.com/java-programming/do-while-loop&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>programming</category>
    </item>
    <item>
      <title>switch and while loop:</title>
      <dc:creator>Kaviya.k</dc:creator>
      <pubDate>Sun, 02 Feb 2025 12:00:23 +0000</pubDate>
      <link>https://dev.to/kaviyak/switch-and-while-loop-3g6f</link>
      <guid>https://dev.to/kaviyak/switch-and-while-loop-3g6f</guid>
      <description>&lt;p&gt;&lt;strong&gt;Java Switch Statements:&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        Instead of writing many if..else statements, you can use the switch statement.

       The switch statement selects one of many code blocks to be executed.Unlike if-then and if-then-else statements, the switch statement can have a number of possible execution paths. A switch works with the byte, short, char, and int primitive data types. It also works with enumerated types (discussed in Enum Types), the String class, and a few special classes that wrap certain primitive types: Character, Byte, Short, and Integer (discussed in Numbers and Strings).
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Syntax:&lt;/p&gt;

&lt;p&gt;switch(expression)&lt;br&gt;
{&lt;br&gt;
  case value1 :&lt;br&gt;
     // Statements&lt;br&gt;
     break; // break is optional&lt;/p&gt;

&lt;p&gt;case value2 :&lt;br&gt;
     // Statements&lt;br&gt;
     break; // break is optional&lt;br&gt;
     ....&lt;br&gt;
     ....&lt;br&gt;
     ....&lt;br&gt;
   default : &lt;br&gt;
     // default Statement&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;Example: Size Printer Example&lt;br&gt;
1&lt;/p&gt;

&lt;p&gt;public class SizePrinter {&lt;/p&gt;

&lt;p&gt;2&lt;/p&gt;

&lt;p&gt;​&lt;/p&gt;

&lt;p&gt;3&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public static void main(String[] args) {
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;4&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    int sizeNumber = 2; // Replace with your desired size (1, 2, 3, 4, or 5)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;5&lt;/p&gt;

&lt;p&gt;​&lt;/p&gt;

&lt;p&gt;6&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    switch (sizeNumber) {
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;7&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        case 1:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;8&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            System.out.println("Extra Small");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;9&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            break;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;10&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        case 2:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;11&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            System.out.println("Small");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;12&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            break;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;13&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        case 3:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;14&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            System.out.println("Medium");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;15&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            break;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;16&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        case 4:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;17&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            System.out.println("Large");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;18&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            break;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;19&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        case 5:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;20&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            System.out.println("Extra Large");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;21&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            break;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;22&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        default:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;23&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            System.out.println("Invalid size number");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;24&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;25&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;26&lt;/p&gt;

&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;Output&lt;/p&gt;

&lt;p&gt;Small&lt;/p&gt;

&lt;p&gt;Some Important Rules for Java Switch Statements&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Case values must be constants or literals and of the same type as the switch expression.
Duplicate case values are not allowed.
The break statement is used to exit from the switch block. It is optional but recommended to prevent fall-through.
The default case is optional and executes if no case matches the switch expression. It can appear anywhere within the switch block.

Note: Starting from Java 7, switch statements can use String type values. They can also handle wrapper classes like Integer, Short, Byte, Long.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Flowchart of Switch-Case Statement &lt;/p&gt;

&lt;p&gt;This flowchart shows the control flow and working of switch statements:&lt;/p&gt;

&lt;p&gt;switch statement flowchart in java&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Note: Java switch statement is a fall through statement that means it executes all statements if break keyword is not used, so it is highly essential to use break keyword inside each case.  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Example: Finding Day&lt;/p&gt;

&lt;p&gt;Consider the following Java program, it declares an int named day whose value represents a day(1-7). The code displays the name of the day, based on the value of the day, using the switch statement.&lt;br&gt;
1&lt;/p&gt;

&lt;p&gt;// Java program to Demonstrate Switch Case&lt;/p&gt;

&lt;p&gt;2&lt;/p&gt;

&lt;p&gt;// with Primitive(int) Data Type&lt;/p&gt;

&lt;p&gt;3&lt;/p&gt;

&lt;p&gt;​&lt;/p&gt;

&lt;p&gt;4&lt;/p&gt;

&lt;p&gt;// Class&lt;/p&gt;

&lt;p&gt;5&lt;/p&gt;

&lt;p&gt;public class GFG {&lt;/p&gt;

&lt;p&gt;6&lt;/p&gt;

&lt;p&gt;​&lt;/p&gt;

&lt;p&gt;7&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Main driver method
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;8&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public static void main(String[] args)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;9&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;10&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    int day = 5;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;11&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    String dayString;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;12&lt;/p&gt;

&lt;p&gt;​&lt;/p&gt;

&lt;p&gt;13&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    // Switch statement with int data type
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;14&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    switch (day) {
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;15&lt;/p&gt;

&lt;p&gt;​&lt;/p&gt;

&lt;p&gt;16&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    // Case
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;17&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    case 1:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;18&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        dayString = "Monday";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;19&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        break;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;20&lt;/p&gt;

&lt;p&gt;​&lt;/p&gt;

&lt;p&gt;21&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    // Case
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;22&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    case 2:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;23&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        dayString = "Tuesday";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;24&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        break;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;25&lt;/p&gt;

&lt;p&gt;​&lt;/p&gt;

&lt;p&gt;26&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        // Case
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;27&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    case 3:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;28&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        dayString = "Wednesday";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;29&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        break;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;30&lt;/p&gt;

&lt;p&gt;​&lt;/p&gt;

&lt;p&gt;31&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        // Case
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;32&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    case 4:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;33&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        dayString = "Thursday";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;34&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        break;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;35&lt;/p&gt;

&lt;p&gt;​&lt;/p&gt;

&lt;p&gt;36&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    // Case
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;37&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    case 5:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;38&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        dayString = "Friday";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;39&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        break;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;40&lt;/p&gt;

&lt;p&gt;​&lt;/p&gt;

&lt;p&gt;41&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        // Case
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;42&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    case 6:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;43&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        dayString = "Saturday";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;44&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        break;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;45&lt;/p&gt;

&lt;p&gt;​&lt;/p&gt;

&lt;p&gt;46&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        // Case
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;47&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    case 7:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;48&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        dayString = "Sunday";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;49&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        break;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;50&lt;/p&gt;

&lt;p&gt;​&lt;/p&gt;

&lt;p&gt;51&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    // Default case
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;52&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    default:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;53&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        dayString = "Invalid day";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;54&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;55&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    System.out.println(dayString);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;56&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;57&lt;/p&gt;

&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;Output&lt;/p&gt;

&lt;p&gt;Friday&lt;/p&gt;

&lt;p&gt;Break in switch case Statements&lt;/p&gt;

&lt;p&gt;A break statement is optional. If we omit the break, execution will continue into the next case. &lt;/p&gt;

&lt;p&gt;It is sometimes desirable to have multiple cases without “break” statements between them. For instance, let us consider the updated version of the above program, it also displays whether a day is a weekday or a weekend day.&lt;br&gt;
Example: Switch statement program without multiple breaks&lt;br&gt;
1&lt;/p&gt;

&lt;p&gt;// Java Program to Demonstrate Switch Case&lt;/p&gt;

&lt;p&gt;2&lt;/p&gt;

&lt;p&gt;// with Multiple Cases Without Break Statements&lt;/p&gt;

&lt;p&gt;3&lt;/p&gt;

&lt;p&gt;​&lt;/p&gt;

&lt;p&gt;4&lt;/p&gt;

&lt;p&gt;// Class&lt;/p&gt;

&lt;p&gt;5&lt;/p&gt;

&lt;p&gt;public class GFG {&lt;/p&gt;

&lt;p&gt;6&lt;/p&gt;

&lt;p&gt;​&lt;/p&gt;

&lt;p&gt;7&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// main driver method
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;8&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public static void main(String[] args)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;9&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;10&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    int day = 2;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;11&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    String dayType;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;12&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    String dayString;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;13&lt;/p&gt;

&lt;p&gt;​&lt;/p&gt;

&lt;p&gt;14&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    // Switch case
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;15&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    switch (day) {
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;16&lt;/p&gt;

&lt;p&gt;​&lt;/p&gt;

&lt;p&gt;17&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    // Case
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;18&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    case 1:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;19&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        dayString = "Monday";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;20&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        break;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;21&lt;/p&gt;

&lt;p&gt;​&lt;/p&gt;

&lt;p&gt;22&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    // Case
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;23&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    case 2:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;24&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        dayString = "Tuesday";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;25&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        break;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;26&lt;/p&gt;

&lt;p&gt;​&lt;/p&gt;

&lt;p&gt;27&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        // Case
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;28&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    case 3:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;29&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        dayString = "Wednesday";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;30&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        break;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;31&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    case 4:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;32&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        dayString = "Thursday";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;33&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        break;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;34&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    case 5:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;35&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        dayString = "Friday";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;36&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        break;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;37&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    case 6:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;38&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        dayString = "Saturday";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;39&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        break;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;40&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    case 7:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;41&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        dayString = "Sunday";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;42&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        break;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;43&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    default:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;44&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        dayString = "Invalid day";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;45&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;46&lt;/p&gt;

&lt;p&gt;​&lt;/p&gt;

&lt;p&gt;47&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    switch (day) {
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;48&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        // Multiple cases without break statements
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;49&lt;/p&gt;

&lt;p&gt;​&lt;/p&gt;

&lt;p&gt;50&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    case 1:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;51&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    case 2:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;52&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    case 3:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;53&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    case 4:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;54&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    case 5:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;55&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        dayType = "Weekday";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;56&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        break;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;57&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    case 6:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;58&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    case 7:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;59&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        dayType = "Weekend";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;60&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        break;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;61&lt;/p&gt;

&lt;p&gt;​&lt;/p&gt;

&lt;p&gt;62&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    default:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;63&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        dayType = "Invalid daytype";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;64&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;65&lt;/p&gt;

&lt;p&gt;​&lt;/p&gt;

&lt;p&gt;66&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    System.out.println(dayString + " is a " + dayType);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;67&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;68&lt;/p&gt;

&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;Output&lt;/p&gt;

&lt;p&gt;Tuesday is a Weekday&lt;/p&gt;

&lt;p&gt;Java Nested Switch Statements&lt;/p&gt;

&lt;p&gt;We can use a switch as part of the statement sequence of an outer switch. This is called a nested switch. Since a switch statement defines its block, no conflicts arise between the case constants in the inner switch and those in the outer switch.&lt;br&gt;
Example: Nested Switch Statement&lt;br&gt;
1&lt;/p&gt;

&lt;p&gt;// Java Program to Demonstrate&lt;/p&gt;

&lt;p&gt;2&lt;/p&gt;

&lt;p&gt;// Nested Switch Case Statement&lt;/p&gt;

&lt;p&gt;3&lt;/p&gt;

&lt;p&gt;​&lt;/p&gt;

&lt;p&gt;4&lt;/p&gt;

&lt;p&gt;// Class&lt;/p&gt;

&lt;p&gt;5&lt;/p&gt;

&lt;p&gt;public class GFG {&lt;/p&gt;

&lt;p&gt;6&lt;/p&gt;

&lt;p&gt;​&lt;/p&gt;

&lt;p&gt;7&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Main driver method
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;8&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public static void main(String[] args)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;9&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;10&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    // Custom input string
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;11&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    String Branch = "CSE";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;12&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    int year = 2;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;13&lt;/p&gt;

&lt;p&gt;​&lt;/p&gt;

&lt;p&gt;14&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    // Switch case
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;15&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    switch (year) {
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;16&lt;/p&gt;

&lt;p&gt;​&lt;/p&gt;

&lt;p&gt;17&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    // Case
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;18&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    case 1:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;19&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        System.out.println(
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;20&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            "elective courses : Advance english, Algebra");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;21&lt;/p&gt;

&lt;p&gt;​&lt;/p&gt;

&lt;p&gt;22&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        // Break statement to hault execution here
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;23&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        // itself if case is matched
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;24&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        break;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;25&lt;/p&gt;

&lt;p&gt;​&lt;/p&gt;

&lt;p&gt;26&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        // Case
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;27&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    case 2:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;28&lt;/p&gt;

&lt;p&gt;​&lt;/p&gt;

&lt;p&gt;29&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        // Switch inside a switch
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;30&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        // Nested Switch
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;31&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        switch (Branch) {
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;32&lt;/p&gt;

&lt;p&gt;​&lt;/p&gt;

&lt;p&gt;33&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        // Nested case
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;34&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        case "CSE":
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;35&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        case "CCE":
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;36&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            System.out.println(
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;37&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                "elective courses : Machine Learning, Big Data");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;38&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            break;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;39&lt;/p&gt;

&lt;p&gt;​&lt;/p&gt;

&lt;p&gt;40&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        // Case
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;41&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        case "ECE":
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;42&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            System.out.println(
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;43&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                "elective courses : Antenna Engineering");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;44&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            break;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;45&lt;/p&gt;

&lt;p&gt;​&lt;/p&gt;

&lt;p&gt;46&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            // default case
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;47&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            // It will execute if above cases does not
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;48&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            // execute
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;49&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        default:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;50&lt;/p&gt;

&lt;p&gt;​&lt;/p&gt;

&lt;p&gt;51&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            // Print statement
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;52&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            System.out.println(
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;53&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                "Elective courses : Optimization");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;54&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;55&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;56&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;57&lt;/p&gt;

&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;Output&lt;/p&gt;

&lt;p&gt;elective courses : Machine Learning, Big Data&lt;/p&gt;

&lt;p&gt;Java Enum in Switch Statement&lt;/p&gt;

&lt;p&gt;Enums in Java are a powerful feature used to represent a fixed set of constants. They can be used in switch statements for better type safety and readability.&lt;br&gt;
Example: Use of Enum in Switch&lt;br&gt;
1&lt;/p&gt;

&lt;p&gt;// Java Program to Illustrate Use of Enum&lt;/p&gt;

&lt;p&gt;2&lt;/p&gt;

&lt;p&gt;// in Switch Statement&lt;/p&gt;

&lt;p&gt;3&lt;/p&gt;

&lt;p&gt;​&lt;/p&gt;

&lt;p&gt;4&lt;/p&gt;

&lt;p&gt;// Class&lt;/p&gt;

&lt;p&gt;5&lt;/p&gt;

&lt;p&gt;public class GFG {&lt;/p&gt;

&lt;p&gt;6&lt;/p&gt;

&lt;p&gt;​&lt;/p&gt;

&lt;p&gt;7&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Enum
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;8&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public enum Day { Sun, Mon, Tue, Wed, Thu, Fri, Sat }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;9&lt;/p&gt;

&lt;p&gt;​&lt;/p&gt;

&lt;p&gt;10&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Main driver method
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;11&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public static void main(String args[])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;12&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;13&lt;/p&gt;

&lt;p&gt;​&lt;/p&gt;

&lt;p&gt;14&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    // Enum
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;15&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    Day[] DayNow = Day.values();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;16&lt;/p&gt;

&lt;p&gt;​&lt;/p&gt;

&lt;p&gt;17&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    // Iterating using for each loop
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;18&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    for (Day Now : DayNow) {
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;19&lt;/p&gt;

&lt;p&gt;​&lt;/p&gt;

&lt;p&gt;20&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        // Switch case
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;21&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        switch (Now) {
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;22&lt;/p&gt;

&lt;p&gt;​&lt;/p&gt;

&lt;p&gt;23&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        // Case 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;24&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        case Sun:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;25&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            System.out.println("Sunday");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;26&lt;/p&gt;

&lt;p&gt;​&lt;/p&gt;

&lt;p&gt;27&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            // break statement that hault further
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;28&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            // execution once case is satisfied
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;29&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            break;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;30&lt;/p&gt;

&lt;p&gt;​&lt;/p&gt;

&lt;p&gt;31&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        // Case 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;32&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        case Mon:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;33&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            System.out.println("Monday");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;34&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            break;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;35&lt;/p&gt;

&lt;p&gt;​&lt;/p&gt;

&lt;p&gt;36&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        // Case 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;37&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        case Tue:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;38&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            System.out.println("Tuesday");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;39&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            break;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;40&lt;/p&gt;

&lt;p&gt;​&lt;/p&gt;

&lt;p&gt;41&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        // Case 4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;42&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        case Wed:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;43&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            System.out.println("Wednesday");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;44&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            break;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;45&lt;/p&gt;

&lt;p&gt;​&lt;/p&gt;

&lt;p&gt;46&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        // Case 5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;47&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        case Thu:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;48&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            System.out.println("Thursday");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;49&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            break;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;50&lt;/p&gt;

&lt;p&gt;​&lt;/p&gt;

&lt;p&gt;51&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        // Case 6
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;52&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        case Fri:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;53&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            System.out.println("Friday");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;54&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            break;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;55&lt;/p&gt;

&lt;p&gt;​&lt;/p&gt;

&lt;p&gt;56&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        // Case 7
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;57&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        case Sat:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;58&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            System.out.println("Saturday");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;59&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;60&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;61&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;62&lt;/p&gt;

&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;Output&lt;/p&gt;

&lt;p&gt;Sunday&lt;br&gt;
Monday&lt;br&gt;
Tuesday&lt;br&gt;
Wednesday&lt;br&gt;
Thursday&lt;br&gt;
Friday&lt;br&gt;
Saturday&lt;/p&gt;

&lt;p&gt;Default statement in Java Switch Case&lt;/p&gt;

&lt;p&gt;The default case in a switch statement specifies the code to run if no other case matches. It can be placed at any position in the switch block but is commonly placed at the end.&lt;br&gt;
Example: Writing default in the middle of switch statements:&lt;br&gt;
1&lt;/p&gt;

&lt;p&gt;import java.io.*;&lt;/p&gt;

&lt;p&gt;2&lt;/p&gt;

&lt;p&gt;​&lt;/p&gt;

&lt;p&gt;3&lt;/p&gt;

&lt;p&gt;class GFG {&lt;/p&gt;

&lt;p&gt;4&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public static void main (String[] args) {
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;5&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    int i=2;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;6&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;      switch(i){
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;7&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;      default:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;8&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        System.out.println("Default");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;9&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;      case 1:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;10&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        System.out.println(1);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;11&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        break;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;12&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;      case 2:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;13&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        System.out.println(2);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;14&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;      case 3:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;15&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        System.out.println(3);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;16&lt;/p&gt;

&lt;p&gt;17&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;18&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;19&lt;/p&gt;

&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;Output&lt;/p&gt;

&lt;p&gt;2&lt;br&gt;
3&lt;/p&gt;

&lt;p&gt;Example: Writing Default at the Beginning of Switch Statements&lt;br&gt;
1&lt;/p&gt;

&lt;p&gt;import java.io.*;&lt;/p&gt;

&lt;p&gt;2&lt;/p&gt;

&lt;p&gt;​&lt;/p&gt;

&lt;p&gt;3&lt;/p&gt;

&lt;p&gt;class GFG {&lt;/p&gt;

&lt;p&gt;4&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public static void main(String[] args)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;5&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;6&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    int i = 5;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;7&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    switch (i) {
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;8&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    default:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;9&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        System.out.println("Default");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;10&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    case 1:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;11&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        System.out.println(1);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;12&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        break;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;13&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    case 2:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;14&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        System.out.println(2);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;15&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    case 3:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;16&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        System.out.println(3);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;17&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;18&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;19&lt;/p&gt;

&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;Output&lt;/p&gt;

&lt;p&gt;Default&lt;br&gt;
1&lt;/p&gt;

&lt;p&gt;Case label variations&lt;/p&gt;

&lt;p&gt;Case labels and switch arguments can be constant expressions. The switch argument can be a variable expression but the case labels must be constant expressions.&lt;/p&gt;

&lt;p&gt;Example: Using Variable in Switch Argument&lt;br&gt;
1&lt;/p&gt;

&lt;p&gt;import java.io.*;&lt;/p&gt;

&lt;p&gt;2&lt;/p&gt;

&lt;p&gt;​&lt;/p&gt;

&lt;p&gt;3&lt;/p&gt;

&lt;p&gt;class GFG {&lt;/p&gt;

&lt;p&gt;4&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public static void main(String[] args)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;5&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;6&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    int x = 2;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;7&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    switch (x + 1) {
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;8&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    case 1:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;9&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        System.out.println(1);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;10&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        break;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;11&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    case 1 + 1:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;12&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        System.out.println(2);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;13&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        break;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;14&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    case 2 + 1:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;15&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        System.out.println(3);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;16&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        break;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;17&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    default:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;18&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        System.out.println("Default");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;19&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;20&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;21&lt;/p&gt;

&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;Output&lt;/p&gt;

&lt;p&gt;3&lt;/p&gt;

&lt;p&gt;Example: Case Label Cannot Be Variable&lt;/p&gt;

&lt;p&gt;A case label cannot be a variable or variable expression. It must be a constant expression.&lt;br&gt;
1&lt;/p&gt;

&lt;p&gt;import java.io.*;&lt;/p&gt;

&lt;p&gt;2&lt;/p&gt;

&lt;p&gt;​&lt;/p&gt;

&lt;p&gt;3&lt;/p&gt;

&lt;p&gt;class GFG {&lt;/p&gt;

&lt;p&gt;4&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public static void main(String[] args)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;5&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;6&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    int x = 2;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;7&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    int y = 1;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;8&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    switch (x) {
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;9&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    case 1:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;10&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        System.out.println(1);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;11&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        break;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;12&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    case 2:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;13&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        System.out.println(2);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;14&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        break;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;15&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    case x + y:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;16&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        System.out.println(3);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;17&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        break;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;18&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    default:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;19&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        System.out.println("Default");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;20&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;21&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;22&lt;/p&gt;

&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;./GFG.java:16: error: constant expression required&lt;br&gt;
          case x+y:&lt;br&gt;
                ^&lt;br&gt;
1 error&lt;/p&gt;

&lt;p&gt;Java Wrapper in Switch Statements&lt;/p&gt;

&lt;p&gt;Java allows the use of wrapper classes (Integer, Short, Byte, Long, and Character) in switch statements. This provides flexibility when dealing with primitive data types and their corresponding wrapper types.&lt;br&gt;
Example: Using Wrapper Classes&lt;br&gt;
1&lt;/p&gt;

&lt;p&gt;public class WrapperSwitchExample {&lt;/p&gt;

&lt;p&gt;2&lt;/p&gt;

&lt;p&gt;​&lt;/p&gt;

&lt;p&gt;3&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public static void main(String[] args) {
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;4&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    Integer age = 25;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;5&lt;/p&gt;

&lt;p&gt;​&lt;/p&gt;

&lt;p&gt;6&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    switch (age) { // Extract primitive value for switch
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;7&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        case 25:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;8&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            System.out.println("You are 25.");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;9&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            break;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;10&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        case 30:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;11&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            System.out.println("You are 30.");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;12&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            break;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;13&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        default:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;14&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            System.out.println("Age not matched.");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;15&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;16&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;17&lt;/p&gt;

&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;Output&lt;/p&gt;

&lt;p&gt;You are 25.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Note: Regardless of its placement, the default case only gets executed if none of the other case conditions are met. So, putting it at the beginning, middle, or end doesn’t change the core logic (unless you’re using a less common technique called fall-through).
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Example: Using Character Wrapper&lt;br&gt;
1&lt;/p&gt;

&lt;p&gt;public class WrapperSwitchExample {&lt;/p&gt;

&lt;p&gt;2&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public static void main(String[] args) {
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;3&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    Character ch = 'c';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;4&lt;/p&gt;

&lt;p&gt;​&lt;/p&gt;

&lt;p&gt;5&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    switch (ch) { // Extract primitive value for switch
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;6&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        case 'a':
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;7&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            System.out.println("You are a.");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;8&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            break;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;9&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        case 'c':
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;10&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            System.out.println("You are c.");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;11&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            break;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;12&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        default:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;13&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            System.out.println("Character not matched.");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;14&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;15&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;16&lt;/p&gt;

&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;Output&lt;/p&gt;

&lt;p&gt;You are c.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Read More:

    Usage of Enum and Switch Keyword in Java
    String in Switch Case in Java
    Java Tutorial 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Exercise:&lt;/p&gt;

&lt;p&gt;To practice Java switch statements you can visit the page: Java Switch Case statement Practice&lt;br&gt;
Conclusion&lt;/p&gt;

&lt;p&gt;Switch statements in Java are control flow structures that allow you to execute specific blocks of code based on the value of a single expression. They can be considered an alternative to if-else-if statements and are useful for handling multiple conditions in a clean and readable manner.&lt;br&gt;
Java Switch Statements- FAQs&lt;br&gt;
How to use switch statements in Java?&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;To use switch statement in Java, you can use the following syntax:

switch (expression) {
   case value1:
       // code to execute if expression equals value1
       break;
   case value2:
       // code to execute if expression equals value2
       break;
   // … more cases
   default:
       // code to execute if none of the above cases match
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;*&lt;em&gt;Can we pass null to a switch?&lt;br&gt;
*&lt;/em&gt;&lt;br&gt;
    No, you can not pass NULL to a switch statement as they require constant expression in its case.&lt;/p&gt;

&lt;p&gt;*Can you return to a switch statement?&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;No, switch statements build a control flow in the program, so it can not go back after exiting a switch case.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Flowchart of Switch-Case Statement:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxxyskrjw9os5p79ptadj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxxyskrjw9os5p79ptadj.png" alt="Image description" width="702" height="971"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5wngwnbcbimgzkhf2nnl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5wngwnbcbimgzkhf2nnl.png" alt="Image description" width="702" height="971"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Example program:

int day = 4;
switch (day) {
  case 1:
    System.out.println("Monday");
    break;
  case 2:
    System.out.println("Tuesday");
    break;
  case 3:
    System.out.println("Wednesday");
    break;
  case 4:
    System.out.println("Thursday");
    break;
  case 5:
    System.out.println("Friday");
    break;
  case 6:
    System.out.println("Saturday");
    break;
  case 7:
    System.out.println("Sunday");
    break;
}
// Outputs "Thursday" (day 4)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;*&lt;em&gt;Some Important Rules for Java Switch Statements&lt;br&gt;
*&lt;/em&gt;&lt;br&gt;
    Case values must be constants or literals and of the same type as the switch expression.&lt;br&gt;
    Duplicate case values are not allowed.&lt;br&gt;
    The break statement is used to exit from the switch block. It is optional but recommended to prevent fall-through.&lt;br&gt;
    The default case is optional and executes if no case matches the switch expression. It can appear anywhere within the switch block.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Break in switch case Statements:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A break statement is optional. If we omit the break, execution will continue into the next case. &lt;/p&gt;

&lt;p&gt;It is sometimes desirable to have multiple cases without “break” statements between them.&lt;/p&gt;

</description>
      <category>java</category>
      <category>programming</category>
    </item>
    <item>
      <title>Switch and Ternary operator:</title>
      <dc:creator>Kaviya.k</dc:creator>
      <pubDate>Sun, 02 Feb 2025 11:05:44 +0000</pubDate>
      <link>https://dev.to/kaviyak/switch-and-ternary-operator-2cme</link>
      <guid>https://dev.to/kaviyak/switch-and-ternary-operator-2cme</guid>
      <description>&lt;p&gt;&lt;strong&gt;Java Switch Statements:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Instead of writing many if..else    statements, you can use the switch statement.&lt;/p&gt;

&lt;p&gt;Note: The working of the switch-case statement is similar to the Java if...else...if ladder. However, the syntax of the switch statement is cleaner and much easier to read and write.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How does the switch-case statement work?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The expression is evaluated once and compared with the values of each case.&lt;/p&gt;

&lt;p&gt;If expression matches with value1, the code of case value1 are executed. Similarly, the code of case value2 is executed if expression matches with value2&lt;/p&gt;

&lt;p&gt;If there is no match, the code of the default case is executed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Flow chart:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuhhu8gtgsl9q1zcftc65.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuhhu8gtgsl9q1zcftc65.png" alt="Image description" width="702" height="971"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;switch(expression)&lt;br&gt;
{&lt;br&gt;
  case value1 :&lt;br&gt;
     // Statements&lt;br&gt;
     break; // break is optional&lt;/p&gt;

&lt;p&gt;case value2 :&lt;br&gt;
     // Statements&lt;br&gt;
     break; // break is optional&lt;br&gt;
     ....&lt;br&gt;
     ....&lt;br&gt;
     ....&lt;br&gt;
   default : &lt;br&gt;
     // default Statement&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Important Rules for Java Switch Statements:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Case values must be constants or literals and of the same type as the switch expression.&lt;br&gt;
   Duplicate case values are not allowed.&lt;br&gt;
   The break statement is used to exit from the switch block. It is optional but recommended to prevent fall-through.&lt;br&gt;
   The default case is optional and executes if no case matches the switch expression. It can appear anywhere within the switch block.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Example program:

public class Month
{
public static void main(String args[])
{
int day = 4;
switch (day) {
  case 1:
    System.out.println("Monday");
    break;
  case 2:
    System.out.println("Tuesday");
    break;
  case 3:
    System.out.println("Wednesday");
    break;
  case 4:
    System.out.println("Thursday");
    break;
  case 5:
    System.out.println("Friday");
    break;
  case 6:
    System.out.println("Saturday");
    break;
  case 7:
    System.out.println("Sunday");
    break;
}
}
}
output:
   Thursday   

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Break Keyword:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When Java reaches a break keyword, it breaks out of the switch block.&lt;/p&gt;

&lt;p&gt;This will stop the execution of more code and case testing inside the block.&lt;/p&gt;

&lt;p&gt;When a match is found, and the job is done, it's time for a break. There is no need for more testing.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Example program:

public class Break {
    public static void main(String[] args) {
        int day = 3;
        switch (day) {
            case 1:
                System.out.println("Monday");
                break;
            case 2:
                System.out.println("Tuesday");
                break;
            case 3:
                System.out.println("Wednesday");
                break; // Terminate switch after matching case
            case 4:
                System.out.println("Thursday");
                break;
            default:
                System.out.println("Invalid day");
        }
    }
}

output:
  wednesday

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Default Case in Java switch-case&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The switch statement also includes an optional default case. It is executed when the expression doesn't match any of the cases.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;For example:

public class Main {
  public static void main(String[] args) {

    int expression = 9;

    switch(expression) {

      case 2:
        System.out.println("Small Size");
        break;

      case 3:
        System.out.println("Large Size");
        break;

      // default case
      default:
        System.out.println("Unknown Size");
    }
  }
}

output:
Unknown Size

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Java Enum in switch statement:&lt;/strong&gt;(TBD)&lt;/p&gt;

&lt;p&gt;An Enum is a unique type of data type in java which is generally a collection (set) of constants. More specifically, a Java Enum type is a unique kind of Java class. An Enum can hold constants, methods, etc.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Example program:
enum Car {
   lamborghini,tata,audi,fiat,honda
}
public class Main1{
   public static void main(String args[]){
      Car c;
      c = Car.tata;
      switch(c) {
         case lamborghini:
            System.out.println("You choose lamborghini!");
            break;
         case tata:
            System.out.println("You choose tata!");
            break;
         case audi:
            System.out.println("You choose audi!");
            break;
         case fiat:
            System.out.println("You choose fiat!");
            break;
         case honda:
            System.out.println("You choose honda!");
            break;
         default:
            System.out.println("I don't know your car.");
            break;
      }
   }
}

output:
You choose tata!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Java switch statement with String:&lt;/strong&gt;(TBD)&lt;/p&gt;

&lt;p&gt;Java allows you to use string objects in the expression of switch statement. In order to use string&lt;/p&gt;

&lt;p&gt;It must be only string object.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Object game = "Hockey"; // It is not allowed  
    String game = "Hockey"; // It is OK.  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;String object is case sensitive.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"Hickey" and "hocker" are not equal.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Example program:

// Java Program to implement String on switch statements in Java

class Main {
  public static void main(String[] args) {

    // create a string
    String language = "Java";

    switch(language) {

      case "Java":
        System.out.println(language + " is famous for enterprise applications.");
        break;

      case "JavaScript":
        System.out.println(language + " is famous for frontend and backend.");
        break;

      case "Python":
        System.out.println(language + " is famous for ML and AI.");
        break;

      default:
        System.out.println(language + " not found on record.");
        break;
    }
  }
}

output:
Java is famous for enterprise applications.


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Ternary operator:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Java ternary operator is the only conditional operator that takes three operands. It’s a one-liner replacement for the if-then-else statement and is used a lot in Java programming. We can use the ternary operator in place of if-else conditions or even switch conditions using nested ternary operators. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuvu92tmrymszfjwnyld4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuvu92tmrymszfjwnyld4.png" alt="Image description" width="800" height="493"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax:&lt;/strong&gt;&lt;br&gt;
variable = Expression1 ? Expression2: Expression3&lt;/p&gt;

&lt;p&gt;If operates similarly to that of the if-else statement as in Exression2 is executed if Expression1 is true else Expression3 is executed.  &lt;/p&gt;

&lt;p&gt;if(Expression1)&lt;br&gt;
{&lt;br&gt;
    variable = Expression2;&lt;br&gt;
}&lt;br&gt;
else&lt;br&gt;
{&lt;br&gt;
    variable = Expression3;&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Flowchart of Ternary Operation:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq7rvoj2dw6jhnkm8u5d7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq7rvoj2dw6jhnkm8u5d7.png" alt="Image description" width="800" height="954"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Example program:

class Ternary {
    public static void main(String[] args)
    {

        // variable declaration
        int n1 = 5, n2 = 10, max;

        System.out.println("First num: " + n1);
        System.out.println("Second num: " + n2);

        // Largest among n1 and n2
        max = (n1 &amp;gt; n2) ? n1 : n2;

        // Print the largest number
        System.out.println("Maximum is = " + max);
    }
}

output:
First num: 5
Second num: 10
Maximum is = 10

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;References:&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://www.geeksforgeeks.org/switch-statement-in-java/" rel="noopener noreferrer"&gt;https://www.geeksforgeeks.org/switch-statement-in-java/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.w3schools.com/java/java_switch.asp" rel="noopener noreferrer"&gt;https://www.w3schools.com/java/java_switch.asp&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.javatpoint.com/java-switch" rel="noopener noreferrer"&gt;https://www.javatpoint.com/java-switch&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>programming</category>
    </item>
    <item>
      <title>if else and nested if in java:</title>
      <dc:creator>Kaviya.k</dc:creator>
      <pubDate>Thu, 30 Jan 2025 18:38:59 +0000</pubDate>
      <link>https://dev.to/kaviyak/if-else-and-nested-if-in-java-28m5</link>
      <guid>https://dev.to/kaviyak/if-else-and-nested-if-in-java-28m5</guid>
      <description>&lt;p&gt;&lt;strong&gt;If statement:&lt;/strong&gt;&lt;br&gt;
                if a certain condition is true then a block of statements is executed otherwise not.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhxr5fbu5sdp3y589rm52.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhxr5fbu5sdp3y589rm52.png" alt="Image description" width="440" height="525"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If (condition){&lt;br&gt;
//code to be executed&lt;br&gt;
}&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;program:
public class If {  
public static void main(String[] args) {  
      float a =2.2f; 
      float b =5.4f; 
        if(b&amp;gt;a)
         {  
          System.out.println(" B is greater than a");  
           }  
       }  
}  


Output:
B is greater than a
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;if-else statement: &lt;/strong&gt;&lt;br&gt;
                 In Java, the if-else condition is a set of rules or statements,It executes one block of code if a condition is true and another block if the condition is false. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkdfw6dzlxc5l3f97txjf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkdfw6dzlxc5l3f97txjf.png" alt="Image description" width="456" height="544"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If(condition){&lt;br&gt;
// code if condition is true&lt;br&gt;
}else{&lt;br&gt;
//code if condition is false&lt;br&gt;
}&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;**program**:

public class IfElse {

    public static void main(String[] args) {

        int n = 3;

        if (n &amp;gt; 5) {
            System.out.println("The number is greater than 5.");
        } 
         else  
        {
            System.out.println("The number is 5 or less.");
        }
    }
}


Output:
The number is 5 or less.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The else if Statement:&lt;/p&gt;

&lt;p&gt;Use the else if statement to specify a new condition if the first condition is false.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2xx28r0xx1dnwhru7b35.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2xx28r0xx1dnwhru7b35.png" alt="Image description" width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Syntax:&lt;br&gt;
if (condition1) {&lt;br&gt;
  //  block of code to be executed if condition1 is true&lt;br&gt;
} else if (condition2) {&lt;br&gt;
  //  block of code to be executed if the condition1 is false and condition2 is true&lt;br&gt;
} else {&lt;br&gt;
  //  block of code to be executed if the condition1 is false and condition2 is false&lt;br&gt;
}&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;**program:**

public class Greater
{
public static void main (String args[])
{
int no1 = 1000; 
        int no2 = 1000; 
        if(no1&amp;gt;no2)
          {
            System.out.println("no1 is greater");
          }
        else if(no2&amp;gt;no1)
         {
              System.out.println("no2 is greater");
         }      
        else 
           {
         System.out.println("Both are equal");
           }
}
}

output:
     Both are equal

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Nested if in Java&lt;/strong&gt;&lt;br&gt;
                Refers to having one if statement inside another if statement. If the outer condition is true the inner conditions are checked and executed accordingly. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmuz59koz36p9z2w6up3z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmuz59koz36p9z2w6up3z.png" alt="Image description" width="203" height="700"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;*&lt;em&gt;Syntax of Nested if *&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;if (condition1) {&lt;br&gt;
if (condition2) {&lt;br&gt;
if (condition3) {&lt;br&gt;
// statements;&lt;br&gt;
}&lt;br&gt;
}&lt;br&gt;
}&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;program:

    public class nested if
    {
    public static void main(String args[])
    {
        int a = 10;
        int b = 20;

        // Outer if condition
        if (a == 10) {

            // Inner if condition
            if (b != 20) {
                System.out.println("GeeksforGeeks");
            }

            else {
                System.out.println("GFG");
            }
        }
    }
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Task:&lt;/strong&gt; program:1&lt;/p&gt;

&lt;h2&gt;
  
  
  java
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Age
{
public static void main (String args[])
{
 int age1=25;
 int age2=21;
    if(age1&amp;lt;age2)
{
       System.out.println("The condition is false");
}
    else 
{
       System.out.println("The condition is true");
}
}
}

output:
 The condition is true

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  python
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;age1 = 25
age2 = 21

if age1 &amp;lt; age2:
    print("The condition is false")
elif age2 &amp;lt; age1:
    print("The condition is true")

output:
The condition is true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  java
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;program:2

public class If {  
public static void main(String[] args) {  
      float a =2.2f; 
      float b =5.4f; 
        if(b&amp;gt;a)
         {  
          System.out.println(" B is greater than a");  
           }  
       }  
}  

output:
B is greater than a

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  python
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;a = 2.2
      b = 5.4
if b &amp;gt; a:
    print("B is greater than a")

output:
  num is greater than or equal 120

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  java
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;program:3

public class Greater
{
public static void main (String args[])
{
int no1 = 1000; 
        int no2 = 1000; 
        if(no1&amp;gt;no2)
          {
        System.out.println("no1 is greater");
          }
        else if(no2&amp;gt;no1)
         {
           System.out.println("no2 is greater");
         }      
        else 
           {
      System.out.println("Both are equal");
           }
}
}

output:
Both are equal

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  python
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;no1 = 1000
no2 = 1000

if no1 &amp;gt; no2:
    print("no1 is greater")
elif no2 &amp;gt; no1:
    print("no2 is greater")
else:
    print("Both are equal")

output:
Both are equal

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;References:&lt;a href="https://www.javatpoint.com/java-if-else" rel="noopener noreferrer"&gt;https://www.javatpoint.com/java-if-else&lt;/a&gt;&lt;br&gt;
References:&lt;a href="https://www.geeksforgeeks.org/java-if-else-statement-with-examples/" rel="noopener noreferrer"&gt;https://www.geeksforgeeks.org/java-if-else-statement-with-examples/&lt;/a&gt;&lt;br&gt;
References:&lt;a href="https://www.w3schools.com/java/java_conditions.asp" rel="noopener noreferrer"&gt;https://www.w3schools.com/java/java_conditions.asp&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
