PAYLAŞ

kaynak ;https://www.learnjavaonline.org/en/Conditionals

Conditionals


Java uses boolean variables to evaluate(hesaplama) conditions. The boolean values true and false are returned when an expression is compared or evaluated. For example:

int a = 4;
boolean b = a == 4; //true

if (b) {
    System.out.println("It's true!");
}

Execute Code

Of course we don’t normally assign(atamak) a conditional expression to a boolean, we just use the short version:

int a = 4;

if (a == 4) {
    System.out.println("Ohhh! So a is 4!");
}

Execute Code

Boolean operators

There aren’t that many operators to use in conditional statements and most of them are pretty(odukça) strait forward(basit):

int a = 4;
int b = 5;
boolean result;
result = a < b; // true
result = a > b; // false
result = a <= 4; // a smaller or equal to 4 - true
result = b >= 6; // b bigger or equal to 6 - false
result = a == b; // a equal to b - false
result = a != b; // a is not equal to b - true
result = a > b || a < b; // Logical or - true
result = 3 < a && a < 6; // Logical(mantıksal) and - true
result = !result; // Logical not - false

Execute Code

if – else and between

The if, else statement in java is pretty simple.

if (a == b) {
    // a and b are equal, let's do something cool
}

Execute Code

And we can also add an else statement after an if, to do something if the condition is not true

if (a == b) {   //if it is true it will work
    // We already know this part
} else {     //if it is false it will work
    // a and b are not equal... :/
}

Execute Code

The if – else statements doesn’t have to be in several lines with {}, if can be used in one line, or without the {}, for a single line statment.(tek satırlık ifadede süslü parantez koymayabilirsin)

if (a == b)
    System.out.println("Another line Wow!");
else
    System.out.println("Double rainbow!");

Execute Code

Although this method might be useful for making your code shorter by using fewer lines, we strongly recommend for beginners not to use this short version of statements and always use the full version with {}. This goes to every statement that can be shorted to a single line (for, while, etc).

The ugly side of if

There is a another way to write a one line if – else statement by using the operator ? :

int a = 4;
int result = a == 4 ? 1 : 8;

// result will be 1
// This is equivalent(denk) to
int result;
// a = 1 else works
if (a == 4) {
    result = 1;
} else {
    result = 8;
}

Execute Code

Again, we strongly recommend for beginners not to use this version of if.

== and equals

The operator == works a bit different on objects than on primitives(temel). When we are using objects and want to check if they are equal, the operator == wiil say if they are the same, if you want to check if they are logically(mantıklı olarak) equal, you should use the equals method on the object. For example:

String a = new String("Wow");
String b = new String("Wow");
String sameA = a; 

// new olduğunda dikkat et ikisi farklı yer kaplar
boolean r1 = a == b;      // This is false, since(çünkü) a and b are not the same object
// a ile b new olduğundan farklı objeler kaplar vebirbirine eşit olmaz ve false olur)
boolean r2 = a.equals(b); // This is true, since a and b are logically equals
// equals için durum aynı olur ve true olur)
boolean r3 = a == sameA;  // This is true, since a and sameA are really the same object

Execute Code

equals ile == arasındaki fark "new " methodunda equals eşitlik sağlarken == "eşitlik" sağlamaz

exercises

Kimler Neler Demiş?

İlk Yorum Hakkı Senin!

Bildir
avatar

wpDiscuz