Simple Program Flow issue

You must Login before you can answer or comment on any questions.

Hi all,

I'm a n00b at JS so I apologise for the level of this question but I just can't figure this out?!

Can anyone please explain to me WHY this fails?

I know people prefer to use self calling functions etc but I just can't get this code to execute correctly. (I also know I am using global variables and other bad practices but I have been trying to write a different program and it is the while or do-while loops that keep causing execution issues so I have written this, reduced to its most basic level, just as a code testbed)

I am not looking for the way I ought to do it per-se so much as understanding why / how JS works so I understand why this code doesn't!

Many thanks in advance!


This is running on an android emulator in Windows Vista (I know - ew!) Titanium Studio, build: 2.0.1.201204132053. Android 2.0.1GA2 Runtime V8

If the "while" loop is commented out, it runs and displays a blue window and the label "Done!"

WITH the while loop, all I get is the red Titanium banner page and it goes no further!

What don't I understand about while loops / JavaScript flow control / execution please?


var counter = 5;

var win = Ti.UI.createWindow({backgroundColor:'#00f'});

var label = Ti.UI.createLabel({ color:'#fff', top:(Ti.Platform.displayCaps.platformHeight)/2, });

win.add(label);

win.open();

while (counter>0);
{   
label.text=counter;
counter-=1;
}

label.text = 'Done!';

5 Answers

Accepted Answer

I dont see any fail in the while loop, I tried to run your code in my android device and it worked after some changes. you have given a semicolon after the while syntax, remove that (it may be your typo mistake)

here is the code which I tried and it showed me the blue screen and the label done

var counter = 5;
var win = Ti.UI.createWindow({backgroundColor:'#00f'});
var label = Ti.UI.createLabel({ color:'#fff', top:(Ti.Platform.displayCaps.platformHeight)/2,height:50, width: 100 });
win.add(label);
win.open();
while (counter>0)
{   
label.text=counter;
counter-=1;
}
label.text = 'Done!';

— answered 1 year ago by Ajeet pratap Maurya
answer permalink
3 Comments
  • Ajeet is right. Having the semi-colon at the end on your while statement simply skips what's inside the brackets.

    — commented 1 year ago by Christian Brousseau

  • infact The semicolon present in front of the while loop leads to the infinite loop as the value of counter is always greater than 0, and with the semicolon in front of it the while loops keeps on iterating un till that loop condition gets false.. This is the reason why you wee stuck at the appcelerator red screen.

    — commented 1 year ago by Ajeet pratap Maurya

  • Ajeet is right. Having the semi-colon at the end on your while statement simply skips what's inside the brackets.

    — commented 1 year ago by Christian Brousseau

Hm I dont see an error, try this:

for (var i=counter;i>0;i--) {
    label.setText(i);
}
label.setText('DONE');

Thanks for your response!

I see why that would work...

But why does the while loop fail?!

Thank you both so much!

I can't believe I missed something as obvious and simple as that!

I thought I was going mad! lol

Thanks again!

Thanks for your help so far guys,

There is definitely something I don't understand about program flow in Titanium / JS...

My above typo not withstanding, when I then took the corrected version to my application, it still fails in a way I don't expect.

Again, here is a simplified version that behaves 'oddly'.

I would have expected it to display an incredibly fast moving number that counts up to 2000.

Instead, it 'appears' to do nothing (sitting on the Appcelerator banner) while counting in the background and then only displays a window with "Done!" at the very end.

Can someone please explain to me why it displays nothing until the end of the program or the loop?

Many thanks again!

(ps - again - watch out for the "less-than" sign in the IF statement!)


var counter = 0;

var win = Ti.UI.createWindow({backgroundColor:'#00f'});

var label = Ti.UI.createLabel({ color:'#fff', text:'<blank>', top:(Ti.Platform.displayCaps.platformHeight)/2, });

win.add(label);

win.open();

do
{
    counter+=1;
    label.text=counter;
} while (counter&lt;2000);

label.text = 'Done!';

— answered 1 year ago by Robin Williams
answer permalink
7 Comments
  • In your loop, try to pass a string instead of an int value :

    label.text=counter + '';
    Not sure about this but I think it's worth trying

    — commented 1 year ago by Jb Gartner

  • Thanks for the suggestion. I just tried it...

    No difference though.

    — commented 1 year ago by Robin Williams

  • Just tried your code, it works fine in 1.8.2 and 2.0.1

    — commented 1 year ago by Jb Gartner

  • Show 4 more comments

Your Answer

Think you can help? Login to answer this question!