Repository navigation
Expand file tree
/
Copy pathControlFlowLec.java
More file actions
164 lines (140 loc) · 5.62 KB
/
Copy pathControlFlowLec.java
File metadata and controls
164 lines (140 loc) · 5.62 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import java.util.Scanner;
public class ControlFlowLec {
//Methods used to illustrate the difference between && and &.
public static boolean left(boolean value) {
System.out.printf("ControlFlowLec.left was called with a %s value.\n", value);
return value;
}
public static boolean right(boolean value) {
System.out.printf("ControlFlowLec.right was called with a %s value.\n", value);
return value;
}
public static void main(String[] args) {
System.out.println("Control Flow Lecture");
// boolean isTrue = true;
// System.out.println("isTrue = " + isTrue);
//Relational operators
int age = 38;
//
// // Equality ==
// boolean isTwentyOne = age == 21;
// System.out.println("isTwentyOne = " + isTwentyOne);
//
// // Inequality !=
// boolean isNotTwentyOne = age != 21;
// System.out.println("isNotTwentyOne = " + isNotTwentyOne);
//
// // Less Than <
// boolean isLessThanTwentyOne = age < 21;
// System.out.println("isLessThanTwentyOne = " + isLessThanTwentyOne);
//
// // More Than >
// boolean isMoreThanTwentyOne = age > 21;
// System.out.println("isMoreThanTwentyOne = " + isMoreThanTwentyOne);
// // Greater Than or Equal >=
// float orderTotal = 30.00F;
// boolean applyFreeShipping = orderTotal >= 30;
// System.out.println("applyFreeShipping = " + applyFreeShipping);
// // Less Than or Equal <=
// float orderTotal = 15.99F;
// boolean applySmallOrderSurcharge = orderTotal <= 15.00F;
// System.out.println("applySmallOrderSurcharge = " + applySmallOrderSurcharge);
// Logical Operators
// && And
// boolean isLoggedIn = true;
// boolean isAdmin = true;
// boolean showTheAdminPanel = isLoggedIn && isAdmin && age > 21;
// System.out.println("showTheAdminPanel = " + showTheAdminPanel);
// Showing that Java optimizes when using && by only evaluating what needs to be evaluated.
// The first false means that the entire expression is false therefore when using && the
// second expression is not evaluated.
// boolean booleanResult = left(false) && right(false);
// System.out.println("booleanResult = " + booleanResult);
// || Or
// boolean isAdmin = false;
// boolean isAuthor = false;
// boolean canEditArticle = isAdmin || isAuthor;
// System.out.println("canEditArticle = " + canEditArticle);
// & And
// boolean isLoggedIn = false;
// boolean isAdmin = true;
// boolean showTheAdminPanel = isLoggedIn & isAdmin;
// System.out.println("showTheAdminPanel = " + showTheAdminPanel);
// showing evaluation of both sides when using a single ampersand.
// boolean booleanResult = left(false) & right(true);
// System.out.println("booleanResult = " + booleanResult);
// | Or
// boolean isAdmin = true;
// boolean isAuthor = false;
// boolean canEditArticle = isAdmin | isAuthor;
// System.out.println("canEditArticle = " + canEditArticle);
// ! Not
// boolean isLoggedIn = true;
// boolean shouldShowLogin = !isLoggedIn;
// System.out.println("shouldShowLogin = " + shouldShowLogin);
// String Comparison Warning
// Scanner sc = new Scanner(System.in);
// System.out.println("Continue? [y/n]");
// String userInput = sc.next();
//
// // Never do this!
//// boolean confirmation = userInput == "y";
//// System.out.println("confirmation = " + confirmation);
//
// // Always use .equals to compare Strings.
// boolean confirmation = userInput.equals("y");
// System.out.println("confirmation = " + confirmation);
// Control Structures
// if
// int weeksInClass = 10;
// boolean moreThanHalfWayDone = weeksInClass >= 10;
// if(moreThanHalfWayDone) {
// System.out.println("We are more than half way done now!");
// } else if(weeksInClass != 0) {
// System.out.printf("We are %s weeks in class.\n", weeksInClass);
// } else {
// System.out.println("We have not started class yet.");
// }
// Switch
// int dayNumber = 1;
//
// switch (dayNumber) {
// case 1:
// System.out.println("Monday");
// //Fell through we need a break.
// break;
// case 2:
// System.out.println("Tuesday");
// break;
// default:
// System.out.println("Unknown");
// break;
// }
//While loop
// int i = 0;
//
// while (i < 10) {
// System.out.println("From a while loop: i = " + i);
// i++;
// }
//Do While loop
// int counter = 0;
// do {
// System.out.println("From a do while loop: counter = " + counter);
// counter++;
// } while (counter < 10);
//For loop
// for(int a = 0; a < 10; a++){
// System.out.println("From a for loop: a = " + a);
// }
for(int i = 1; i <= 10; i++){ // <--+
if(i % 2 == 1) { // |
continue; // ---------------+ Continue to the next iteration.
} else if( i > 5) {
break; // --------------------+ Break out of the loop.
} // |
System.out.println("i = " + i);// |
} // |
// <----------------------------------+
}
}