29 Eylül 2009 Salı

for döngüsü (For Statment)

class ForDemo {
public static void main(String[] args){
for(int i=1; i<11;>
The output of this program is:
Count is: 1
Count is: 2
Count is: 3
Count is: 4
Count is: 5
Count is: 6
Count is: 7
Count is: 8
Count is: 9
Count is: 10


veya...
class EnhancedForDemo {
public static void main(String[] args){
int[] numbers = {1,2,3,4,5,6,7,8,9,10};
for (int item : numbers) {
System.out.println("Count is: " + item);
}
}
}
In this example, the variable item holds the current value from the numbers array. The output from this program is the same as before:
Count is: 1
Count is: 2
Count is: 3
Count is: 4
Count is: 5
Count is: 6
Count is: 7
Count is: 8
Count is: 9

Count is: 10

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

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