-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02_Boolean_and_Logical_Operators
214 lines (166 loc) · 6.96 KB
/
02_Boolean_and_Logical_Operators
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# 02_Boolean_and_Logical_Operators
## Boolean
## Comparison Operators
## Logical Operators
### Variables - Analogy
A variable is something that can change. The name is fairly self-explanatory, right?
Imagine there are two boxes. One box is named **box1** and the other is named **box2**.
However, they are just names of the box. What's actually *inside* each box is different.
For example, when you opened the **box1**, you might find it **empty** or find **some stuffs**.
Also, you might decide to **empty the box** and **put something else in the box**.
Those are all properties of variables in programming.
<pre>
+------+ +------+
| box1 | | box2 |
+------+ +------+
</pre>
### Variables in Mathematics
If you learned variables in your maths class, it should be simpler to learn variables in programming.
If you've seen things like **x=5, y=6**, those x and y are variables.
### Varaibles in Programming
The boxes themselves we talked about above are **variables**. They contain something and what they contain various things. What they contain are called **values of variables**.
The names of the boxes are **variable names**. When you want to look at values of variables or give variables values, you use the variable names to find the exact box you want.
Of course, variable names have to be **unique**. (Actually, they don't have to be unique "everywhere", but we will talk about that later.) They are labels of boxes - when there are 5 identical boxes you can only find the right box by looking at the names.
When you look at the variables' values (i.e. opening boxes), we call that **reading**.
When you put something in the variables, we call that either **writing** or **assignment**.
## Types
### Types - Analogy
Not all boxes can take in everything. For example, some boxes can only take fishes and some can only take fruits.
### Common Types in Programming - Primitive Types
Some of the basic and most common "types" are **boolean**, **char**, **int**, and **double**.
#### int
int is short for integer. In maths, it's the set of all number like -5, -4, ..., 0, 1, 2, 3 .....
However, in programming it has limit. It can't go to negative infinite or positive infinite. We don't have to worry about the limit for now.
#### double
double is the set of all numbers like 1.356, 7.135, -0.182.....
However, in programming it also has limit on range just like int, and also has limit on how accurate it can represent some numbers. We will also not worry about it for now.
## Equal Sign - Assignment and Comparison
### Equal Sign in Mathematics
In maths, **5=5** means 5 is equal to 5.
**x=5** means x is equal to 5.
### Equal Sign in Programming
However, in programming, **x=5** means you put value 5 in the variable named x.
It is more like x <- 5.
### Comparison in Programming
Then how do we say two things are equal in programming?
You use two equal signs. So instead of doing 5=5, you do **5==5** which means 5 is equal to 5 in programming.
Likewise, **x==5** means x is equal to 5 in programming.
## Mathematical Operators
Addition uses "+". e.g. 1+2
Subtracion uses "-". e.g. 2-1
Multiplication uses "*". e.g. 2*3
Division uses "/". e.g. 1/2
## Learn And Exercises
### Recap: Create a class named "Variables" in the project you created from lesson 0 in the same way.
Review lesson 0 if this is difficult.
### Exercise 0 : Two variables with different types.
When you first *create* a variable, you specify its type. It is a good idea to also *initialize* the variable with a value.
This is called *declaration*, or *to declare a variable*. Here is an example of how you *declare an int* and *assign* it a value.
```
int myInt = 0;
```
*myInt* is a name of the variable, *int* is its type, and 0 is its current value.
1. In main function, create an int type variable named *number* with value 1. Print it. Try running it!
2. Follwing 1, create a double type variable named *doubleNumber* with value 1.3. Print it. Try running it!
e.g.
```
package GwcS2019Chena;
public class Variables {
public static void main(String[] args) {
int number = 1;
System.out.println(number);
double doubleNumber = 1.3;
System.out.println(doubleNumber);
}
}
```
Results in console:
```
1
1.3
```
### Exercise 1 : Changing values in a variable.
You can change a value of a variable you declared already. That's why it's called a *variable*!
Continue in the same funciton you coded in exercise 0.
1. Change the value of *number* to 2. Print it. Try running it!
2. Change the value of *doubleNumber* to 2.5. Print it. Try running it!
e.g.
```
public class Variables {
public static void main(String[] args) {
int number = 1;
System.out.println(number);
double doubleNumber = 1.5;
System.out.println(doubleNumber);
number = 2;
System.out.println(number);
doubleNumber = 2.5;
System.out.println(doubleNumber);
}
}
```
Results in console:
```
1
1.3
2
2.5
```
### Exercise 2 : Two variables with incompatible values. Commenting Code
1. Create another int type variable named "anotherNumber* with value 2.3. What happened?
> You should be seeing a red line on your code. Hover your mouse over it. Can you explain why that happened?
> int type variable cannot take non-integer value! If you try running your code, what happens?
2. Putting // in front of each line let you *comment* your code. This means the line is a comment for human, and computer won't run that line. Try putting // in front of the line that just errored out. Can you run your code now?
3. Create another double type variable named "anotherDoubleNumber* with value 2. Print it. Try running it! What happened?
> Can you see it printed 2.0 instead of 2? Can you explain why that happened?
e.g.
```
//int anotherNumber = 2.3;
double anotherDoubleNumber = 2;
System.out.println(anotherDoubleNumber);
```
Results in console:
```
2.0
```
### Exercise 3 : Mathematical Operators
Print can take an operation as well. For example, you can do
```
System.out.println(x+2);
```
1. Print number+1, doubleNumber-1, number*2, and doubleNumber/2
2. Print number and doubleNumber again. Did they change? Why not?
e.g.
```
System.out.println(number+1);
System.out.println(doubleNumber-1);
System.out.println(number*2);
System.out.println(doubleNumber/2);
System.out.println(number);
System.out.println(doubleNumber);
```
Results in console:
```
3
1.5
4
1.25
2
2.5
```
### Exercise 4 : Modifying value of a variable using operation
You can modify a vairable using operation. For example, **x=x+1** will take x value, add 1 to it, and save it back into x. If x was 2 in the beginning, after **x=x+1**, x will become 3.
1. Try the following code snippet and explain what happened, and why it happened. You will see a type mismatch.
```
number = doubleNumber+2;
```
2. Now instead, increase doubleNumber's value by 2 and save it back into doubleNumber. Print it. Did it change from what it was before?
e.g.
```
doubleNumber = doubleNumber+2;
System.out.println(doubleNumber);
```
Results in console:
```
4.5
```