# Problem with FOR loop

**URL:** <https://community.br-automation.com/t/problem-with-for-loop/2364>\
**Category:** Programming\
**Created:** [February 27, 2024, 9:19am UTC](https://community.br-automation.com/t/problem-with-for-loop/2364 "2024-02-27T09:19:46Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![limhyeonsoo](https://avatars.discourse-cdn.com/v4/letter/l/FF8800/32.png) [@limhyeonsoo](https://community.br-automation.com/u/limhyeonsoo)\
**Post date:** [February 27, 2024, 9:19am UTC](https://community.br-automation.com/t/problem-with-for-loop/2364/1 "2024-02-27T09:19:46Z")

</div>

I think what’s in for is the same as what I handwritten 1 to 6 in if statements  
But there’s a different move. Where did it go wrong?  
The code inside was written by copying and pasting  
When I alternated between the two versions, only the ones written in if (from 1 to 6) worked properly

When I pressed gxUpButtonPress or gxDownButtonPress (i is the floor of the building), I wanted gxElevatorA.moving to be true  
So I wrote it in a form, but it didn’t go as I wanted  
[https://www.youtube.com/watch?v=NJzwzwP1r-k](https://www.youtube.com/watch?v=NJzwzwP1r-k)  
[bandicam 2024 02 27 17 36 02 141 (youtube.com)](https://www.youtube.com/watch?v=dC4A-GG-cFY)  
[bandicam 2024 02 27 17 38 19 223 복사본 (youtube.com)](https://www.youtube.com/watch?v=c8P4Dy6RNV0)

---

<div class="post-metadata">

**Author:** ![kovarj](https://dub1.discourse-cdn.com/brcommunity/user_avatar/community.br-automation.com/kovarj/32/537_2.png) [@kovarj](https://community.br-automation.com/u/kovarj)\
**Post date:** [February 27, 2024, 11:07am UTC](https://community.br-automation.com/t/problem-with-for-loop/2364/2 "2024-02-27T11:07:38Z")

</div>

Hi @limhyeonsoo I’m not patient enough to watch all your videos. I would need source code backup on my PC or online meeting for that. But what about to use debugger to step by step your code execution, I think this would help a lot in this case or what do you think? [B&R Online Help](https://help.br-automation.com/#/en/4/diagnostics_support%2Fdiagnosis%2Fdebugging%2Fdiagnosis_debugging.html)

---

<div class="post-metadata">

**Author:** ![limhyeonsoo](https://avatars.discourse-cdn.com/v4/letter/l/FF8800/32.png) [@limhyeonsoo](https://community.br-automation.com/u/limhyeonsoo)\
**Post date:** [February 27, 2024, 2:02pm UTC](https://community.br-automation.com/t/problem-with-for-loop/2364/3 "2024-02-27T14:02:05Z")

</div>

[https://youtu.be/-uyww90xAas](https://youtu.be/-uyww90xAas)  
When I watch debugging, when the ‘i’ is 1, it doesn’t work the way I want it to in upbuttonpress

---

<div class="post-metadata">

**Author:** ![kovarj](https://dub1.discourse-cdn.com/brcommunity/user_avatar/community.br-automation.com/kovarj/32/537_2.png) [@kovarj](https://community.br-automation.com/u/kovarj)\
**Post date:** [February 27, 2024, 2:23pm UTC](https://community.br-automation.com/t/problem-with-for-loop/2364/4 "2024-02-27T14:23:55Z")

</div>

→ first of all, your loop iterate from 0 to 5 but your array in watch is from 1 to 6  
 → your array values for gxUpButtonPressed as well as for gxDownButtonPressed are FALSE, your condition in IF statement of these button is waiting for TRUE value, so behavior for buttons iteration from 1 to 5 is correct. Condition is not met, content of IF and ELSEIF statement is not executed.

 ![Loop](https://europe1.discourse-cdn.com/brcommunity/original/2X/d/dddf1ad50f891303ae45ff47a4586e34130bf2b8.jpeg)

---

<div class="post-metadata">

**Author:** ![kovarj](https://dub1.discourse-cdn.com/brcommunity/user_avatar/community.br-automation.com/kovarj/32/537_2.png) [@kovarj](https://community.br-automation.com/u/kovarj)\
**Post date:** [February 27, 2024, 2:30pm UTC](https://community.br-automation.com/t/problem-with-for-loop/2364/5 "2024-02-27T14:30:58Z")

</div>

This brings up additional note from me , check how you access arrays items in your code. If you have more lines like that then very likely you are overwriting memory. So behavior will be always weird 🙂 colleagues here have already provided you nice examples how to do it better (check your previous question).

---

<div class="post-metadata">

**Author:** ![limhyeonsoo](https://avatars.discourse-cdn.com/v4/letter/l/FF8800/32.png) [@limhyeonsoo](https://community.br-automation.com/u/limhyeonsoo)\
**Post date:** [February 28, 2024, 12:36am UTC](https://community.br-automation.com/t/problem-with-for-loop/2364/6 "2024-02-28T00:36:19Z")

</div>

I thought the syntax of ‘elsif’ wouldn’t work because I didn’t click on ‘upbuttonpress’ number 1  
Thanks for the answer, I found it during a quick check while changing my last question  
I’ll take a closer look at your answer

---

<div class="post-metadata">

**Author:** ![limhyeonsoo](https://avatars.discourse-cdn.com/v4/letter/l/FF8800/32.png) [@limhyeonsoo](https://community.br-automation.com/u/limhyeonsoo)\
**Post date:** [February 29, 2024, 3:54am UTC](https://community.br-automation.com/t/problem-with-for-loop/2364/7 "2024-02-29T03:54:33Z")

</div>

I thought I understood, but I was wrong and thought I understood.

---

<div class="post-metadata">

**Author:** ![kovarj](https://dub1.discourse-cdn.com/brcommunity/user_avatar/community.br-automation.com/kovarj/32/537_2.png) [@kovarj](https://community.br-automation.com/u/kovarj)\
**Post date:** [February 29, 2024, 7:32am UTC](https://community.br-automation.com/t/problem-with-for-loop/2364/8 "2024-02-29T07:32:42Z")

</div>

if you solved your problem, please mark this question as a solved 🙂 so we can focus on other questions 🙂
