# Loops execution

**URL:** <https://forum.codewithmosh.com/t/loops-execution/15242>\
**Category:** Java\
**Created:** [September 17, 2022, 5:00pm UTC](https://forum.codewithmosh.com/t/loops-execution/15242 "2022-09-17T17:00:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dppraveen](https://avatars.discourse-cdn.com/v4/letter/d/50afbb/32.png) [@dppraveen](https://forum.codewithmosh.com/u/dppraveen)\
**Post date:** [September 17, 2022, 5:00pm UTC](https://forum.codewithmosh.com/t/loops-execution/15242/1 "2022-09-17T17:00:51Z")

</div>

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

code:  
//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");
        i++;
    }

```

output i got is:

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

can anyone clear me on this

---

<div class="post-metadata">

**Author:** ![jmrunkle](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/jmrunkle/32/3765_2.png) [@jmrunkle](https://forum.codewithmosh.com/u/jmrunkle)\
**Post date:** [September 17, 2022, 5:20pm UTC](https://forum.codewithmosh.com/t/loops-execution/15242/2 "2022-09-17T17:20:52Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dppraveen](https://avatars.discourse-cdn.com/v4/letter/d/50afbb/32.png) [@dppraveen](https://forum.codewithmosh.com/u/dppraveen)\
**Post date:** [September 17, 2022, 5:41pm UTC](https://forum.codewithmosh.com/t/loops-execution/15242/3 "2022-09-17T17:41:07Z")

</div>

Ohh, ok will check tht. thanks jason

---

<div class="post-metadata">

**Author:** ![dppraveen](https://avatars.discourse-cdn.com/v4/letter/d/50afbb/32.png) [@dppraveen](https://forum.codewithmosh.com/u/dppraveen)\
**Post date:** [September 17, 2022, 5:42pm UTC](https://forum.codewithmosh.com/t/loops-execution/15242/4 "2022-09-17T17:42:21Z")

</div>

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