-
Notifications
You must be signed in to change notification settings - Fork 0
/
Conditionals.java
97 lines (73 loc) · 2.95 KB
/
Conditionals.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package week2Videos;
public class Conditionals {
public static void main(String[] args) {
// ******** if ********
// format: if (boolean comparison) followed by {code to be run if boolean statement is true}
String name = "Sam";
String name2 = "Tom";
String name3 = "Alex";
if (name == "Sam") {
System.out.println("Hello Sam!");//statement after if is true so code will run
}
if ( name2 == "Tom") {
System.out.println("Hello Tom!");//statement after if is true so code will run
}
if ( name3 == "Ana") {
System.out.println("Hello Ana!");// statement after if is false so code will not run/will not print
}
// ***** else *****
// goes after the last curly brace of the "if" statement
// format : else {code to be run when the "if" statement is false}
System.out.println("**************************");//cosmetic break
String name4 = "Nate";
if ( name4 == "Matt") {
System.out.println("Hello Matt");
}else {
System.out.println("You're not Matt, " + name4);
}
System.out.println("********");
int age = 15;
if (age >= 16) { // if statement is false, so it will go on to the else statement
System.out.println("You can get your license!");
}else {
System.out.println("Not old enough!");
System.out.println("Please wait " + (16 - age) + " year(s) to get your license");
}
// ***** else if *****
// this statement follows an "if" statement
// contains an additional boolean expression inside () followed by a code inside {},
// which will run if the boolean expression if true
// * the boolean expression in the "else if" evaluates only if the the boolean
// expression in the previous "if" statement is false
// followed by an "else" statement, which is the default if the previous "if" or "else if" statement(s) are false
System.out.println("********");
double costOfMilk = 3;
if (costOfMilk <= 2){ //this boolean expression is false, so it will go on to the "else if" statement
System.out.println("Buying 2");
}else if (costOfMilk <= 3) {
System.out.println("Buying 1");
}else {
System.out.println(" Not buying any");
}
// ******* switch ******
// evaluates a variable, then provide multiple code blocks that could be executed,
// based on the value of the variable
// format: switch (variable), followed by the blocks of code or case then a "break" before moving on to the next case
// ending with a default if the previous cases are false
System.out.println("********");
char grade = 'C';
switch (grade) {
case 'A':
System.out.println("90%");// without the "break;" it will continue to run the next case even
break; //if the first one was true
case 'B':
System.out.println("80%");
break;
case 'C':
System.out.println("70%");//case A and B are false, so it moves to case C, which is true-> runs code
break;
default:
System.out.println("0%");
}
}
}