Variables inside of an while loop


#1

I’ve noticed something that I’m curious about. When I place the variable referencing an array outside of a while loop it doesn’t run. For example:

 var enemies = this.findEnemies();
var enemyIndex = 0;
var enemy = enemies[enemyIndex];

while (enemyIndex < enemies.length) {
    
    if (enemy.type == 'thrower' && this.isReady("cleave")) {
        this.cleave(enemy);
    }else if (enemy.type =='thrower' && !this.isReady("cleave")) {
        this.attack(enemy);
    }enemyIndex = 1 + enemyIndex;
}

Whereas if I place it inside the while loop it does run. What is the reason for this?


#2

Well… I don’t get why enemy is defined outside of the loop.

var enemy = enemies[enemyIndex];

when enemyIndex is set to 0, this statement just means the variable enemy will always be the first enemy in the list defined at the top. It’s never reassigned

So, your while loop is running blank, just checking enemies.length times the same body. I hope this enemy is a thrower, because if it’s not, your loop does nothing.

I didn’t run it, but I think that’s what it should do. Here is my shot at making it working and (a little bit) nicer to read.

// Initialization of the loop
var enemies = this.findEnemies();
var enemyIndex = 0;

// Running the loop
while (enemyIndex < enemies.length) {
    var enemy = enemies[enemyIndex];
    if( enemy.type == "thrower"){
        if( this.isReady("cleave") {
            this.cleave(enemy);
        } else {
            this.attack(enemy);
        }
        enemyIndex +=1 ;
    }
}

#3

So placing:

var enemy = enemies[enemyIndex];

Inside of the while loop is just to increment the array forward each pass through the loop?


#4

I’m not sure I understand the question, but if the variable “enemy” isn’t assigned anywhere else than outside the loop, you can’t expect it to change. A variable isn’t a function.

When I define f(x) -> x² , I define it ONCE and I can change x. Then f(x) will change as well.

When you define

var enemyIndex = 0;
var enemy=enemies[enemyIndex] ; 

if not manually reassigned, the variable enemy will always be equal to enemies[0], whatever value enemyIndex could take afterwards.

This is why you must change the variable value at each iteration in your loop. Otherwise there is no point making a loop through the array enemies if you’re checking only the first value again and again.


#5

Ah, thank you. These arrays have been taking some effort to grok.


#6

I think it’s customary to apologize for adding something to an old thread, so I’m gonna do that (although I’m not sure why).

Here’s my question about this exact same problem (at least that’s what it looks like), only in Python:

This code works:

while True:
    enemies = hero.findEnemies()
    enemyIndex = 0
        
    while enemyIndex < len(enemies):
    #I'm declaring the variable enemy inside the while loop... 
        enemy = enemies[enemyIndex]
        if enemy.type != "sand-yak":
            while enemy.health > 0:
                hero.attack(enemy)
    #and also incrementing its index inside the while loop:  
        enemyIndex += 1
        pass
    hero.moveXY(40, 32)

This also works:

while True:
    enemies = hero.findEnemies()
    enemyIndex = 0
    #here, I'm declaring the variable outside the while loop...
    enemy = enemies[enemyIndex]
    while enemyIndex < len(enemies):
        if enemy.type != "sand-yak":
            while enemy.health > 0:
                hero.attack(enemy)
    #and also incrementing the index outside:
    enemyIndex += 1
    #this way, the index won't remain 0 for each loop!
    pass
    hero.moveXY(40, 32)

…although at some point it (not sure who “it” is in this case, maybe the compiler, the runtime environment?) said there was an infinite loop, still goals were achieved and the actions of hero were the same as in first code version.
Where’s that infinite loop?