| JAVA COSTRUTTI ITERATIVI |eng-ita

in Olio di Balenalast year

image.png

CHE COSA SONO I COSTRUTTORI ITERATIVI

in java, i costruttori iterativi, non sono altro che che pezzi di codici che hanno come obbiettivo la ripetizione di un operazione fino ad una ripetuta condizione.
Principalmente conosciamo 3 costruttori, tutti diversi che hanno compiti diversi:

-while
-do-while
-for

Nei righi successivi vi spiegherò bene le funzioni di ciascun costruttore e vari compiti, illustrando il tutto con degli esempi...partiamo!!

WHILE

il ciclo while esegue un operazione fino ad una certa condizione,che può essere un controllo o un paragone,in italiano traduciamo il tutto con "ESEGUI QUESTA OPERAZIONE FINCHE'...",procediamo con un esempio.

image.png

In questo breve esempio ho creato un while,in questo caso gli diciamo "finché il numero sarà minore di 50,allora farai stampare una scritta con scritto "ciao"", essendo che noi non abbiamo messo ne un incremento del numero ne un cambio numero all'interno del blocco, questo while andrà ad essere eseguito all'infinito, al contrario potremmo mettere un incremento del numero ogni volta che viene stampata la scritta e in questo mondo non sarà più all'infinito, ma una volta arrivo a 50 si fermerà.

      while(num<50) { 
                System.out.println("CIAO");
        }

DO-WHILE

Sostanzialmente in while do-while non hanno molte differenze, nel while la condizione si inserisce all'inizio, mentre nel do-while si inserisci alla fine,in italiano leggeremmo "ESEGUI QUESTO SE LA CONDIZIONE E'...",while e do-while eseguono le stesse operazione e durante la scrittura del codice possiamo usarli entrambi...un mio personale consiglio è di usare sempre il while,è più intuitivo anche per i meno esperti.

image.png

In questo esempio ho proceduto alla realizzazione di un codice che ha lo stesso scopo del while, ma creato con un do-while, all'interno gli andiamo a dire..."stampa ciao se il numero è minore di 50,ke come potete notare, sia il significato che l'operazione vera e propria restano le stesse.

    do {
        System.out.println("CIAO");
    }while(num<50);

FOR

Il for è il costruttore più conosciuto, per vari motivi: per prima cosa lo possiamo trovare in vari linguacci,come c, android studio, sql e javascript, seconda cosa per il suo facile utilizzo.
Il suo scopo,è quello di far ripetere un determinato blocco di codice un determinato numero di volte, scelto da noi,a differenza dagli altri costruttori, il for non ha bisogno di nessun genere di controllo, questo rende tutto il facile.

image.png

In questo breve esercizio vi ho illustrato la funzione del for,per prima cosa andiamo a scrivere il comando per la creazione, all'interno delle parentesi tonde inseriamo da che numero partire per la ripetizione e per quante volte ripetere l'istruzione, mentre all'interno delle parentesi graffe inseriamo l'istruzione da ripetere, in questo caso il codice ripeterà 10 volte la scritta "CIAO", tutto questo ci permette di accorciare il codice e di evitare di scrivere righi inutili.

for(int i=0;i<=10;i++){
            System.out.println("CIAO");
        }

CONCLUSIONI

Siamo giunti alle conclusioni, spero di esservi stato utile con queste spiegazioni, nei prossimi post porterò android studio, quindi la programmazione android,personalmente sono argomenti che ritengo molto divertente ma sopratutto che vi possono sempre ritornare utili,i costruttori sono fondamentali se volete imparare a programmare in java :)




image.png

WHAT ARE ITERATIVE CONSTRUCTORS

in java, iterative constructors, are nothing more than pieces of code that have as their goal the repetition of an operation up to a repeated condition.
Mainly we know 3 constructors, all different ones that have different tasks:

-while
-do-while
-for

In the following lines I will explain well the functions of each constructor and various tasks, illustrating it with examples...let's start!!!

WHILE

the while loop performs an operation until a certain condition,which can be a control or a comparison,in Italian we translate this as "EXECUTE THIS OPERATION UNTIL...",let's proceed with an example.

image.png

In this short example I have created a while,in this case we tell it "as long as the number is less than 50,then you will have a write "hello" printed",being that we did not put neither a number increment nor a number change inside the block,this while will go to be executed infinitely,on the contrary we could put a number increment every time the write is printed and in this world it will not be infinitely anymore,but once I get to 50 it will stop.

      while(num<50) { 
                System.out.println("HELLO");
        }

DO-WHILE

Basically in while do-while they don't have much difference,in while the condition is entered at the beginning,while in do-while it is entered at the end,in Italian we would read "EXECUTE THIS IF THE CONDITION IS...",while and do-while perform the same operation and when writing code we can use both...my personal advice is to always use while,it is more intuitive even for the less experienced.

image.png

In this example I proceeded to make a code that has the same purpose as while, but created with a do-while, inside we are going to tell it... "print hello if the number is less than 50,ke as you can see, both the meaning and the actual operation remain the same.

    do {
        System.out.println("HELLO");
    }while(num<50);

FOR

The for is the best-known constructor,for several reasons: first,we can find it in various bad languages,such as c,android studio,sql and javascript,second because of its easy use.
Its purpose,is to make a given block of code repeat a given number of times,chosen by us,unlike the other constructors,the for does not need any kind of control,this makes it all the easy.

image.png

In this short exercise I have shown you the function of the for,first we go to write the command for the creation, inside the round brackets we insert what number to start from for the repetition and how many times to repeat the instruction, while inside the curly brackets we insert the instruction to be repeated, in this case the code will repeat 10 times the words "HELLO", all this allows us to shorten the code and avoid writing unnecessary lines.

for(int i=0;i<=10;i++){
            System.out.println("HELLO");
        }

CONCLUSIONS
.
We have come to the conclusion, I hope I have been useful to you with these explanations, in the next posts I will bring android studio, then android programming,personally these are topics that I think are very fun but most of all they can always come back to you useful,constructors are essential if you want to learn how to program in java :)