Conditionals
boolean isLectureDay = true;
if(isLectureDay == true) {
goToLecture();
writeSomeCode();
}
if(CONDITION) {
STATEMENT1;
STATEMENT2;
...
STATEMENTN;
}
x > y: x is greater than y
x < y: x is less than y
int a = 10;
if(a = 10) {
// "=" Really !?
}
if("Test" == "Test") { // !?
// "Test".equals("Test")
}
x >= y: x is greater than or equal to x
x <= y: x is less than or equal to y
x == y: x equals y
&&: logical AND
||: logical OR
boolean isProgrammerAtHome = true;
int workingHours = 3;
if(isProgrammerAtHome) {
if(workingHours < 8) {
// You are fired :(
}
}
boolean isProgrammerAtHome = true;
int workingHours = 3;
if(isProgrammerAtHome && workingHours < 8) {
// You are fired :(
}
boolean isProgrammerHome = true,
shouldFireProgrammer = false;
if(!isProgrammerHome == true) {
System.out.println("WORK HARDER!");
} else {
shouldFireProgrammer = true;
}
if(CONDITION) {
STATEMENT1;
} else {
STATEMENT2;
}
if (CONDITION) {
STATEMENTS
} else if (CONDITION) {
STATEMENTS
} else if (CONDITION) {
STATEMENTS
} else {
STATEMENTS
}
boolean isProgrammerAtHome = false;
int workingHours = 8;
if(isProgrammerAtHome) {
// Fire
} else if(workingHours < 8) {
// Fire
} else {
// WORK HARDER!
}
int workingHours = 8;
// Default value is ...?
boolean shouldReceiveBonus;
switch(workingHours) {
case 8:
shouldReceiveBonus = true;
break;
case 9:
shouldReceiveBonus = true;
break;
default:
shouldReceiveBonus = false;
}
int workingHours = 8;
boolean shouldReceiveBonus;
switch(workingHours) {
case 8:
case 9:
shouldReceiveBonus = true;
break;
default:
shouldReceiveBonus = false;
}
switch(CONDITION) {
case CONDITION1:
break;
case CONDITIONN:
break;
default:
}
switch(1) {
case 1:
print("No break !?")
case 2:
print("I am leaving")
}
// Result:
// No break !?
// I am leaving
When the method execution is finished the program returns to the area of the code from which it was called and continues on to the next line of code.
public static void method(String[] arguments) {
System.out.println("I am a method");
}
public static void NAME() {
STATEMENTS;
}
NAME();
To call a method:
public static void method(TYPE NAME) {
STATEMENTS;
}
public static void printHi() {
print("Hello!")
}
public static void printNumbers() {
print(42); // 42 + "" ?
}
public static void print(String message) {
System.out.println(message);
}
public static int calculateDDS(int a, int b) {
return 0.2 * (a + b);
}
calculateDDS(50, 60);
public static void NAME(TYPE NAME, TYPE NAME) {
STATEMENTS;
}
public static TYPE NAME() {
STATEMENTS
return EXPRESSION;
}
void is the word for "no type"
void a = ""; // !?
public class Math {
public static int multiply(int a, int b) {
return a * b;
}
public static int pow(int number, int degree) {
int result = 0;
switch(degree) {
case 2:
result = multiply(number, number);
break;
case 3:
result = multiply(multiply(number, number),
number);
break;
}
}
}
They are a block of code