Math Wars
Spaceship Commander
Lecture Project
Homework Project
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) {
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 leavingWhen 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.
Input?
public static void NAME() {
STATEMENTS;
}
private static void makeBread() {
print("Your bread is ready, sir!");
}
public static void main (String[] args) {
// Call method
makeBread();
}public static void method(TYPE NAME) {
STATEMENTS;
}
private static void makeBread(int flourAmount) {
print("Your bread with " + flourAmount +
"kg of flour is ready, sir!");
}
public static void main (String[] args) {
makeBread(5);
}public static void NAME(TYPE NAME, TYPE NAME) {
STATEMENTS;
}
private static void makeBread(int flourAmount, int waterAmount) {
print("Your bread with " + flourAmount + "kg of flour and "
+ waterAmount + "L of water is ready, sir!");
}
public static void main (String[] args) {
makeBread(5, 2);
}public static TYPE NAME() {
STATEMENTS
return EXPRESSION;
}void is the word for "no type"
void a = ""; // !?
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;
}
return result;
}
public static void main (String[] args) {
print(pow(2, 3));
}They are a block of code