DEV Community

Saravanan
Saravanan

Posted on

1

While loop examples

(1)

                int count = 0;
        while(count<5)
        {
            System.out.println("1 2 3 4 5");
            count = count + 1;
        }

//      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 
Enter fullscreen mode Exit fullscreen mode

(2)

                int count = 1;
        while (count <=5)
        {
            System.out.println("1 0 1 0 1");
            count = count + 1;
        }

//      output:1 0 1 0 1
//             1 0 1 0 1
//             1 0 1 0 1
//             1 0 1 0 1
//             1 0 1 0 1
Enter fullscreen mode Exit fullscreen mode

(3) - Chocolate

                int chocolate = 15;
        int wrapper = 15;
        while (wrapper >=3)
        {
            wrapper = wrapper - 3;
            chocolate = chocolate + 1;
            wrapper = wrapper + 1;

        }
        System.out.println(chocolate);

//      output: 22
Enter fullscreen mode Exit fullscreen mode

(4) - Dosai count

                int total_balance = 8;
        int count = 1;
        while(count<=3)
        {
        int dosai_count =total_balance/2;
        total_balance = total_balance + dosai_count;
        count = count + 1;
        }
        {
        System.out.println(total_balance);
        }

//      output: 27
Enter fullscreen mode Exit fullscreen mode

(5) - 3 tables

int no = 1;
        int count = 1;
        while(no<=10)
        {
        int j = no*3;
        System.out.println(no +"*"+3+"="+j);
        no = no+ 1;
        count = count + 1;
        }

//      output: 1*3=3
//              2*3=6
//              3*3=9
//              4*3=12
//              5*3=15
//              6*3=18
//              7*3=21
//              8*3=24
//              9*3=27
//              10*3=30
Enter fullscreen mode Exit fullscreen mode

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay