Loops execution

I’m trying to print a statement using while loop, but I’m not getting output.

//for Loop
for (int i=0; i<5; i++)
System.out.println(“Hello World!”);

    //while Loop
    int i=0;
    while (i>0) {
        System.out.println("Using while loops now");

output i got is:

Hello World!
Hello World!
Hello World!
Hello World!
Hello World!

can anyone clear me on this

So the problem is that your loop condition is not met the first time it is encountered. You set the value of i to 0, so when the loop starts, it checks the condition i > 0 and 0 is not greater than 0 so the loop exits immediately without executing.

If you change the loop condition to something like i < 5 you should see it print “Using while loops now” several times as you probably expect.


Ohh, ok will check tht. thanks jason

yes, now im getting the output. thanks for the explanation

1 Like