Operators, if, switch, loops
Operators
An Operators is a symbol that the compiler to perform manipulations such as assign a value to a variable or change a value of a variable or compare two values.
Arithmetic Operators
Operator | Example | Return value |
---|---|---|
+ | 5 + 2 | 7 |
- | 5 - 2 | 3 |
* | 5 * 2 | 10 |
/ | 5 / 2 | 2 |
% | 5 % 2 | 1 |
++ | ++5 + 2 |
8 (++ returns the value after incrementing.) |
5++ + 2 |
7 (++ returns the value before incrementing.) |
|
-- | --5 + 2 |
6 (-- returns the value after decrementing.) |
5-- + 2 |
7 (-- returns the value before decrementing.) |
Equality and Relational Operators
Operator | Example | Return value |
---|---|---|
> | 5 > 2 | true |
>= | 5 >= 2 | true |
< | 5 < 2 | false |
<= | 5 <= 2 | false |
== | 5 == 2 | false |
!= | 5 != 2 | true |
Conditional Operators
Operator | Example | Return value |
---|---|---|
&& |
int year = 2016; year >= 1901 && year <= 2000; |
false |
|| |
int age = 6; age < 6 || age >= 65; |
false |
? |
int a = 5; int b = 2; return a > b ? a : b; |
5 |
Assignment Operators
Operator | Example | Description |
---|---|---|
= | a = 9; | Assign 9 to a |
+= | a += b; | a = a + b; |
-= | a -= b; | a = a - b; |
*= | a *= b; | a = a * b; |
/= | a /= b; | a = a / b; |
%= | a %= b; | a = a % b; |
+ operator when operand is a string
when operand is a string, + operator performs string concatenation.
Example | Return value |
---|---|
1988 + ": Hi"; | "1988: Hi" |
if, switch, loops
if
int year = 2001; if (year >= 2001 && year <= 2100) { System.out.println("21st century"); }
21st century
int age = 65; if (age < 6 && age >= 65) { System.out.println("Free"); } else { System.out.println("Not Free"); }
Free
int age = 65; if (age >= 65) { System.out.println("Free"); } else if (age < 6) { System.out.println("Free"); } else { System.out.println("NOT Free"); }
Free
switch
int channel = 7; swicth (channel) { case 5: System.out.println("SBS"); break; case 7: case 9: System.out.println("KBS"); break; case 11: System.out.println("MBC"); break; default: System.out.println("Cabel TV"); }
KBS
for
int sum = 0; for (int i = 1; i <= 10; i++) { sum += i; } System.out.println(sum);
55
while
int sum = 0; int i = 1; while (i <= 10) { sum += i; i++; } System.out.println(sum);
55
do ~ while
int sum = 0; int i = 1; do { sum += i; i++; } while (i <= 10); System.out.println(sum);
55
- break;
- Breaks out of the inner loop
- continue;
- Skips rest of loop's body and makes program flow goto the boolean expression that controls the loop.
- return;
-
Terminates the method and makes program flow goto the line which call the method.
A return; is only available at void method.
- for (int i = 0; i < 10; i++) { }
-
In Java, you can declare the variable in the initial expression as the above.
Variable i is valid in {} of for statement.
OperatorsTest.java
public class OperatorsTest { public static void main(String[] args) { int a = 1; int b = 2; int c = 3; int d = 4; int e = 5; System.out.println("a = " + a); //a = 1 System.out.println("b = " + b); //b = 2 System.out.println("c = " + c); //c = 3 System.out.println("d = " + d); //d = 4 System.out.println("e = " + e); //e = 5 System.out.println("a + b = " + (a + b)); //a + b = 3 System.out.println("b - c = " + (b - c)); //b - c = -1 System.out.println("c * d = " + (c * d)); //c * d = 12 System.out.println("e / b = " + (e / b)); //e / b = 2 System.out.println("e % b = " + (e % b)); //e % b = 1 /* ++e and e++ are extremely differ in the operator precedence. */ System.out.println("e++ = " + e++); //e++ = 5 System.out.println("++e = " + ++e); //++e = 7 System.out.println("e-- = " + e--); //e-- = 7 System.out.println("--e = " + --e); //--e = 5 e++; //e++ equals ++e when it show alone. System.out.println("e = " + e); //e = 6 ++e; System.out.println("e = " + e); //e = 7 } }
Add the below to the source.
System.out.println("a > b " + (a > b)); System.out.println("b >= a " + (b >= a)); System.out.println("c < d " + (c < d)); System.out.println("d <= a " + (d <= a)); System.out.println("a == b " + (a == b)); System.out.println("a != b " + (a != b));
a > b false b >= a true c < d true d <= a false a == b false a != b true
Add the below to the source.
int yr = 2000; String msg = null; msg = yr >= 2001 && yr <= 2100 && msg.length() > 0 ? yr+" is 21C" : yr +" is not 21C"; System.out.println(msg); msg = null; msg = yr >= 2001 & yr <= 2100 & msg.length() > 0 ? yr+" is 21C" : yr +" is not 21C"; System.out.println(msg);
2000 is not 21C Exception in thread "main" java.lang.NullPointerException at Test.main(Test.java:46)
& msg.length() > 0 occurs an exception.
Unlike &&, & checks a following boolean expression,
even if the previous boolean expression is false.
Comments lines that occur exception.
//msg = yr >= 2001 & yr <= 2100 & msg.length() > 0 ? yr+" is 21C" : yr +" is not 21C"; //System.out.println(msg);
Add the below to the source.
class User { boolean signIn = true; String id = "Batman"; String authority = "ADMIN"; } User user = new User(); //Check whether login user's id is the "Superman" or authority is an "ADMIN". if ((user.signIn && user.id.equals("Superman")) || (user.signIn && user.authority.equals("ADMIN"))) { System.out.println("1st TEST: Superman OR ADMIN"); } user.authority = "USER"; if ((user.signIn && user.id.equals("Superman")) || (user.signIn && user.authority.equals("ADMIN"))) { System.out.println("2nd TEST: Superman OR ADMIN"); }
1st TEST: Superman OR ADMINReferences