29 Eylül 2009 Salı

Contol Statments; if-then-else

if
if - else
if - then- else

void applyBrakes(){
if (isMoving){ // if (eger) hareket ediyorsa
currentSpeed--; //hızı düşür, durdur
}
}


veya

void applyBrakes(){
if (isMoving) currentSpeed--; // aynı anlama geliyor ama parantezleri yok, //but without braces
}


The if-then-else Statement


void applyBrakes(){
if (isMoving) {
currentSpeed--;
} else {
System.err.println("The bicycle has already stopped!"); // bisiklet zaten duruyor
}
}


if else else if

class IfElseDemo {
public static void main(String[] args) {

int testscore = 76;
char grade;

if (testscore >= 90) {
grade = 'A';
} else if (testscore >= 80) {
grade = 'B';
} else if (testscore >= 70) {
grade = 'C';
} else if (testscore >= 60) {
grade = 'D';
} else {
grade = 'F';
}
System.out.println("Grade = " + grade);
}
}
The output from the program is://programın sonucu
    Grade = C // büyük: C

Kaynak: http://java.sun.com/docs/books/tutorial/java/nutsandbolts/if.html


Hiç yorum yok:

Yorum Gönder