Un fragment de prelegere cu un mentor ca parte a cursului Universității Codegym. Înscrie-te la cursul complet.


"Bună, Amigo. Astăzi vom vorbi despre declarațiile if/else ."

„Programele ar fi de puțin folos dacă nu ar răspunde circumstanțelor externe în schimbare. Un program trebuie să știe cum să se adapteze circumstanțelor și să efectueze o acțiune într-un caz și alte acțiuni în alte cazuri. În Java, acest lucru se realizează folosind „instrucțiunea if/else” – o construcție specială care face posibilă efectuarea diferitelor blocuri de cod dacă o condiție este îndeplinită.”

„Constă din trei părți: „ condiție ”, „ comandă 1 ” și „ comandă 2 ”. Dacă condiția este adevărată, atunci „ comanda 1 ” este executată, în caz contrar „comanda 2” este executată. Aceste comenzi nu sunt niciodată executate ambele. Afirmația arată mai mult sau mai puțin așa:”

Cod pentru o declarație if/else
if (condition)
    command_1;
else
    command_2;

"Ce interesant! Cred că această afirmație va face programarea mult mai interesantă!"

"Da. Iată câteva exemple pentru tine:"

Cod Explicaţie
1
if (a < b)
    System.out.println("A is less than B");
else
    System.out.println("B is less than  A");
Dacă a este mai mic decât b, prima comandă va fi executată. În caz contrar, a doua comandă va fi executată . Comenzile nu sunt niciodată executate ambele.
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");
}
Puteți înlocui o comandă cu un bloc de cod. Restul este la fel.
3
if (a < b)
{
    a = 0;
}
else
{
}
Puteți omite blocul else dacă este gol.
Aceste trei exemple sunt complet echivalente.
Puteți omite parantezele dacă trebuie să executați o singură comandă. Dacă aveți mai multe comenzi, trebuie să păstrați parantezele.
4
if (a < b)
{
    a = 0;
}
5
if (a < b)
    a = 0;

— Diego tocmai mi-a cerut să-ți dau câteva sarcini.

2
Sarcină
Java Syntax,  nivellecţie
Blocat
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
Sarcină
Java Syntax,  nivellecţie
Blocat
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
Sarcină
Java Syntax,  nivellecţie
Blocat
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
Sarcină
Java Syntax,  nivellecţie
Blocat
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
Sarcină
Java Syntax,  nivellecţie
Blocat
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
Sarcină
Java Syntax,  nivellecţie
Blocat
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 fragment de prelegere cu un mentor ca parte a cursului Universității Codegym. Înscrie-te la cursul complet.