Skip to content

Commit 6bc9c3a

Browse files
committed
If else / Boolean Expressions / If else Examples / Ternary Operator
1 parent 27bfe3d commit 6bc9c3a

8 files changed

Lines changed: 215 additions & 0 deletions

src/lessons/l6/AndOrOperators.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package lessons.l6;
2+
3+
public class AndOrOperators {
4+
public static void main(String[] args)
5+
{
6+
7+
}
8+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package lessons.l6;
2+
3+
public class BooleanExpressions {
4+
public static void main(String[] args)
5+
{
6+
java.util.Scanner keyboard = new java.util.Scanner(System.in);
7+
8+
System.out.print("First number: ");
9+
int firstNumber = keyboard.nextInt();
10+
11+
System.out.print("Second number: ");
12+
int secondNumber = keyboard.nextInt();
13+
14+
boolean isEqual = firstNumber == secondNumber;
15+
boolean isFirstNumberGreater = firstNumber > secondNumber;
16+
boolean isFirstNumberLess = firstNumber < secondNumber;
17+
18+
boolean isFirstNumberGreaterOrEquals = firstNumber >= secondNumber;
19+
boolean isFirstNumberLessOrEquals = firstNumber <= secondNumber;
20+
21+
boolean isNotEqual = firstNumber != secondNumber; // ***
22+
23+
boolean isAlive = true;
24+
//...
25+
26+
if (!isAlive) {
27+
System.out.println("DEAD");
28+
}
29+
}
30+
}

src/lessons/l6/IfElse.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package lessons.l6;
2+
3+
public class IfElse {
4+
public static void main(String[] args)
5+
{
6+
java.util.Scanner keyboard = new java.util.Scanner(System.in);
7+
8+
System.out.print("Yasin age: ");
9+
int yasinAge = keyboard.nextInt(); // 21
10+
11+
System.out.print("Akif age: ");
12+
int akifAge = keyboard.nextInt(); // 29
13+
14+
System.out.print("Eren age: ");
15+
int erenAge = keyboard.nextInt(); //17
16+
17+
// Let's go to the bar
18+
19+
if (yasinAge >= 18)
20+
System.out.println("Yasin can enter the bar");
21+
else
22+
System.out.println("Yasin cant enter the bar");
23+
24+
if (akifAge >= 18) {
25+
System.out.println("Akif can enter the bar");
26+
}
27+
else {
28+
System.out.println("Akif cant enter the bar");
29+
}
30+
31+
if (erenAge >= 18) {
32+
System.out.println("Eren can enter the bar");
33+
} else {
34+
System.out.println("Eren cant enter the bar");
35+
}
36+
}
37+
}

src/lessons/l6/IfElseExample.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package lessons.l6;
2+
3+
public class IfElseExample {
4+
public static void main(String[] args)
5+
{
6+
java.util.Scanner keyboard = new java.util.Scanner(System.in);
7+
8+
System.out.print("First number: ");
9+
int firstNumber = keyboard.nextInt();
10+
11+
System.out.print("Second number: ");
12+
int secondNumber = keyboard.nextInt();
13+
14+
// First > Second
15+
if (firstNumber > secondNumber) {
16+
System.out.println("First number > Second Number");
17+
}
18+
19+
System.out.print("Yasin age: ");
20+
int yasinAge = keyboard.nextInt();
21+
22+
23+
if (yasinAge >= 18) {
24+
System.out.println("Ilk adim tamamlandi");
25+
26+
System.out.print("Yasinin kiz arkadasi var mi? ");
27+
boolean hasGirlFriend = keyboard.nextBoolean();
28+
29+
if (hasGirlFriend) {
30+
System.out.println("Girebilirsin");
31+
} else {
32+
System.out.println("Giremezsin");
33+
}
34+
}
35+
}
36+
}

src/lessons/l6/IfElseExample2.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package lessons.l6;
2+
3+
public class IfElseExample2 {
4+
public static void main(String[] args)
5+
{
6+
java.util.Scanner keyboard = new java.util.Scanner(System.in);
7+
8+
System.out.print("Age: ");
9+
int age = keyboard.nextInt();
10+
11+
// Display Generation
12+
13+
if (age < 5) {
14+
System.out.println("Bebek");
15+
} else if (age < 13) {
16+
System.out.println("Cocuk");
17+
} else if (age < 18) {
18+
System.out.println("Genc");
19+
} else if (age < 25) {
20+
System.out.println("Yetiskin");
21+
} else if (age < 35) {
22+
System.out.println("Orta yas");
23+
} else {
24+
System.out.println("Yasli");
25+
}
26+
}
27+
}

src/lessons/l6/IfElseExample3.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package lessons.l6;
2+
3+
public class IfElseExample3 {
4+
public static void main(String[] args)
5+
{
6+
java.util.Scanner keyboard = new java.util.Scanner(System.in);
7+
8+
System.out.print("Username: ");
9+
String userName = keyboard.nextLine();
10+
11+
// Username = "efficient" + Password = "compareTo"
12+
13+
if (userName.equals("efficient")) {
14+
15+
System.out.print("Password: ");
16+
String password = keyboard.nextLine();
17+
18+
if (password.equals("compareTo")) {
19+
System.out.println("Welcome to Efficient House");
20+
} else {
21+
System.out.println("Your password is wrong");
22+
}
23+
} else {
24+
System.out.println("There is no such user");
25+
}
26+
}
27+
}

src/lessons/l6/IfElseExample4.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package lessons.l6;
2+
3+
public class IfElseExample4 {
4+
public static void main(String[] args)
5+
{
6+
java.util.Scanner keyboard = new java.util.Scanner(System.in);
7+
8+
System.out.print("Username: ");
9+
String userName = keyboard.nextLine();
10+
11+
if (userName.charAt(2) == 's') {
12+
13+
System.out.print("Password: ");
14+
String password = keyboard.nextLine();
15+
16+
if (password.charAt(password.length() - 3) == 'x') {
17+
System.out.println("Welcome to Efficient House");
18+
} else {
19+
System.out.println("Your password is wrong");
20+
}
21+
} else {
22+
System.out.println("There is no such user");
23+
}
24+
}
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package lessons.l6;
2+
3+
public class TernaryOperator {
4+
public static void main(String[] args)
5+
{
6+
java.util.Scanner keyboard = new java.util.Scanner(System.in);
7+
8+
System.out.print("First number: ");
9+
int firstNumber = keyboard.nextInt();
10+
11+
System.out.print("Second number: ");
12+
int secondNumber = keyboard.nextInt();
13+
14+
int max;
15+
16+
if (firstNumber >= secondNumber) {
17+
max = firstNumber;
18+
} else {
19+
max = secondNumber;
20+
}
21+
22+
max = (firstNumber >= secondNumber) ? firstNumber : secondNumber;
23+
System.out.println(max);
24+
}
25+
}

0 commit comments

Comments
 (0)