Simply loop counter

Hi to all.
This is my first post here and I hope to solve my little problem.

Well.

Using appInventor, I need to explain to the students how a loop work creating a counter (to up or to down) like this C++ code

#include
int main(){
float n;
std::cout<<"Inserisci un numero: ";
std::cin>>n;
while( n >= 0 )
{
std::cout<<n<<", ";
n = n - 1;
}
std::cout<<" END!";
return 0;
}

On AppInventor I've make this code using:

  • a checkbox to read start/end number
  • a label to show the single number during the cowndown or the countup
  • two buttons to start the counter to down zero or to up to the ed number.

And this blocks code

During the loop, I want to print all single number in the label.
But the code don't work properly because only the last number is displayed at the end of the count.
Why?
Is it necessary to enter a delay code? And how can I to do?
I tried to insert a nested loop but it doesn't work.

I know how to use the clock component for this but I want to understand why the simple loop don't work.

At the end, if it is possible, where can I to found the source code of clock component (or of all components) for to study how they works?

Thanks for help.
Francesco

Try like this (blocks are draggable):

blocks (2)

Screen output

image

1 Like

Thank you so much .
I understand how it work and my error.

But now another question because I'm going to try this feature.

If I want that the next number substitute the last number printed, and the number are show only on one line, how can I to do it?

Show example of what you mean - the output ?

Yes, sorry.
when I click on button I need to show only one number in to the label. Not all rows or all on the single row.

Ex.
the startnumber is 10. the global number is = 10
In the label show 10.
at the next,
the global number is = 10-1
In the label show ONLY the 9.
at the next,
the global number is = 9-1
In the label show ONLY the 8.
ect...

I've tried to use a set label text to "" "" but, obviously, don't work because i show only the last number zero that is out of the loop.

I think it is necessary to use a second variable and for you?

This is the optimization of code clean of text

You will need a clock timer for this, in order to slow the processing of the while loop down. You may find it better to use a counter and increment the counter with the timer.

Thanks so much.

I already know the use of the clock as I wrote in the first post.

The purpose of the question is precisely to build a method to perform this substitution without the clock component

Without a clock, the loop will execute so quickly that you won't see anything but the last zero.

Thanks. Yes I know.
Now I'm trying to develop a different algorithm like, for example, using a nested loop but it doesn't work...

The research continues.
:laughing:

because you need to use the clock :smiley:

Of course
:joy: :joy: :joy:

Well for tomorrow lesson I use this code for to explain the Loop and for the next I Introduce a Clock component and then I assign a work to crete an app counter using it.

Thanks a lot

Dear professor,
please let me tell you a hint: don't use a float to explain a counter.
A counter is typically an integer value, Also because the comparison between floats is normally difficult. It depends on the amount of bits used to represent the decimal part and the exponent of a float. When you compare a float vs 0 or whatever other value, you shall compare it with a float too: i.e. 0.0 or x.y and the comparison could depend on the rounding (if any) of the two elements to be compared. In a nutshell it would be better if "n" would be an int .
I'm so sorry if I'm so boring..... :innocent:but I started writing code more or less 40 years ago, when a float variable was only for very rich people (each consuming 4 bytes of RAM or EEPROM :slight_smile: ) or very rich hardware architecture...
Have a great lesson, tomorrow... (in "DAD" I believe ).
Cordialmente.

Thanks uskiara.
Yes I'm in DAD :grin:
Are you italian, I presume? :yum:

Sì!
sono un "vecchio" softwarista dagli anni '80 in assembler...(primi processori i8080 e Z80).
Quando un float era "roba da ricchi" :rofl: :rofl: :rofl:
Buona lezione!!!!
(Talvolta mi capita di aiutare mio figlio e i suoi compagni di liceo SA, per cui mi tengo aggiornato ...)
Ciao, Ugo.

Siamo, quindi, molto probabilmente... coetanei :rofl: :rofl: :rofl:
tra poco arrivo a 51 e ho iniziato a "basiccare" nel natale 1980 con lo zx81 arrivato direttamente dall'Inghilterra.
Poi... ho saltato i vari Atari e commodore e sono passato direttamente ai primi PC con Olivetti M20, M21 (trasportabile), M24 e il primo portatile italiano M10, unico pezzo rimasto in mio possesso... Bei tempi.

1 Like

Dear all, please let me apologise if we use the MIT AI2 forum to speak about our lives!
Just few lines then we'll go back on tech matters. Have a great WE to all.

Magari!!!
Sono molto più vecchio...il mio primo processore è stato l'8080 (e mi sono laureato -in fisica- nell'84 quando il quark beauty era stato solo teorizzato e lo stavamo cercando al CERN !!!).
Ho iniziato a programmare direttamente in codice macchina con il tastierino alfanumerico e il "compilatore" era un cartoncino scorsoio, tipo regolo calcolatore....in ottale, neanche esadecimale.
Ormai programmare è rimasta una "ginnastica mentale". Alla sera, se non lavoro o gioco a scacchi con mio figlio piccolo (18 anni :-)) faccio quattro salti sul forum, e smanetto con Arduino. Se ti capita di insegnarlo ai tuoi, qui sul MIT AI2 trovi una vera miniera !!! Con AI2 puoi far fare delle user interface splendide, poi con Arduino far fare le attuazioni. Uno spettacolo !
Speriamo che la DAD finisca presto.
Buona salute a te, ai tuoi cari ed ai tuoi alunni.
Ciao, Ugo.

First of all I also apologize for this "personal" use of the AI2 forum.

We finished. This is the last message. :smiley:

Mi scuso anche io con gli utenti, una cortesia a cui non avevo pensato.

Grazie anche per i suggerimenti su Arduino che sto imparando ad usare.
In effetti io ho fatto dei lavori con Lego Mindstorm.

Auguro anche a te di tornare il prima possibile a condividere di persona questa nostra passione con i ragazzi.
Ciao. Francesco

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.