Un frammento di lezione con un mentore come parte del corso Codegym University. Iscriviti al corso completo.


"Ciao, Amigo. Oggi parleremo delle affermazioni if/else ."

"I programmi sarebbero di scarsa utilità se non rispondessero alle mutevoli circostanze esterne. Un programma deve sapere come adattarsi alle circostanze ed eseguire un'azione in un caso e altre azioni in altri casi. In Java, questo si ottiene utilizzando il 'if/else statement' – un costrutto speciale che rende possibile eseguire diversi blocchi di codice se una condizione è soddisfatta."

"Si compone di tre parti: ' condizione ', ' comando 1 ' e ' comando 2 '. Se la condizione è vera, viene eseguito ' comando 1 ', altrimenti viene eseguito 'comando 2' . Questi comandi non vengono mai eseguiti entrambi. La frase è più o meno così:"

Codice per un'istruzione if/else
if (condition)
    command_1;
else
    command_2;

"Che emozione! Penso che questa affermazione renderà la programmazione molto più interessante!"

"Sì. Ecco un paio di esempi per te:"

Codice Spiegazione
1
if (a < b)
    System.out.println("A is less than B");
else
    System.out.println("B is less than  A");
Se a è minore di b, verrà eseguito il primo comando . Altrimenti verrà eseguito il secondo comando . I comandi non vengono mai eseguiti entrambi.
2
if (a < b)
{
    System.out.println("A is less than B");
    System.out.println("B is greater than A");
}
else
{
     System.out.println("B is less than A");
     System.out.println("A is greater than B");
}
Puoi sostituire un comando con un blocco di codice. Il riposo è lo stesso.
3
if (a < b)
{
    a = 0;
}
else
{
}
Puoi omettere il blocco else se è vuoto.
Questi tre esempi sono del tutto equivalenti.
Puoi omettere le parentesi graffe se hai solo bisogno di eseguire un comando. Se hai più di un comando, devi mantenere le parentesi.
4
if (a < b)
{
    a = 0;
}
5
if (a < b)
    a = 0;

"Diego mi ha appena chiesto di affidarti alcuni compiti."

2
Compito
Java Syntax,  livello 4lezione 4
Bloccato
Good or bad?
Student robot Peter is an overachiever. Previously, his server was configured to receive scores on a five-point scale, but now his teachers have switched to a 12-point scale. But Peter doesn't know this. He's still focused on getting fives. Let's write him a compare method that compares any number with five.
4
Compito
Java Syntax,  livello 4lezione 4
Bloccato
Closest to 10
Ten is extremely popular and attractive number. Everyone wants to be a ten. Or at least as close to it as possible. Two numbers are standing around wondering which of them is cooler. Answer: whichever is closer to ten. Let's write these numbers a displayClosestToTen method that will determine which of them is cooler.
4
Compito
Java Syntax,  livello 4lezione 4
Bloccato
Come on, lucky seven!
Dice games are popular on the planet Foggy Multidimensions. The rules are different than the Earth version: Multidimensionals perceive far more dimensions than primitive three-dimensional earthlings. Their die has 4294967295 faces. Players win only if they roll a number between 50 and 100. We'll write a method that checks whether the die roll is in this range.
4
Compito
Java Syntax,  livello 4lezione 4
Bloccato
Seasons on Terra
An Earth year consists of four seasons, each of which lasts 3 months. While our ship was parked on this cradle of humanity, the Interplanetary Tax Service asked us to write a program to determine the season based on a number corresponding to the month of the year. We don't know why they want it. They say that it's none of our business. But they promised not to remain in our debt.
4
Compito
Java Syntax,  livello 4lezione 4
Bloccato
Positive and negative numbers
Diego is tall, but Alfredo is short. Rishi is experienced, but you're a "noob" programmer. Comparisons are unavoidable in life. And the same thing is true in programs. So we'll continue practicing comparisons and displaying stuff on the screen. This time let's compare the entered number with zero and manipulate it based on the result of the comparison.
4
Compito
Java Syntax,  livello 4lezione 4
Bloccato
Day of the week
Planet Terra still has "offices"—an outdated type of workspace. With current technology, there is no need for them, but earthlings tend to be nostalgic and haven't been in a hurry to eradicate them. Terrestrial office workers develop "TGIF syndrome": they constantly want to know what day of the week it is. Let's write a program for them!

Un frammento di lezione con un mentore come parte del corso Codegym University. Iscriviti al corso completo.