Lesson_4: Control flow
1. if(), if() else(), if() else if() else()
a) Simple if else case
Scanner in = new Scanner(System.in);
int a = in.nextInt();
if (a > 0) {
System.out.println("Positive");
} else if (a == 0) {
System.out.println("Neutral");
} else {
System.out.println("Negative");
}
System.out.println("Continue...");
b) Wrong if else case
Scanner in = new Scanner(System.in);
int grade = in.nextInt();
if(grade > 90 && grade <= 100) {
System.out.println("A");
} else if (grade > 80) {
System.out.println("B");
} else {
System.out.println("Fail");
}
System.out.println("Continue...");
When we input for example 150 code crashes.
Random random = new Random();
int grade = random.nextInt(100);
if (grade > 90) {
System.out.println(grade + " A");
} else if (grade > 80) {
System.out.println(grade + " B");
} else if (grade > 70) {
System.out.println(grade + " C");
} else if (grade > 60) {
System.out.println(grade + " D");
} else if (grade > 50) {
System.out.println(grade + " F");
} else {
System.out.println(grade + " Failed");
}
Statement problem:
int i = 2;
if (i < 3) {
int ii = 10;
}
System.out.println(ii);
int i = 2;
if (i < 3)
int ii = 10;
2. While statement:
double d = Math.random();
System.out.println(d);
while (d < 0.5) {
d = Math.random();
System.out.println("inside while: " + d);
}
System.out.println("Outside while: " + d);
In while we can not use this particular case:
int[] arr = {1, 2, 3, 4, 5, 6};
int i = 3;
int k = 0;
while (i < arr[k]) {
System.out.println(arr[k++]);
}
3. For dovru:
For dovru ile Fibonacci ardicilliginin cap edilmesi:
// 0, 1, 1, 2, 3, 5, 8, 13, 21
int first = 0;
int second = 1;
int result = 0;
for(int i = 0; i < 10; i++) {
System.out.println(first);
result = first + second;
first = second;
second = result;
}
for(char c = 0; c < 255; c++) {
System.out.println("value : " + (int)c + " character : " + c);
}
Mesele1:
*****
*****
*****
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
System.out.print(" * ");
}
System.out.println();
}
Mesele2:
*
**
***
for (int i = 1; i <= 10; i++) {
for (int j = 0; j < i; j++) {
System.out.print(" * ");
}
System.out.println();
}
Mesele3:
*
**
***
****
for (int i = 1; i <= 10; i++) {
for (int j = 10; j > 0; j--) {
if(j > i)
System.out.print(" ");
else
System.out.print("*");
}
System.out.println();
}
Mesele4:
****
***
**
*
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if(j < i)
System.out.print(" ");
else
System.out.print("*");
}
System.out.println();
}
Mesele5:
******
* *
******
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if(i == 0 || i == 9 || j == 0 || j == 9) {
System.out.print("*");
} else
System.out.print(" ");
}
System.out.println();
}
Mesele6:
********
* * *
* * *
* * *
* **
********
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if(i == 0 || i == 9 || j == 0 || j == 9 || i == j) {
System.out.print("*");
} else
System.out.print(" ");
}
System.out.println();
}
Mesele7:
********
* * * *
* * *
* * * *
* * **
********
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if(i == 0 || i == 9 || j == 0 || j == 9 || i == j || i + j == 9) {
System.out.print("*");
} else
System.out.print(" ");
}
System.out.println();
}
Mesele8:
********
*
*******
*
*******
*
*******
int flag = 1;
for (int i = 0; i < 10; i++) {
if(i % 2 == 0) flag++;
for (int j = 0; j < 10; j++) {
if(i % 2 == 0) {
System.out.print("*");
} else {
if(j == 9 && flag % 2 == 0) {
System.out.print("*");
}
if(j == 0 && flag % 2 == 1) {
System.out.print("*");
}
System.out.print(" ");
}
}
System.out.println();
}
Break:
for(int i = 1; i <= 5; i++) {
if(i == 4) break;
System.out.println(i + " " + i * i);
}
Continue:
for(int i = 1; i <= 5; i++) {
if(i == 4) continue;
System.out.println(i + " " + i * i);
}
Label:
outer:
for (int i = 0; i < 10; i++) {
inner:
for (int j = 0; j < 10; j++) {
if(j == 5) break inner;
System.out.println(i + " : " + j);
}
System.out.println("-------------------------------");
}
Label: hour example
for (int hours = 0; hours < 24; hours++) {
System.out.println("Start of outer loop");
for (int minutes = 0; minutes < 60; minutes++) {
System.out.println(hours + " : " + minutes);
if (minutes == 30) break;
}
System.out.println("End of outer loop");
}
indi ise men isteyirem ki, minutes 30 olduqda outer loop dayansin:
for (int hours = 0; hours < 24; hours++) {
System.out.println("Start of outer loop");
for (int minutes = 0; minutes < 60; minutes++) {
System.out.println(hours + " : " + minutes);
if (minutes == 30) {
hours = 24;
break;
}
}
System.out.println("End of outer loop");
}
Switch case:
final ve blank final iwledilmesi:
int month = new Scanner(System.in).nextInt();
final int i;
i = 5;
switch (month) {
case i:
}
A switch case works with the byte, short, char, int primitive data types. Also it works with Enumerated, String, Byte, Short, Character and Integer reference types.
Return in switch case:
public static Integer square(int n) {
switch (n) {
case 1:
return 1;
case 2:
return 4;
default:
return null;
}
}
Arrays. One dimentional array.
int[] array1 = new int[10];
Integer array2[] = new Integer[10];
int[] array3 = {1, 2, 3, 4};
Primitive tiplerde default value her bir primitive mexsus default value olur, referanslarda ise null olur.
int[] array1 = new int[10];
Integer array2[] = new Integer[10];
for(int i = 0; i < array1.length; i++) {
System.out.println(array1[i]);
System.out.println(array2[i]);
}
For each:
int[] arr = {1, 2, 3, 4, 5};
for (int i: arr) {
System.out.println(i);
}
For each loop icinde eger biz deyer deyiwmek istesek o zaman bu mumkun olmayacaq.
int[] arr = {1, 2, 3, 4, 5};
for (int i: arr) {
i = 10;
}
for (int i: arr) {
System.out.println(i);
}
2D arrays:
int[][] arr1 = new int[3][];
int[][] arr2 = {{1, 2}, {3, 4}, {5, 6}};
for(int i = 0; i < arr2.length; i++) {
for(int j = 0; j < arr2[i].length; j++) {
System.out.println(arr2[i][j]);
}
}
int[] a1 = {1, 2, 3};
int[] a2 = {4, 5, 6, 7};
int[] a3 = {8, 9};
int[][] arr = {a1, a2, a3};
int[][] b = new int[2][];
b[0] = new int[3];
b[1] = new int[5];
Alqoritmik sual 3:
Ferz edek ki 1000000 ehalimiz var ve onlarin yashi 0-100 arasindadir. Buna esasen statistika cixarilmalidir ve her yasha uygun nece nefer oldugu bilinmelidir.
int[] people = new int[10];
Random random = new Random();
for(int i = 0; i < people.length; i++) {
int rand = random.nextInt(10);
System.out.println(rand);
people[i] = rand;
}
int[] result = new int[10];
for(int i = 0; i < people.length; i++) {
int age = people[i];
result[age] = ++result[age];
}
for(int i = 0; i < result.length; i++) {
System.out.println(i + " : " + result[i]);
}
Комментарии
Отправить комментарий