# While Loop not looping

**URL:** <https://forum.makecode.com/t/while-loop-not-looping/542>\
**Category:** EV3\
**Created:** [September 16, 2019, 5:16pm UTC](https://forum.makecode.com/t/while-loop-not-looping/542 "2019-09-16T17:16:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![SusanMcN](https://avatars.discourse-cdn.com/v4/letter/s/fbc32d/32.png) [@SusanMcN](https://forum.makecode.com/u/SusanMcN)\
**Post date:** [September 16, 2019, 5:16pm UTC](https://forum.makecode.com/t/while-loop-not-looping/542/1 "2019-09-16T17:16:49Z")

</div>

I’m trying to get up to speed on MakeCode for EV3 for FLL. It appears that the While Loop doesn’t actually loop on the EV3 or the simulator. When I make a While Loop for the motor to run when the button on the Touch Sensor = pushed, it won’t stop running when the Touch Sensor /= pushed. From what I can tell, instead of being a loop, the While Loop is acting more like an If True logic block.

Here’s my code:

```auto
pauseUntil(() => sensors.touch1.isPressed())
while (sensors.touch1.isPressed()) {
    motors.largeBC.steer(0, 50)
}

```

---

<div class="post-metadata">

**Author:** ![NightIsland](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/nightisland/32/349_2.png) [@NightIsland](https://forum.makecode.com/u/NightIsland)\
**Post date:** [September 16, 2019, 6:01pm UTC](https://forum.makecode.com/t/while-loop-not-looping/542/2 "2019-09-16T18:01:07Z")

</div>

Out of curiosity, could you reorganise that loop to test a variable in the while, and then update the variable inside the loop…?

i.e.:

```auto
test=sensors.touch1.isPressed()
while (test) {
    motors.largeBC.steer(0,50)
    test=sensors.touch1.isPressed()
}

```

If that works, then it sounds like the while code is cacheing the result of that isPressed() function call.  
Not terribly useful to you, but might be a quick workaround?

---

<div class="post-metadata">

**Author:** ![mmoskal](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/mmoskal/32/20_2.png) [@mmoskal](https://forum.makecode.com/u/mmoskal)\
**Post date:** [September 16, 2019, 6:44pm UTC](https://forum.makecode.com/t/while-loop-not-looping/542/3 "2019-09-16T18:44:52Z")

</div>

My guess would be that steer is permanent, ie it keeps driving until you call steer(0,0). Try adding that after the loop.

---

<div class="post-metadata">

**Author:** ![mmoskal](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/mmoskal/32/20_2.png) [@mmoskal](https://forum.makecode.com/u/mmoskal)\
**Post date:** [September 16, 2019, 6:47pm UTC](https://forum.makecode.com/t/while-loop-not-looping/542/4 "2019-09-16T18:47:29Z")

</div>

A better way to write this would be to have pause until pressed, steer 50, pause until not pressed, steer 0. A yet better way would be to use event handlers instead of while loop.

---

<div class="post-metadata">

**Author:** ![SusanMcN](https://avatars.discourse-cdn.com/v4/letter/s/fbc32d/32.png) [@SusanMcN](https://forum.makecode.com/u/SusanMcN)\
**Post date:** [September 16, 2019, 7:06pm UTC](https://forum.makecode.com/t/while-loop-not-looping/542/5 "2019-09-16T19:06:26Z")

</div>

Well, those solutions didn’t really work with the ev3 blocks in MakeCode upon conversion. But your suggestions did lead me to a possible work around. I added in what Ev3 folks would call “wait blocks” (called Pause in MakeCode) in lieu of using the While Loop. Here’s what I’ve got.

```auto
pauseUntil(() => brick.buttonEnter.isPressed())
motors.largeBC.tank(50, 50)
pause(100)
pauseUntil(() => sensors.touch1.isPressed())
motors.stopAll()

```

---

<div class="post-metadata">

**Author:** ![peli](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/peli/32/12_2.png) [@peli](https://forum.makecode.com/u/peli)\
**Post date:** [September 18, 2019, 8:18pm UTC](https://forum.makecode.com/t/while-loop-not-looping/542/6 "2019-09-18T20:18:31Z")

</div>

@SusanMcN please again your original program at [https://makecode.mindstorms.com/beta](https://makecode.mindstorms.com/beta) . We have made fixes to the runtime to handle this kind of loops.
