Isang lecture snippet na may mentor bilang bahagi ng kurso ng Codegym University. Mag-sign up para sa buong kurso.


"Hi, Amigo. Ngayon ay pag-uusapan natin ang tungkol sa mga pahayag ng if/else ."

"Makaunting pakinabang ang mga programa kung hindi sila tumugon sa pagbabago ng mga panlabas na pangyayari. Kailangang malaman ng isang programa kung paano iaangkop sa mga pangyayari at magsagawa ng isang aksyon sa isang kaso at iba pang mga aksyon sa ibang mga kaso. Sa Java, ito ay nakakamit gamit ang 'if/else statement' – isang espesyal na konstruksyon na ginagawang posible na magsagawa ng iba't ibang mga bloke ng code kung nasiyahan ang isang kundisyon."

"Binubuo ito ng tatlong bahagi: ' kundisyon ', ' command 1 ' at ' command 2 '. Kung ang kundisyon ay totoo, ang ' command 1 ' ay ipapatupad, kung hindi ang 'command 2' ay isasagawa. Ang mga command na ito ay hindi kailanman parehong naisakatuparan. Ang pahayag ay parang ganito:"

Code para sa isang if/else na pahayag
if (condition)
    command_1;
else
    command_2;

"Nakakapanabik! Sa tingin ko ang pahayag na iyon ay gagawing mas kawili-wili ang programming!"

"Oo. Narito ang ilang halimbawa para sa iyo:"

Code Paliwanag
1
if (a < b)
    System.out.println("A is less than B");
else
    System.out.println("B is less than  A");
Kung ang a ay mas mababa sa b, ang unang utos ay isasagawa. Kung hindi, ang pangalawang utos ay isasagawa . Ang mga utos ay hindi kailanman parehong naisakatuparan.
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");
}
Maaari mong palitan ang isang utos ng isang bloke ng code. Ang natitira ay pareho.
3
if (a < b)
{
    a = 0;
}
else
{
}
Maaari mong alisin ang ibang block kung ito ay walang laman.
Ang tatlong halimbawang ito ay ganap na katumbas.
Maaari mong alisin ang mga kulot na bracket kung kailangan mo lamang magsagawa ng isang utos. Kung mayroon kang higit sa isang utos, kailangan mong panatilihin ang mga bracket.
4
if (a < b)
{
    a = 0;
}
5
if (a < b)
    a = 0;

"Nakiusap lang sa akin si Diego na bigyan ka ng ilang gawain."

2
Gawain
Java Syntax,  antasaralin
Naka-lock
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
Gawain
Java Syntax,  antasaralin
Naka-lock
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
Gawain
Java Syntax,  antasaralin
Naka-lock
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
Gawain
Java Syntax,  antasaralin
Naka-lock
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
Gawain
Java Syntax,  antasaralin
Naka-lock
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
Gawain
Java Syntax,  antasaralin
Naka-lock
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!

Isang lecture snippet na may mentor bilang bahagi ng kurso ng Codegym University. Mag-sign up para sa buong kurso.